In a main function declare an array of 1000 ints. Fill up the array with random
ID: 3623323 • Letter: I
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 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. This is called a frequency array.
Print out how many times each value appears in the array.
1 occurs XXX times
2 occurs XXX times
Hint: If you find yourself repeating the same line of code you need to use a loop. If you declare several variables that are almost the same, maybe you need to use an array. count1, count2, count3, count4, … is wrong. Make an array and use the elements of the array as counters. Output the results using a loop with one printf statement. This gets more important when we are rolling 2 dice.
Part 5
Starting from scratch, repeat Part 4, with 2 dice. Let’s use 10,000 this time instead of 1000.
Each die can be from 1 - 6, so the total of 2 dice must be in the range 2 – 12.
Your output will be in the range of 2 – 12.
Notice that it is much less likely to roll a 2 than it is to roll a 7, since a 2 can only be rolled a 1 + 1. But a 7 can be 6 + 1, 5 + 2, 4 + 3, 3 + 4, 2 + 5, 1 + 6.
Count how many times each value occurs and output that count as in the previous part.
Calculate what percentage of the time you roll a 2, 3, 4, etc. up to 12. Output the percentages with an accuracy of one place past the decimal. Post the results of this one on the discussion board.
Part 6:
Repeat part 5 using any number of dice. (well not an infinite number)
That means that the values rolled will range between DICE (all 1’s) – DICE * 6 (all 6’s).
Output the (DICE*6) – (DICE – 1) different percentages.
Suppose we choose 5 dice, but it must work for any number.
#define DICE 5
The lowest number we can roll is 5, and the largest is 30
That is 26 different values. 5 => 30, or (5 * 6) – (5 – 1)
Demonstrate that your program works for one die, 5 dice, and 10 dice.
Explanation / Answer
please rate - thanks
I think it's finished. message me if it needs any changes
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define DICE 5
#define ROLLS 10000
int main()
{srand(time(0));
int die,i;
int amount=DICE*6-(DICE-1);
int *counts=malloc(sizeof(int)*amount);
for(i=0;i<amount;i++)
counts[i]=0;
for(i=0;i<ROLLS;i++)
{die=rand()%amount;
counts[die]++;
}
printf("number rolled percent ");
for(i=0;i<amount;i++)
printf("%d %d %.1f ",i+DICE,counts[i],(counts[i]*100.)/ROLLS);
getch();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.