Solved

Sorting Random Numbers (and making non 0 based)

Posted on 2004-08-28
6
208 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

747 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

11 Experts available now in Live!

Get 1:1 Help Now