Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This program will store 500 dice throws (ie random numbers between 1 and 6) in a

ID: 3758212 • Letter: T

Question

This program will store 500 dice throws (ie random numbers between 1 and 6) in an array via the function called generateData.
Then in a for-loop it will call the function called countValue 6 times where countValue will scan the 500 array positions. The first call it will tally how many "1" throws there were, the second call, how many 2 throws..etc until reaching 6

1 - The function generateData will receive the data array as alias "d" and a 500 limit as alias "cnt".

     It will compute a random number between 1 and 6 and store it in the dice variable.
     Then it will move that value into d[i]...i is 1, then 2, then 3..etc .
     So each loop iteration causes the next dice throw to be stored in the next available position of array d.

2 - The function countValue will receive the data array, the 500 limit and the current value of the for-loop "i" variable as alias values d, howmany and value, respectively.

3 - Inside function countValue...use an if statement to see if the ith position of array d is equal to its alias "value"

     ...adding one to total if that is so

Explanation / Answer

// Complete the following program
// The program generates 500 numbers between 1 and 6 and stores them in an array
// The program loops and counts how many times each number occurs

#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace::std;

void generateData(int [] , int count);

int countValue(const int [], int howMany, int value);

int main()
{
int data[1000];
  
generateData(data,500); // call the function to generate 500 numbers between 1 and 6
// storing the values in the data array
  
for ( int i = 1 ; i <= 6 ; i++ )
{
cout << i << " " << countValue(data,500,i) << endl; // call the countValue function
}
return 0;
}

void generateData(int d[], int cnt)
{
int dice;
for(int i = 0; i < cnt ; i++)
{
dice = rand()%6 + 1 ; // generate a random number between 1 and 6
d[i] = dice; // store it in the next position
}
}

int countValue(const int d[], int howMany, int value)
{
int total = 0;
for( int i = 0; i < howMany ; i++)
{
if ( d[i]== value ) // adjust the total if number in cell i is equal to the parameter "value"
   total++;
}
return total;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote