Link to home
Start Free TrialLog in
Avatar of thefirstfbli
thefirstfbli

asked on

Sorting the arrays numbers

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 ?
Avatar of ozo
ozo
Flag of United States of America image

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;
      }
}
ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
#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
@sreenathk :
>>      k[5] = 0;
Buffer overflow :) Memory corruption :)
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
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 !
Avatar of thefirstfbli
thefirstfbli

ASKER

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.
Did our replies help you ? Or do you still have questions or problems ?
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..
Alternatively you could define the function to return nothing :

    void function(int a[], int n);
and no return in definition.. ok.. thaks for everyting..
>> 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 ...