Solved

Sorting the arrays numbers

Posted on 2006-11-28
12
6,334 Views
Last Modified: 2008-01-09
int function(int a[] ) ;

main ()
{
     int i;
     int k[something];
     for (i=0;i<5 ; i ++ )
     {

          k[i] = // something which generates random number

          printf("%d", function(k)); // if the randoms are 12 8 34 5; i want to print the numbers like  5,8,12,34
     }

}

int function (int k[] )
{  
int i, j, g;
int n=5; // there must be n but i dont know what, so assume 5.
for ( i=n-1 ; i > 0 ; i-- )
        for  (j=0; j<1; j++ )
                if ( k[j]>k[j+1] ) {
                                g = k [j];
                                k[j]=k[i];
                                k[i]=k;
                    }
}


how can ?
0
Comment
Question by:thefirstfbli
  • 6
  • 3
  • 2
  • +1
12 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 18026403
main ()
{
  int i;
  int k[5];
  for (i=0;i<5 ; i ++ )
    {

      k[i] = i*37%50; // something which generates random number
    }
  function(k);
  for (i=0;i<5 ; i ++ )
    {
        printf("%d, ", k[i]); // if the randoms are 12 8 34 5; i want to print the numbers like  5,8,12,34

    }



}

int function (int k[] )
{
  int i, j, g;
  int n=5; // there must be n but i dont know what, so assume 5.
  for ( i=n-1 ; i > 0 ; i-- )
    for  (j=0; j<i; j++ )
      if ( k[j]>k[i] ) {
        g = k [j];
        k[j]=k[i];
        k[i]=g;
      }
}
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 400 total points
ID: 18026510
See my comments in the code :

#include <stdio.h>       /* <-- for output */
#include <stdlib.h>      /* <-- for random number generation */
#include <time.h>       /* <-- idem (to get a good seed) */

int function(int a[], int n) ;  /* <-- the prototype is changed to also pass the array length !! */

int main ()                        /* <-- return type of main has to be int */
{
     int i;
     int k[5];                       /* <-- you want to store 5 ints in the array */
     srand(time(0));            /* <-- initialize the random number generator */
     for (i=0;i<5 ; i++ )
     {
          k[i] = rand() % 100; /* <-- generates a pseudo random number between 0 and 99 */
     }

     function(k, 5);               /* <-- sort the array (also pass the length of the array !!) */

     for (i=0;i<5 ; i++ )        /* <-- prints the sorted array */
     {
          printf("%d\n", k[i]);
     }

     return 0;                       /* <-- main has to return an int */
}

int function (int k[], int n)               /* <-- the length of the array is also passed !! */
{  
int i, j, g;
for ( i=n-1 ; i > 0 ; i-- )
        for  (j=0; j<i; j++ )               /* <-- used j<i instead of j<1 as condition */
                if ( k[j]>k[j+1] ) {
                                g = k [j];
                                k[j]=k[i];
                                k[i]=g;         /* <-- note that I put g here instead of k */
                    }
return 0;                                      /* <-- you defined your function to return an int, so you have to do that */
}


I tried to understand the sorting algorithm you wanted to implement - I hope I got it right. If you intended a different sorting algorithm, then please say which ...

If something is not clear to you, then please ask ! Don't take this code without understanding why I made the changes I did !

More info for rand() :
http://www.cplusplus.com/ref/cstdlib/rand.html
0
 
LVL 3

Expert Comment

by:sreenathk
ID: 18026523
#include <stdio.h>
#include <stdlib.h>
void function(int a[], int n ) ;

main ()
{
     int i;
     int k[5];
     for (i=0;i<5 ; i ++ )
     {

          k[i] = rand();// something which generates random number

     }
       k[5] = 0;
       function(k,sizeof(k)/sizeof(int));


}

void function (int k[], int n )
{  
      int i, j, g;
      // int n=5; // there must be n but i dont know what, so assume 5.
      // Pass n as parameter instead of local variable
      for ( i= n-1 ; i > 0 ; i-- )
      {
        for  (j=0; j< i; j++ )
            {
                if ( k[j]>k[i] )
                        {
                    g = k [j];
                    k[j]=k[i];
                    k[i]=g;
                }
            }
      }
      for (i=0; i<n;i++)
            printf("%d \n",k[i] ); // if the randoms are 12 8 34 5; i want to print the numbers like  5,8,12,34

}

This will work for your cause..

add one more parameter to function.. to get the size of array atomatically...
Move the print statement to function.. or out to main loop.. but use for loop to print the array...

Thanks,
Sreenath
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18026547
@sreenathk :
>>      k[5] = 0;
Buffer overflow :) Memory corruption :)
0
 
LVL 3

Expert Comment

by:sreenathk
ID: 18026849
Yaa.. But I lost interest in this question.. as he is asking School Assignments.. Just for points I can not answer..

I think these kind of obvious questions should not be repeated..

What do you say?

Sreenath
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18026904
I meant that in your code you're writing past the boundaries of the array, which corrupts memory. I pointed that out to make sure that thefirstfbli does not copy that mistake !
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 1

Author Comment

by:thefirstfbli
ID: 18027121
first of all, this not a school assigment.. i try to understand C.. also if it is school assigment, i wrote almost all code.. in adition to this, really i dont try to create random numbers,  i just write it to here because easy to write, as a result, the array was there on my code not for randoms, some different things.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18027214
Did our replies help you ? Or do you still have questions or problems ?
0
 
LVL 1

Author Comment

by:thefirstfbli
ID: 18027233
return 0;                                      /* <-- you defined your function to return an int, so you have to do that */

i forget to write this line so; i think because of this ? it returns nothing.. thanks..  i try to modified it..
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18027350
Alternatively you could define the function to return nothing :

    void function(int a[], int n);
0
 
LVL 1

Author Comment

by:thefirstfbli
ID: 18027366
and no return in definition.. ok.. thaks for everyting..
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18027367
>> i think because of this ? it returns nothing
What do you mean ? Do you mean that your code didn't output what you wanted ? That's because of this line :

          printf("%d", function(k)); // if the randoms are 12 8 34 5; i want to print the numbers like  5,8,12,34

See the code (and comments) I posted to fix this line ...
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to setup iphone app subscription service 3 119
Beginner Probleme with my IDE Code::Blocks C++ 5 233
Read file descriptor 3 in C 14 104
how to understand recursion 12 218
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now