counting iteration of a number within an array

Dec 10, 2015 at 8:44pm
I have been trying to get this to function. I however seem to be re displaying the array rather than it given me a count of each number. so for instance the array is populated by 1,2, 2,4,7,9, 9, 9, 11, 12 , 13, 13 , 15, 16, 16 , 16, 16, 19, 20

it will display
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
       
        |     Number     |     Count     |
        ----------------------------------
        |      20        |      20       |
        |      19        |      19       |
        |      18        |      16       |
        |      17        |      16       |
        |      16        |      16       |
        |      15        |      16       |
        |      14        |      15       |
        |      13        |      13       |
        |      12        |      13       |
        |      11        |      12       |
        |      10        |      11       |
        |       9        |      13       |
        |       8        |       9       |
        |       7        |       9       |
        |       6        |       9       |
        |       5        |       7       |
        |       4        |       4       |
        |       3        |       2       |
        |       2        |       2       |
        |       1        |       1       |
        ----------------------------------

What it should display however is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
      
        |     Number     |     Count     |
        ----------------------------------
        |      20        |       1       |
        |      19        |       1       |
        |      18        |       0       |
        |      17        |       0       |
        |      16        |       4       |
        |      15        |       1       |
        |      14        |       0       |
        |      13        |       2       |
        |      12        |       1       |
        |      11        |       1       |
        |      10        |       0       |
        |       9        |       3       |
        |       8        |       0       |
        |       7        |       1       |
        |       6        |       0       |
        |       5        |       0       |
        |       4        |       1       |
        |       3        |       0       |
        |       2        |       2       |
        |       1        |       1       |
        ----------------------------------

1
2
3
4
5
6
  void display_frequency(const int number_list[])
{
    for(int i=20;i > 0; i--)
        cout << "\t" << "|"<< setw(8) << i << setw(9)<< "|" << setw(8)
			 << number_list[i]<< setw(8) << "|" << endl;
 }


my random generator to populate the array is:
1
2
3
4
5
6
7
void load_array(int number_list[])//populate the array
{
	int x=0;
	srand ((unsigned)time(0));
	for(int x = 0; x < SIZE; x++)
		number_list[x]=(rand() % 20+1); // 1-20
}


I would like to point out I am not using classes or anything to that effect. this is rather simple in practice I am just missing something
Last edited on Dec 10, 2015 at 8:47pm
Dec 10, 2015 at 9:14pm
The problem is that you're loading the array differently then you want to. What you are doing now is loading each element with it's own value of 1-20. What you want to do is increment the element when the number is rolled.

So, instead of
1
2
for(int x = 0; x < SIZE; x++)
    number_list[x]=(rand() % 20+1); // 1-20 

you should do something like
1
2
3
4
for(int x = 0; x < SIZE; x++) {
    int randNum = rand() % 20+1); // 1-20
    ++number_list[randNum - 1];
}
Dec 11, 2015 at 12:33am
Is your Number array initialized from 0-20?

I don't see anything wrong with your load_array function, except you need to pass it by reference

Also, how are you checking it? You would need to use a nested for loop, where the first loop will hold the first index of the random array, then check 0-20 of the number array and see which matches, if it matches, you simply increment the number array index
Dec 11, 2015 at 12:33am
Also it should be number_list[x] = rand()%20+1; idk if the parenthesis around the whole thing is necessary or even correct
Topic archived. No new replies allowed.