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).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <cstdlib>
#include <ctime>

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<RND_ARR_SIZE; i++)
	{
		randNumArr[i] = rand() % (CNT_ARR_SIZE);
		//generate a random integer that is between 0 to 5.

		cout << randNumArr[i] <<"  ";
	}
	
	cout<<endl;
	
	if (randNumArr == 0)
		cout << "There are " << countArr[0] << "instances of integer " << randNumArr[0] << endl;
	else if (randNumArr == 1)
		cout << "There are " << countArr[1] << "instances of integer " << randNumArr[1] << endl;
	else if (randNumArr == 2)
		cout << "There are " << countArr[2] << "instances of integer " << randNumArr[2] << endl;
	else if (randNumArr == 3)
		cout << "There are " << countArr[3] << "instances of integer " << randNumArr[3] << endl;
	else if (randNumArr == 4)
		cout << "There are " << countArr[4] << "instances of integer " << randNumArr[4] << endl;
	else if (randNumArr == 5)
		cout << "There are " << countArr[5] << "instances of integer " << randNumArr[5] << endl;
		
	return 0;
}


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
1
2
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.