### Stuck on C++ problem

I've been currently stuck on a C++ problem for about an hour and half. Here's the question:

Write a program that generates one hundred random integers between 0 and 9 and displays the count for each number. (Hint: Use rand()
% 10 to generate a random integer between 0 and 9. Use an array of ten integers,
say counts, to store the counts for the number of O's, l 's, . .. , 9's.)

And here's what I have so far. I think I'm pretty close, but I keep on getting "0" for the occurrences (or counts) of each random integer. Any help would be greatly appreciated.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134`` ``````#include #include #include #include #include using namespace std; const int SIZE = 100; int main() { int integers[SIZE]; int index; int zero = 0; int one = 0; int two = 0; int three = 0; int four = 0; int five = 0; int six = 0; int seven = 0; int eight = 0; int nine = 0; cout << "The following 100 integers are random:" << endl; cout << endl; srand(time(0)); for (index = 0; index < SIZE; index++) { integers[SIZE] = rand() % 10; cout << integers[SIZE] << " "; } cout << endl; for (index = 0; index < SIZE; index++) { if (integers[index] == 0) { zero += 1; } } for (index = 0; index < SIZE; index++) { if (integers[index] == 1) { one += 1; } } for (index = 0; index < SIZE; index++) { if (integers[index] == 2) { two += 1; } } for (index = 0; index < SIZE; index++) { if (integers[index] == 3) { three += 1; } } for (index = 0; index < SIZE; index++) { if (integers[index] == 4) { four += 1; } } for (index = 0; index < SIZE; index++) { if (integers[index] == 5) { five += 1; } } for (index = 0; index < SIZE; index++) { if (integers[index] == 6) { six += 1; } } for (index = 0; index < SIZE; index++) { if (integers[index] == 7) { seven += 1; } } for (index = 0; index < SIZE; index++) { if (integers[index] == 8) { eight += 1; } } for (index = 0; index < SIZE; index++) { if (integers[index] == 9) { nine += 1; } } cout << "The number of zeros in the random list are " << zero << endl; cout << "The number of ones in the random list are " << one << endl; cout << "The number of twos in the random list are " << two << endl; cout << "The number of threes in the random list are " << three << endl; cout << "The number of fours in the random list are " << four << endl; cout << "The number of fives in the random list are " << five << endl; cout << "The number of sixes in the random list are " << six << endl; cout << "The number of sevens in the random list are " << seven << endl; cout << "The number of eights in the random list are " << eight << endl; cout << "The number of nines in the random list are " << nine << endl; getch(); return 0; }``````
in your assignment there is said very clear "Use an array of ten integers"
And in any case this code is invalid

 ``12345`` `````` for (index = 0; index < SIZE; index++) { integers[SIZE] = rand() % 10; cout << integers[SIZE] << " "; }``````

Last edited on

Let me suggest that when you having a tonne of if statements often means your code is doing something repetitive that you should code differently (with fewer lines).

 ``123456789101112131415161718192021222324252627`` ``````#include #include #include #include int main() { std::multiset s; enum {SIZE = 100, LIMIT = 10}; int array[SIZE]; std::srand(std::time(nullptr)); for (int i = 0; i < SIZE; ++i) { array[i] = std::rand() % LIMIT; s.insert(array[i]); } for (int i = 0; i < LIMIT; ++i) std::cout << i << " occurs " << s.count(i) << " times\n"; return 0; }``````

Array of 10 ints here.

 ``12345678910111213141516171819202122232425`` ``````#include #include int main() { enum {SIZE = 100, LIMIT = 10}; int array[LIMIT]; for (int i = 0; i < LIMIT; ++i) array[i] = 0; std::srand(std::time(nullptr)); for (int i = 0; i < SIZE; ++i) { int temp = std::rand() % LIMIT; array[temp] += 1; } for (int i = 0; i < LIMIT; ++i) std::cout << i << " occurs " << array[i] << " times\n"; return 0; }``````
Last edited on
 ``12`` ``````// int array[LIMIT]; int array[LIMIT] = {0} ;``````

 ``12345678910111213141516171819202122`` ``````#include #include #include int main() { enum { SIZE = 100, LIMIT = 10 }; int counts[LIMIT] = {}; std::srand( ( unsigned int )std::time( nullptr ) ); for ( int i = 0; i < SIZE; ++i ) ++counts[ std::rand() % LIMIT ]; for ( int i : counts ) { std::cout << i << " occurs " << counts[i] << " times\n"; } return 0; }``````