Solved

Sorting Random Numbers (and making non 0 based)

Posted on 2004-08-28
6
230 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

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