Link to home
Start Free TrialLog in
Avatar of fattumsdad
fattumsdad

asked on

Sorting Random Numbers (and making non 0 based)

Hello all,

Quick question here...  I have a program that generates 5 random numbers and then displays them on the screen.  How do I sort the 5 numbers from smallest to largest before they display?  Also, how should I alter this code to make the random numbers between 1 and 47 vice 0 and 47?  And finally, how do I put a 5 second (or so) delay between each number to avoid any possible duplicates?  The code I have is below:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main ()
{
  srand ( time(NULL) );
 
  int n1 = rand()%47;
  int n2 = rand()%47;
  int n3 = rand()%47;
  int n4 = rand()%47;
  int n5 = rand()%47;
 
  printf ("%d ", n1);
  printf ("%d ", n2);
  printf ("%d ", n3);
  printf ("%d ", n4);
  printf ("%d ", n5);

  system("pause");
  return 0;
}


As usual, any and all help is greatly appreciated.  Thanks in advance!!

Regards,

Tony

Avatar of jkr
jkr
Flag of Germany image

You can do that by using an array and 'qsort()', e.g.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int compare( (void *) elem1, (void *) elem2 ) {

   int n1 = (int) elem1;
   int n2 = (int) elem2;

   if (n1 > n2) return 1;
   if (n1 < n2) return -1;

   return 0;
}

int main ()
{
 srand ( time(NULL) );
 int n[5];

 n[0] = rand()%47;
 n[1] = rand()%47;
 n[2] = rand()%47;
 n[3] = rand()%47;
 n[4] = rand()%47;

 for ( int i = 0; i < 5; ++i) printf ("%d ", n[i]);
 
 qsort ( (void*) n, 5, sizeof(int), compare);

 for ( int i = 0; i < 5; ++i) printf ("%d ", n[i]);

 system("pause");
  return 0;
}
You will find it much easier to visualize what you need to do if you
store the random numbers in an array:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define NUMS      5

int main ()
{
 srandom ( time(NULL) );
 int i;
 int numarray[NUMS];
 
 for (i = 0; i < NUMS; i++)
   numarray[i] = random()%47;      // mod 47???

 printf("Original Order:  );
 for (i = 0; i < NUMS; i++)
    printf("%d  ", numarray[i])
 printf("\n");


 /* sort the array elements here */


 printf("Sorted Order:  );
 for (i = 0; i < NUMS; i++)
    printf("%d  ", numarray[i])
 printf("\n");

 system("pause");
  return 0;
}
Avatar of fattumsdad
fattumsdad

ASKER

What compiler are you folks using?  I cut & pasted each code into a new project, and get multiple errors in each?  I am using Dev-C++ (Bloodshed).  I have Visual C++ 6.0 as well.  If I paste this into a new project, should it be a Win32 Console Project?  Console that supports MFC?  

I'm *very* new to C++ so if anyone has a better idea of how to make this number generator, I'll all for it :)

Thanks,

Tony
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany 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
jkr,

Thank you!!  I appreciate the help!!

Regards,
Tony
Actually, we can do better :o)

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_NUMS

int __cdecl compare( const void * elem1, const void * elem2 ) {

 int n1 = *(int*) elem1;
 int n2 = *(int*) elem2;


 if (n1 > n2) return 1;
 if (n1 < n2) return -1;

 return 0;
}

int main ()
{
srand ( time(NULL) );
int n[MAX_NUMS];

for ( int i = 0; i < MAX_NUMS; ++i) n[i] = rand()%47;

for ( int i = 0; i < MAX_NUMS; ++i) printf ("%d ", n[i]);
printf ( "\n");

qsort ( (void*) n, MAX_NUMS, sizeof(int), compare);

for ( i = 0; i < MAX_NUMS; ++i) printf ("%d ", n[i]);
printf ( "\n");

system("pause");
return 0;
}

BTW, I shamelessly used brettmjohnson's good point of #defining the number of numbers as a perprocessor symbol :o)