GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 10-Nov-2005, 16:25
SergeDrago's Avatar
SergeDrago SergeDrago is offline
New Member
 
Join Date: Nov 2005
Location: Here
Posts: 6
SergeDrago is on a distinguished road

Create ten random numbers between 1 and 99999


The program is supposed to create ten random numbers between 1 and 99999, then sort them using the bucket sort method. The compiler is C++. Any help would be appriciated.

CPP / C++ / C Code:
#include<iostream>
using namespace std;
#include<iomanip>
using std::setw;
#include<cstdlib>

const int SIZE = 10;

void zerobucket( int bucket [][ SIZE ]);
int numberofdigits ( int numberarray [] );
void printnums ( int numberarray [] );
void distelements ( int numberarray [], int bucket[][ SIZE ], int digit );
void collectelements ( int numberarray [], int bucket [][ SIZE ] );


int main() {

int array[SIZE] = {};
int digdug;
for ( int count = 1; count <= SIZE; count++) {
 array[count] = ( 1 + rand() % 100 );
 }

printnums ( array );
digdug = numberofdigits ( array );

int bucket [ SIZE ][ SIZE ];

zerobucket( bucket );
distelements( array, bucket, digdug );

return 0;
}

void zerobucket ( int bucket [][SIZE] ) {
 int M, N;

 for ( M = 0; M <= 10; M++){
  for ( N = 0; N <= 10; N++){
   bucket [M][N] = (0);
  }
 }

return;

}


int numberofdigits ( int numberarray[] ) {

int counter = 0;
int MAX = numberarray[counter];

for (counter = 0; counter <= SIZE; counter++) {
 if ( numberarray[counter] < numberarray[counter + 1]) {
  MAX = numberarray[counter + 1];
 }
}

int dig = 0;

if ( MAX / 10 == 0) {
 if ( MAX / 100 == 0 ) {
  if ( MAX / 1000 == 0 ) {
   if ( MAX / 10000 == 0 ) {
    if ( MAX / 100000 == 0 ) {
     dig = 5;
    }
   }
   else {
    dig = 4;
   }
  }
  else {
   dig = 3;
  }
 }
 else {
  dig = 2;
 }
}
else {
 dig = 1;
}

cout << "The largest number is " << MAX << " and contains " << dig << " digits.\n\n\n";

return dig;

}

void printnums ( int numberarray[] ) {

cout << "Array elements in original order from file:\n" << setw(4);
 for ( int count = 0; count <=SIZE; count++) {
  cout << numberarray[count];
 }

 cout << setw(0) << "\n\n\n";
}


void distelements ( int numberarray[], int bucket[][SIZE], int digits ) {

int value;
int place = 1;
int enddigit;

for ( int count = 1; count <= digits; count++ ) {
 switch (count) {

  case 1:
   value = numberarray[place];
   enddigit = (value % 10);
   bucket[enddigit][place] = (value);
   place++;
  break;

  case 2:
   value = numberarray[place];
   enddigit = (value / 10);
   bucket[enddigit][place] = (value);
   place++;
  break;

  case 3:
   value = numberarray[place];
   enddigit = (value / 100);
   bucket[enddigit][place] = (value);
   place++;
  break;

  case 4:
   value = numberarray[place];
   enddigit = (value / 1000);
   bucket[enddigit][place] = (value);
   place++;
  break;

  case 5:
   value = numberarray[place];
   enddigit = (value / 10000);
   bucket[enddigit][place] = (value);
   place++;
  break;
 }
 collectelements ( numberarray, bucket );
}

return;
}


void collectelements ( int numberarray[], int bucket[][SIZE] ) {

 int value, BITE;
 int count = 1;

for ( BITE = 0; BITE <= SIZE; BITE++) {
 for ( value = 0; value <= SIZE; value++) {
  if (bucket[BITE][value] > 0) {
   (numberarray[value]) = (bucket[BITE][value]);
  }
 }
}

cout << "After distribution/collection pass #" << count << ":" << endl;
for ( count = 1; count <= SIZE; count++) {
 cout << setw(4) << numberarray[count] << "\n\n";
}

return;
}

Last edited by admin : 10-Nov-2005 at 17:48. Reason: Please insert your C code between [c] & [/c] tags
  #2  
Old 10-Nov-2005, 18:29
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough
Thumbs up

Re: Create ten random numbers between 1 and 99999


Hi SergeDrago,

Welcome to the GID forums.
Did you read the Guidelines?

For generating random number always you seed it first. You need to use the srand() function first (and only once). The typical way is to include ctime header and the line.
CPP / C++ / C Code:
srand(time(NULL));
to initialize the generator.

If you want to generate random numbers between 1 and 99999, you should not use an integer because its maximum value is 32767.
So, you can use a float like this:
CPP / C++ / C Code:
float array[SIZE];

and to generate random numbers, you can do like this:
CPP / C++ / C Code:
for ( int count = 1; count < SIZE; count++) {
 array[count] = 99999 * ( float )rand( ) / RAND_MAX;
 }

RAND_MAX is the maximum random number that can be generated.
so, if we divide the number generated by rand() function and divide it by RAND_MAX, then the result is between 0 and 1.
Then we multiply 99999 so that the result is between 0 and 99999.
If you want the numbers between 1 and 99999, just replace the numbers where you have 0 by 1.

The code for finding the number of digits can be replaced by a single for loop.
Instead of this:
CPP / C++ / C Code:
int dig = 0;

if ( MAX / 10 == 0) {
 if ( MAX / 100 == 0 ) {
  if ( MAX / 1000 == 0 ) {
   if ( MAX / 10000 == 0 ) {
    if ( MAX / 100000 == 0 ) {
     dig = 5;
    }
   }
   else {
    dig = 4;
   }
  }
  else {
   dig = 3;
  }
 }
 else {
  dig = 2;
 }
}
else {
 dig = 1;
}

can be replaced by:
CPP / C++ / C Code:
int dig = 0;
int temp;

for(dig = 0, temp = MAX; temp > 0; dig++)
temp = temp / 10;

Then, the line:
CPP / C++ / C Code:
for (counter = 0; counter <= SIZE; counter++) {
Should be replaced by:
CPP / C++ / C Code:
for (counter = 0; counter < SIZE; counter++) {
since the array index starts from zero and ends with SIZE - 1.
You should also change that in all the places where you used <= by <.

Regards,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #3  
Old 10-Nov-2005, 18:45
SergeDrago's Avatar
SergeDrago SergeDrago is offline
New Member
 
Join Date: Nov 2005
Location: Here
Posts: 6
SergeDrago is on a distinguished road

Re: Create ten random numbers between 1 and 99999


Thanks for help Peramesh
  #4  
Old 10-Nov-2005, 23:25
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,435
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Create ten random numbers between 1 and 99999


Quote:
Originally Posted by Paramesh
If you want to generate random numbers between 1 and 99999, you should not use an integer because its maximum value is 32767.
Not true. An integer today is 4 bytes, not 2. Therefore an integer can be 4 billion plus.

The limit is that rand() usually generates a value from 0 to 32767.

Quote:
Originally Posted by Paramesh
... and to generate random numbers, you can do like this:
CPP / C++ / C Code:
for ( int count = 1; count < SIZE; count++) {
 array[count] = 99999 * ( float )rand( ) / RAND_MAX;
 }

RAND_MAX is the maximum random number that can be generated.
so, if we divide the number generated by rand() function by RAND_MAX, then the result is between 0 and 1.
Then we multiply 99999 so that the result is between 0 and 99999.
If you want the numbers between 1 and 99999, just replace the numbers where you have 0 by 1.
Useful technique... I like this! But be sure to parenthesize it for readability:
CPP / C++ / C Code:
array[count] = 99999 * (( float )rand( ) / RAND_MAX);
__________________

Definition: Politics
Latin, from
poly meaning many and
tics meaning blood sucking parasites
-- Tom Smothers
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 23:03.


vBulletin, Copyright © 2000 - 2014, Jelsoft Enterprises Ltd.