BELOW YOU WILL SEE A CODE WITH COMMENTS IN IT. PLEASE FILL IN THE \"TASKS\" 1-8
ID: 3565301 • Letter: B
Question
BELOW YOU WILL SEE A CODE WITH COMMENTS IN IT. PLEASE FILL IN THE "TASKS" 1-8 INDICATED BY THE COMMENTS. #include <iostream> #include <cmath> #include <ctime> using namespace std; int myRandNumGen(){ // insert your code here int num = rand(); return num; } int diceRoll(){ // insert your code here int num = (myRandNumGen()%6)+1; return num; } char coinTossFunction( ){ // insert your code here char coinToss; coinTossValue = (myRandNumGen()%2); // 0 or 1 switch (coinTossValue) { case 0: coinToss = 'H'; break; case 1: coinToss = 'T'; break; default: break; } return coinToss; } double calcAverageDice(int n){ // n = number of dice rolls // Task 1: insert your code here } double calcAverageCoin(int n){ // n = number of coin tosses // Task 2: insert your code here return (cout_heads/n); } // Task 5: insert your myGame function here int main () { // insert code here... // srand should be called once srand(time(0)); // Call the function to calculate average. Roll the dice n times. float avgDice = calcAverageDice(100); // Display the average number as seen from 100 rolls // Task 3: insert your code here // Call the function to toss a coin n times // Is your coin biased? What would you bet on? // Task 4. insert your code here if (val>0.5) { cout << "biased towards heads"; } // Assume we are developing a multi-player game. // Each player gets n chances. // Each chance: The player tosses a coin. // If he rolls a head, he gets to roll the dice. // If he rolls a tail, he forfeits the chance and earns a zero. // Add up all such numbers // write a function called myGame to implement this game. // input arguments: n = number of chances // output/return arguments: total = from summation of dice rolls. // // Now call your function for 2-players. // Calculate player 1's total score // Task 6: insert your code here // Calcualte player 2's total score // Task 7: insert your code here // The highest score wins. Display a message to the winner. // Task 8: insert your code here return 0; }
Explanation / Answer
#include <iostream>
#include <cmath>
#include <ctime>
using namespace std;
int myRandNumGen(){
// insert your code here
int num = rand();
return num;
}
int diceRoll(){
// insert your code here
int num = (myRandNumGen()%6)+1;
return num;
}
char coinTossFunction( ){
// insert your code here
char coinToss;
coinTossValue = (myRandNumGen()%2); // 0 or 1
switch (coinTossValue) {
case 0:
coinToss = 'H';
break;
case 1:
coinToss = 'T';
break;
default:
break;
}
return coinToss;
}
double calcAverageDice(int n){
// n = number of dice rolls
// Task 1: insert your code here
int sum = 0;
double avgDice;
for(int i=1;i<=n;i++)
sum+=diceRoll();
avgDice = (double) sum/n;
return avgDice;
}
double calcAverageCoin(int n){
// n = number of coin tosses
// Task 2: insert your code here
int cout_heads=0;
for(int i=0;i<=n;i++){
if(coinTossFunction() == 'H')
++cout_heads;
}
return (cout_heads/n);
}
// Task 5: insert your myGame function here
int myGame(int n){
int score=0;
for(int i=1;i<=n;i++){
if(coinTossFunction() == 'H')
score+=diceRoll();
}
return score;
}
int main () {
// insert code here...
// srand should be called once
srand(time(0));
// Call the function to calculate average. Roll the dice n times.
float avgDice = calcAverageDice(100);
// Display the average number as seen from 100 rolls
// Task 3: insert your code here
cout<<"Average number seen from 100 rolls is : "<<avgDice<<endl;
// Call the function to toss a coin n times
// Is your coin biased? What would you bet on?
// Task 4. insert your code here
int n;
cout<<"Enter no of times to toss the coin : ";
cin >> n;
double val = calcAverageCoin(n);
if (val>0.5) {
cout << "biased towards heads";
}
// Assume we are developing a multi-player game.
// Each player gets n chances.
// Each chance: The player tosses a coin.
// If he rolls a head, he gets to roll the dice.
// If he rolls a tail, he forfeits the chance and earns a zero.
// Add up all such numbers
// write a function called myGame to implement this game.
// input arguments: n = number of chances
// output/return arguments: total = from summation of dice rolls.
//
// Now call your function for 2-players.
// Calculate player 1's total score
// Task 6: insert your code here
cout<"Enter number of chances provided to each player : ";
cin >> n;
int play_one = myGame(n);
// Calcualte player 2's total score
// Task 7: insert your code here
int play_two = myGame(n);
// The highest score wins. Display a message to the winner.
// Task 8: insert your code here
if(play_one > play_two)
cout<<"Player 1 wins the game with total score of "<<play_one<<endl;
if(play_one < play_two)
cout<<"Player 2 wins the game with total score of "<<play_two<<endl;
else
cout<<"The game ended in a draw."<<endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.