Solved

Sorting the arrays numbers

Posted on 2006-11-28
12
6,337 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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
 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
TCP/IP Socket - connection close results in data lost 14 140
Need example 5 122
Memory going from 12gb to 64gb or 96gb. worth it? 15 203
A  &&  B  || (C||D) how it works in c 7 103
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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

828 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