• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6346
  • Last Modified:

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 ?
0
thefirstfbli
Asked:
thefirstfbli
  • 6
  • 3
  • 2
  • +1
1 Solution
 
ozoCommented:
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
 
Infinity08Commented:
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
 
sreenathkCommented:
#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
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
Infinity08Commented:
@sreenathk :
>>      k[5] = 0;
Buffer overflow :) Memory corruption :)
0
 
sreenathkCommented:
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
 
Infinity08Commented:
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
 
thefirstfbliAuthor Commented:
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
 
Infinity08Commented:
Did our replies help you ? Or do you still have questions or problems ?
0
 
thefirstfbliAuthor Commented:
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
 
Infinity08Commented:
Alternatively you could define the function to return nothing :

    void function(int a[], int n);
0
 
thefirstfbliAuthor Commented:
and no return in definition.. ok.. thaks for everyting..
0
 
Infinity08Commented:
>> 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

  • 6
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now