Create a program in C++, which rolls two die and keeps track of the frequency of
ID: 3668669 • Letter: C
Question
Create a program in C++, which rolls two die and keeps track of the frequency of each possible roll combination. Note that dice1 can have rolls of 1..6 and dice2 can have rolls of 1..6. In order to store the frequency of each possible roll combination, a two dimensional array must be used. Please make sure to follow the example provided in class which maintains the frequency of each possible roll (1..6) when developing your solution.
Use the code below for this assignment.
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
void printArray(int array[], int size);
void arrayToZero(int array[], int size);
void rollDie(int array[], int timesRolled);
int main(int argc, char *argv[]) {
int array[12];
int dieArray[6];
int size = 6;
// printArray(array);
// arrayToZero(array);
// printArray(array);
printArray(dieArray, 6);
arrayToZero(dieArray, 6);
printArray(dieArray, 6);
rollDie(dieArray, 10);
printArray(dieArray, 6);
return 0;
}
void printArray(int array[], int size){
int index = 0;
for(index = 0; index < size; index++){
printf("value of array[%d] is %d ", index, array[index]);
}
}
void arrayToZero(int array[], int size){
int index = 0;
for(index = 0; index < 12; index++){
array[index] = 0;
}
}
void rollDie(int array[], int timesRolled){
int rolls = 0;
for(rolls = 0; rolls < timesRolled; rolls++){
int sideRolled = rand() % 6;
printf("%d ", sideRolled);
array[sideRolled]++;
}
}
Explanation / Answer
//Program
#include <stdio.h>
#include <stdlib.h>
#define size 6
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
void printArray(int array[][size]);
void arrayToZero(int array[][size]);
void rollDie(int array[][size], int timesRolled);
int main() {
int dieArray[size][size];
//printArray(dieArray);
arrayToZero(dieArray);
//printArray(dieArray);
rollDie(dieArray, 10);
printArray(dieArray);
return 0;
}
void printArray(int array[][size]){
int index1 ,index2;
for(index1 = 0; index1 < size; index1++)
{
for(index2=0;index2<size;index2++)
{
printf("[%d][%d] is %d ", index1,index2,array[index1][index2]);
}
}
}
void arrayToZero(int array[][size]){
int index1 ,index2;
for(index1 = 0; index1 < size; index1++){
for(index2=0;index2<size;index2++)
{
array[index1][index2]= 0;
}
}
}
void rollDie(int array[][size], int timesRolled){
int rolls = 0;
for(rolls = 0; rolls < timesRolled; rolls++){
int sideRolled1= rand() % 6;
int sideRolled2= rand() % 6;
array[sideRolled1][sideRolled2]++;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.