I'm a beginner at c++ and I am trying to find the frequency of numbers(1-6) from a random number generator of 100 numbers. The only commands that I can use are rand, srand, cin, cout, loops, and if else. Is it possible to create a program that shows the frequency using only these commands? Thank you.
-
Sounds possible enough. Do you have a general idea or algorithm worked out? Have you tried putting it into code? Where are you getting stuck? – Mike Christensen Nov 06 '14 at 20:03
-
If you can use `std::map` you can do something similar to what I do in my [answer here](http://stackoverflow.com/a/17798317/1708801). I just used a modified example from cppreferences page on the [random header](http://en.cppreference.com/w/cpp/numeric/random) which generates some nice text histograms. – Shafik Yaghmour Nov 06 '14 at 20:04
2 Answers
You can use a std::map<int, int>
where first
would be the random number and second
would be the count.
Therefore the frequency would be the count / total.

- 114,268
- 16
- 167
- 218
-
This might be overkill if you're just tracking numbers 1-6. An `int[6]` should be sufficient. – Mike Christensen Nov 06 '14 at 20:07
-
1An int[6] would be used as a histogram: int histogram[6]. You'd start off by setting the array to zero, then for each number 1 to 6, you'd increment a value in the array: histogram[number-1]++ . – rcgldr Nov 06 '14 at 20:09
Consider the function of each of the commands given to you:
srand(int): seeds your random number generator; you can give this a simple number (say 0 or 1, or 7) so that every time you run your code, your random number generator will have the same output (good for checking your progress. It's difficult to tell if you're going in the right direction if you're getting different input every time)
rand(): generates a random number for you between 0 and RAND_MAX. For generating numbers within a certain range, look at this thread (How does modulus and rand() work?).
cin: reads input from the user.
cout: outputs to the console (ex. cout << "string"; will output a the word "string" to the console)
loops: your two main loops while and for. While loops goes only until a certain condition is met; a for loop allows you to create a temporary variable, check a condition, and increment a variable. Hint: for loops are better when you know exactly how many iterations you want to run through.
if/else: Allows you to check a given condition, and either execute one block of code if that condition results to true or go to another block of code (from the else) if false. Note that you can also establish else if's (ie, if(condition) { // code } else if(condition) { // code } ).
Now looking back at the question, what do we need to solve this? 1. A way to generate random numbers (rand) 2. A way to control how many numbers we are generating (loop) 3. A way to check if the number we generated is a certain number (if/else) 4. A way to store our amount of frequency for each number (variables) 5. A way to output our findings to the screen (cout)
I hope I didn't give too much away - from here it is up to you to form the exact specification of your logic throughout the program (ie, what variables to define, how to execute your loop, what if/else to use, and how and what to output).

- 1
- 1