You are using a version without Ads of this website. Please, consider donating:

### Storing random numbers into an array

I'm trying to figure out how I can store a list of random generated numbers into an array and then output the total number of times each integer appears.
For example:
 ```int 1 has appeared 5 times. ***** int 2 has appeared 4 times. **** int 3 has appeared 4 times. **** int 4 has appeared 3 times. *** int 5 has appeared 4 times. ****```

Now the tricky part for me is the comparison between the numbers that were randomly output and the total number of instances of each integer. As simple as it may be to some, I can't figure out the logic.

Here's what I have so far, with errors (mostly logic based).
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243`` ``````#include #include #include using namespace std; const int RND_ARR_SIZE=20; //named constant for the size of array randNumArr const int CNT_ARR_SIZE=6; //named constant for the size of array countArr int main () { int randNumArr[RND_ARR_SIZE]; int countArr[CNT_ARR_SIZE]; srand (time(0)); //Set the seed for generating random numbers. cout << "The randomly generated integers are: \n"; for (int i=0; i

 ```random_num_example.cpp: In function ‘int main()’: random_num_example.cpp:32:25: error: ISO C++ forbids comparison between pointer and integer [-fpermissive] random_num_example.cpp:34:25: error: ISO C++ forbids comparison between pointer and integer [-fpermissive] random_num_example.cpp:36:25: error: ISO C++ forbids comparison between pointer and integer [-fpermissive] random_num_example.cpp:38:25: error: ISO C++ forbids comparison between pointer and integer [-fpermissive] random_num_example.cpp:40:25: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]```

This is how far I got and now I'm completely stumped. Guidance in the correct direction is greatly appreciated!
Have a look at std::map<>. This will allow you to store the random number as a key and the number of times it's called as the value.

Your issue is that randNumArr is a pointer to the address to the first number in the array. If you want to figure out how many times it appears you have to iterate through the array and count (alternatively just use a map).
Unfortunately, I don't believe I can used std::map<>, it's not outlined in my lab description nor do I recall learning it. In my first for loop, are the random numbers even being stored in the array randNumArr?
Yes.
What steps you need to do.
1. Set all values in countArr to 0
2. Increment the value by 1 when a number is randomly generated
3. Print from countArr
You're referring to the second loop, correct?
A loop that does not yet exist.

But consider
 ``12`` ``````randNumArr[i] = rand() % (CNT_ARR_SIZE); countArr[randNumArr[i]]++;``````

:)
Last edited on
I'll give it a shot based off of what you've guided me toward so far. I'm still a bit stuck but I'll try to refresh my memory later on today using my textbook and notes.
Thanks for the help, I was able to figure it out using three for loops and in the third loop it contains a nested for loop to handle the output of asterisks that are associated with the number of instances each int is generated.
Topic archived. No new replies allowed.

You are using a version without Ads of this website. Please, consider donating: