Question
In a main function declare an array of 1000 ints. Fill up the array with random numbers that represent the rolls of a die. That
In a main function declare an array of 1000 ints.
Fill up the array with random numbers that represent the rolls of a die. That means values from 1 to 6.
Write a loop that will count how many times each of the values appears in the array of 1000 die rolls.
Use an array of 6 elements to keep track of the counts, as opposed to 6 individual variables.
Print out how many times each value appears in the array.
1 occurs XXX times
2 occurs XXX times
my code
#include
#include
#include
int main()
{
int array[1000];
int x;
int sum[6];
srand(time(NULL));
for(x=0; x<1000; x++){
array[x]= (int)(rand()%6+1);
sum[array[x]]=(sum[array[x]]++);
}
for(x=1; x<7; x++){
printf("%d occured %d times. ", x , sum[6]);
}
return 0;
}
output
The dice rolled a 1, 1384960 times. The dice rolled a 2, 1384960 times. The dice rolled a 3, 1384960 times. The dice rolled a 4, 1384960 times. The dice rolled a 5, 1384960 times. The dice rolled a 6, 1384960 times.
i need help fixing this error
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started