In a main function declare an array of 1000 ints. Fill up the array with random
ID: 3623271 • 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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define DICE 6 // this indicate no of dice change here to any number whateer u want.
int main(){
int arrayof10000[10000], arrayof12[37] = {0}, index, i, numberofTimes = 1;
double percentage;
srand(time(NULL));//seeding random number gen
//initializing array with random die numbers
for(index = 0; index < 10000; index++)
arrayof10000[index] = (rand() % 6 + 1) + (rand() % 6 + 1) +
(rand() % 6 + 1) + (rand() % 6 + 1) + (rand() % 6 + 1) + (rand() % 6 + 1);
//if counting number of times for each index number
//1 - 6 using 7 elements beginning at 1
for(i = DICE*1; i <= DICE*6; i++){
numberofTimes = 1;
for(index = 0; index < 10000; index++){
if(i == arrayof10000[index])
arrayof12[i] = numberofTimes++;
}
percentage = ((double)arrayof12[i] / 10000) * 100;
printf("%i occurs %i times at %.1lf percent ", i, arrayof12[i], percentage);
}
// system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.