Write a program that prompts the user for a random seed followed by a second pro
ID: 3778327 • Letter: W
Question
Write a program that prompts the user for a random seed followed by a second prompt asking for the number of times a pair of dice are to be rolled. The program should generate the appropriate random numbers (between 1 - 6), sum the results, and store the sum in a variable length array. The output should print the number of times the sum of the two dice is odd or even, and the percentage of times the sum is odd or even.
Note: After prompting the seed value, use it for the random number generator. Then prompt the user to enter the number of times a pair of dices are to be rolled, and store the result in n. Then create a variable length array called numbers[] whose size is equal to n. Next call the rand() function to simulate rolling 2 dice (each producing a number between 1-6). Sum the result, and store each value in an element of the variable length array. Then determine how many times the sum of the two dice is odd or even, and the percentage of time the sum is odd or even. Print the result to the screen, and include one value after the decimal point for the percentages.
Use following variables:
seed-store random seed (unsigned integer), i-counter variable (int), n-stores the number of elements of the array (int), odd-stores the number of times the sum of the two dice are odd (int),even-stores the number of times the sum of the two dice are even (int), numbers[] - name of the variable length array (int).
Also, can you explain why we need to ask for a random 'seed' to do this problem?
Example of output/input:
Output: "Enter a random seed:" Input: 10
Output: "Enter number of random integers to generate:" Input: 100
Output: "Number of times sum is odd: 48"
Output: "Number of times sum is even: 52"
Output: "Percentage of times sum is odd: 48.0%"
Output: "Percentage of times sum is even: 52.0%"
Explanation / Answer
Answer:
//include the needed header files
#include<iostream>
#include<string>
#include<cstdlib>
#include<cmath>
using namespace std;
//main
int main()
{
//declare the needed variables
int seed;
int n;
int i=0;
int odd=0;
int even=0;
int *numbers;
cout<<" Enter seed value:";
cin>>seed;
cout<<" Enter number of random integers to generate:";
cin>>n;
srand(seed);
numbers=new int[n];
for(i=0;i<n;i++)
{
int d1=rand()%6+1;
int d2=rand()%6+1;
int sm=d1+d2;
numbers[i]=sm;
if(sm%2==0)
even++;
else
odd++;
}
cout<<endl<<"Number of times sum is odd:"<<odd<<endl;
cout<< "Number of times sum is even: "<<even<<endl;
cout<<"Percentage of times sum is odd: "<<(float)(odd)/(float)(n)*100<<"%"<<endl;
cout<<"Percentage of times sum is even: "<<(float)(even)/(float)(n)*100<<"%"<<endl;
return 0;
}
Sample output:
Enter seed value:10
Enter number of random integers to generate:50
Number of times sum is odd:21
Number of times sum is even: 29
Percentage of times sum is odd: 42%
Percentage of times sum is even: 58%
The use of seed creates dissimilar random-numbers for each successive call
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.