| | |
Counting the frequency of random numbers
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
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
I realize that it is currently printing out only the random numbers...
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
C++ Syntax (Toggle Plain Text)
#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...
Last edited by Armfelt; Apr 21st, 2007 at 1:20 pm.
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:
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:
C++ Syntax (Toggle Plain Text)
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:
C++ Syntax (Toggle Plain Text)
srand ( static_cast<unsigned int> ( time ( 0 ) ) ); random_integer = 1 + int(5 * rand()/(RAND_MAX+1.0));
Last edited by ~s.o.s~; Apr 21st, 2007 at 1:30 pm.
I don't accept change; I don't deserve to live.
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:
I presume that the the aesthetics of my code do not appeal to patrons
C++ Syntax (Toggle Plain Text)
#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
Last edited by Armfelt; Apr 21st, 2007 at 3:11 pm. Reason: code indenting enhancements & minor tweaks..
C++ Syntax (Toggle Plain Text)
#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? *Voted best profile in the world*
•
•
•
•
Also does cout << "\n1 = " << number1 << "" << endl; have any purpose? 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! ![]() |
Similar Threads
- Compile time errors in C++ while generating random numbers (C++)
- C++ Random Numbers (C++)
- random numbers all different??? (C++)
Other Threads in the C++ Forum
- Previous Thread: Help Needed on C++ assignment
- Next Thread: usin the ?: operator
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node output parameter pointer problem program programming project proxy python read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






) I edited the code accordingly. 