I need to be able to write a program using arrays that produces 100 integers that are NOT outputted and the it produces a count of how many times a number shows up. The numbers range from 0-9 and the output should look something like this:
Press enter to fill an array with 100 integers, 0-9:
0 occurs 10 times
1 occurs 4 times
2 occurs 7 times..... etc. Of course the number of times it occurs will vary because the numbers are randomly generated. I am stuck at this point and don't know where to go from here. Please help!
#include <iostream>
usingnamespace std;
int main()
{
int i=0;
int num1;
cout<<"Press Enter to fill an array with 100 integers, 0-9\n";
int num[100]= {'1','2','3','4','5','6','7','8','9'};
for (i=0; i<=99; i++)
cout<<num[rand () % 10];
cout<<"\n\n";
system ("pause");
}
Line 10: Why are you initializing this array with '0' - '9' ?
You need another array initialized to zeroes that you're going to use to count the number of occurrances of 0 - 9.
Line 15: This doesn't do what you want. Here you want to increment your counter array based on the random number generated.
You need another loop to print out your counter array.
You also need to include <cstdlib> if you're going to use rand().
You should call srand(time(NULL)); at the beginning of your program to initialize the random number generator, otherwise you're going to get the same sequence of random numbers every time you run your program.