Solved

Sorting Random Numbers (and making non 0 based)

Posted on 2004-08-28
6
217 Views
Last Modified: 2010-04-01
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

0
Comment
Question by:fattumsdad
  • 3
  • 2
6 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 11923060
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;
}
0
 
LVL 23

Expert Comment

by:brettmjohnson
ID: 11923099
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;
}
0
 
LVL 1

Author Comment

by:fattumsdad
ID: 11923126
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
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 86

Accepted Solution

by:
jkr earned 150 total points
ID: 11923159
Sorry, that should be

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

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[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]);
printf ( "\n");

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

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

system("pause");
 return 0;
}

(tested with VC6)
0
 
LVL 1

Author Comment

by:fattumsdad
ID: 11923297
jkr,

Thank you!!  I appreciate the help!!

Regards,
Tony
0
 
LVL 86

Expert Comment

by:jkr
ID: 11924013
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)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

863 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

22 Experts available now in Live!

Get 1:1 Help Now