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

I dont understand this hw assignment please help guys :) ECE 71 – Engineering Co

ID: 674810 • Letter: I

Question

I dont understand this hw assignment please help guys :)

ECE 71 – Engineering Computations in C

Professor Kriehn
Code Due By: Midnight on Tuesday, October 27, 2015

Writeup Due By: Class on Thursday, October 29, 2015 HOMEWORK #21 – Game of Craps

Write a program that simulates the game of craps, which is played with two dice. On the first roll, the player wins if the sum of the dice is 7 or 11. The player loses if the sum is 2, 3, or 12. Any other roll is called the “point”, and the game continues. On each subsequent roll, the player wins if he or she rolls the point again. The player loses by rolling a 7. Any other roll is ignored and the game continues. At the end of the each game, the program will ask the user whether or not to play again. When the user enters a response other than ‘y’ or ‘Y’, the program will display the number of wins and losses and then terminate.

Specifications:

Write the program using three functions: main, roll_dice, and play_game. Use the following function prototypes:

int roll_dice(void); bool play_game(void);

roll_dice should generate two random numbers, each between 1 and 6, and return their sum. Use the rand function to generate random numbers.

play_game should play one craps game (calling roll_dice as many times as necessary to determine the outcome of each dice roll); it will return TRUE if the player wins and FALSE if the player loses. play_game is also responsible for displaying messages showing the results of the player’s dice rolls.

main will call play_game repeatedly, keeping track of the number of wins and losses, and displaying the “You win!” and “You lose!” messages. At the beginning main, after displaying a “Welcome to Craps” message, you should prompt the user to enter a random seed (to satisfy the grader program).

Finally, use the exit function and the EXIT_SUCCESS constant to exit your program (which are both defined in ). From this point forward, each of your programs should use the exit function to terminate.

If you execute the program, the following information should be displayed:

~> hw21.o
Welcome to Craps
Please enter a random seed: 23 You rolled: 4
Your point is: 4

[page1image20416] [page1image20576]
You rolled: 8 You rolled: 8 You rolled: 5 You rolled: 6 You rolled: 11 You rolled: 7 You lose!

Play again?: n Wins: 0 Losses: 1

~>

~> hw21.o
Welcome to Craps
Please enter a random seed: 63 You rolled: 7
You win!
Play again? y
You rolled: 7
You win!
Play again? y
You rolled: 10
Your point is: 10
You rolled: 4
You rolled: 7
You lose!
Play again? n

Wins: 2 Losses: 1 ~>

~> hw21.o
Welcome to Craps
Please enter a random seed: 152 You rolled: 5
Your point is: 5
You rolled: 3
You rolled: 5
You win!
Play again?: q

Wins: 1 Losses: 0

~>

Explanation / Answer

#include <iostream>
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
#include <stdbool.h>
using namespace std;

int roll_dice(void);
bool play_game(void);

int main()
{
   boolean b=play_game();
   if(b==true)
   exit(0);
   return 0;
}
//roll_dice should generate two random numbers, each between 1 and 6,
// and return their sum. Use the rand function to generate random numbers.
int roll_dice(void)
{
   int num1, num2;
   /* initialize random seed: */
srand (time(NULL));

/* generate secret number between 1 and 10: */
num1 = rand() % 1+ 6;
num2 = rand() % 1+ 6;

return num1+num2;
  
}
/*play_game should play one craps game (calling roll_dice as many times
as necessary to determine the outcome of each dice roll); it will return
TRUE if the player wins and FALSE if the player loses.
play_game is also responsible for displaying messages showing the results
of the player’s dice rolls.*/
bool play_game(void)
{
   int sum=0;
   int point=0;
   boolean b=false;
   while(sum!=7 || sum!=11)
   {
       sum=roll_dice();
       if(sum==7 || sum==11)  
       {
           cout<<" You win!";
           b=true;
           break;
       }
       else
       {
           point+=sum;
           cout<<" You rolled"<<sum;
       }
   }
  
}