Solved

Sorting Random Numbers (and making non 0 based)

Posted on 2004-08-28
6
224 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

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…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

832 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