Solved

Sorting the arrays numbers

Posted on 2006-11-28
12
6,333 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

757 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

21 Experts available now in Live!

Get 1:1 Help Now