Hello everyone, this is my first post.
As can be quite rightly guessed, I am new to C++ and struggling with some coursework.

My task is to produce a program that prints out 5000 random numbers between 1-5, also printing out the frequency of each given number.

I have searched for an answer as to how this might be done, and I believe it might have something to do with Arrays, could someone please give me a hint whether this is right?

Here is my code so far

#include <vcl.h>
#include <iostream.h>

using namespace std;

int main(void)
{
    int random_integer;
    for(int numero=0; numero < 5000; numero++){
    random_integer = 0 + int(6 * rand()/(RAND_MAX+1.0));
    cout << random_integer << endl;
}
    cin.get();
    return 0;
}

I realize that it is currently printing out only the random numbers...

You would be required to have an array of the size the same as the range of numbers to be generated. For eg. as in your case, the range is 5, hence create an array of 5 elements, and for each random number generated, do something like:

int frequencyArray [RANGE] = { 0 };
random_integer = int(6 * rand()/(RAND_MAX+1.0));

// asssuming that the generated number is between 1 and 5.
++frequencyArray [randomNumber - 1];

BTW, why the need to add zero to your random number equation, which I think would also generate 0 which you don't need.

Consider doing:

srand ( static_cast<unsigned int> ( time ( 0 ) ) );
random_integer = 1 + int(5 * rand()/(RAND_MAX+1.0));

I managed to figure it out. The code is sans Arrays due to the realization that I am not supposed to know how to use them yet. Sincere thanks for the help ~s.o.s~. For posterity, here is the code:

#include <vcl>
#include <iostream>
#include <ctime>

using namespace std;

int main(void)
{
srand((unsigned)time(0));
int random_integer;
int number1 = 0;
int number2 = 0;
int number3 = 0;
int number4 = 0;
int number5 = 0;
int lowest=1, highest=5;
int size=(highest-lowest)+1;

for(int n=0; n < 5000; n++)
{
random_integer = alin+int(koko*rand()/(RAND_MAX+ 1.0));
 cout << random_integer << endl;
  if (random_integer == 1) numero1++;
   else if (random_integer == 2) numero2++;
    else if (random_integer == 3) numero3++;
     else if (random_integer == 4) numero4++;
      else if (random_integer == 5) numero5++;
}
cout << random_integer << endl;
cout << "\n1 = "<<number1<<""<<endl;
cout << "2 = "<<number2<<""<<endl;
cout << "3 = "<<number3<<""<<endl;
cout << "4 = "<<number4<<""<<endl;
cout << "5 = "<<number5<<""<<endl;

cin.get();
return 0;
}

I presume that the the aesthetics of my code do not appeal to patrons :P

Member Avatar for iamthwee

>I presume that the the aesthetics of my code do not appeal to patrons

No but your avatar does ;)

>#include <iostream.h>
No it should be just iostream.

Don't forget to align your braces and properly indent your code.

Thanks for the pointers (and praise for my avatar :D) I edited the code accordingly.

Member Avatar for iamthwee
#include <vcl>
#include <iostream>
#include <ctime>

using namespace std;

int main ( void )
{
   srand ( ( unsigned ) time ( 0 ) );
   int random_integer;
   int number1 = 0;
   int number2 = 0;
   int number3 = 0;
   int number4 = 0;
   int number5 = 0;
   int lowest = 1, highest = 5;
   int size = ( highest - lowest ) + 1;

   for ( int n = 0;  n < 5000;  n++ )
   {
      random_integer = alin + int ( koko * rand() / ( RAND_MAX + 1.0 ) );
      cout << random_integer << endl;
      if ( random_integer == 1 ) 
          numero1++;
      else if ( random_integer == 2 ) 
          numero2++;
      else if ( random_integer == 3 ) 
          numero3++;
      else if ( random_integer == 4 ) 
         numero4++;
      else if ( random_integer == 5 ) 
         numero5++;
   }
   cout << random_integer << endl;
   cout << "\n1 = " << number1 << "" << endl;
   cout << "2 = " << number2 << "" << endl;
   cout << "3 = " << number3 << "" << endl;
   cout << "4 = " << number4 << "" << endl;
   cout << "5 = " << number5 << "" << endl;

   cin.get();
   return 0;
}

Well I might be tempted to do something like the above.

Also does cout << "\n1 = " << number1 << "" << endl; have any purpose?

Also does cout << "\n1 = " << number1 << "" << endl; have any purpose?

That bit was left over from an a planned text output thingy. Good catch.

Let me guess: you ran the code through a 'beautifier' program of some sort? Or did you blaze through it pressing space :?:. I must agree it looks better that way. Case closed!

Member Avatar for iamthwee

Yes something like that, enjoy your stay at daniweb.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.