Question

Exercise 1: A Game of guessing number Set the default upper and lower limits to be 1-100, and ask you to guess a number. If y

in c programming and c++filed

0 0
Add a comment Improve this question Transcribed image text
Answer #1

EXERCISE 1

#include <stdio.h>
#include <time.h>

int main(void)
{
   int randNum, guessNum;
   int lower = 1, upper = 100;
  
   srand(time(NULL));
   randNum = rand() % 100 + 1;
  
   while(1) {
       printf("Enter guess betweem %d ~ %d: ", lower, upper);
       scanf("%d", &guessNum);
      
       if(guessNum == randNum) {
           printf("\n\nCorrect guess.\n");
           break;
       }
       else if(guessNum > randNum)
           upper = guessNum;
       else
           lower = guessNum;
       printf("Not correct. try again...\n");
   }
  
   return 0;
}

OUTPUT:

Enter guess betweem 1 - 100: 50 Not correct. try again... Enter guess betweem 50 - 100: 56 Not correct. try again... Enter gu

EXERCISE 2

#include <iostream>
/* Writ statement to include file stream header */
#include <cstdlib>
#include <ctime>

using std::cout;
using std::cerr;
using std::cin;
using std::ios;
using std::endl;
/* Write appropriate using statement(s) */

void playCraps();
void reviewStatistics();
int rollDice();

int main()
{
int choice;

// continue game unless user chooses to quit
do {

// offer game options
cout << "Choose an option" << endl
<< "1. Play a game of craps" << endl
<< "2. Review cumulative craps statistics" << endl
<< "3. Quit program" << endl;

cin >> choice;
  
if ( choice == 1 )
playCraps();
else if ( choice == 2 )
reviewStatistics();

} while ( choice != 3 );

return 0;

} // end main

// review cumulative craps statistics
void reviewStatistics()
{
/* Write a body for reviewStatistics which displays
the total number of wins, losses and die rolls recorded
in craps.dat */

} // end function reviewStatistics

// play game
void playCraps()
{
enum Status { CONTINUE, WON, LOST };
int sum;
int myPoint;
int rollCount = 0;
Status gameStatus;

/* Write statement to create an output file stream */

// seed random number generator and roll dice
srand( time( 0 ) );
sum = rollDice();
rollCount++;

// check game conditions
switch( sum ) {
case 7:
case 11:
gameStatus = WON;
break;
case 2:
case 3:
case 12:
gameStatus = LOST;
break;
default:
gameStatus = CONTINUE;
myPoint = sum;
cout << "Point is " << myPoint << endl;
break;

} // end switch

// keep rolling until player matches point or loses
while ( gameStatus == CONTINUE ) {
sum = rollDice();
rollCount++;
  
if ( sum == myPoint )
gameStatus = WON;

else
if ( sum == 7 )
gameStatus = LOST;

} // end while

// display status message and write results to file
if ( gameStatus == WON ) {
cout << "Player wins\n" << endl;
/* Write player WIN status and the total number of die
rolls to a file */

} // end if
else {
cout << "Player loses\n" << endl;
/* Write player LOSE status and the total number of die
rolls to a file */

} // end else

} // end function playCraps

// dice rolling function
int rollDice()
{
int die1;
int die2;
int workSum;

// roll two dice
die1 = 1 + rand() % 6;
die2 = 1 + rand() % 6;

// total and print results
workSum = die1 + die2;
cout << "Player rolled " << die1 << " + " << die2 << " = " << workSum << endl;
  
return workSum;

} // end function rollDice

Add a comment
Know the answer?
Add Answer to:
in c programming and c++filed Exercise 1: A Game of guessing number Set the default upper...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C# Code: Write the code that simulates the gambling game of craps. To play the game,...

    C# Code: Write the code that simulates the gambling game of craps. To play the game, a player rolls a pair of dice (2 die). After the dice come to rest, the sum of the faces of the 2 die is calculated. If the sum is 7 or 11 on the first throw, the player wins and the game is over. If the sum is 2, 3, or 12 on the first throw, the player loses and the game is...

  • III. Overview & Requirements: The following description has been adopted from Deitel & Deitel. One of...

    III. Overview & Requirements: The following description has been adopted from Deitel & Deitel. One of the most popular games of chance is a dice game called "craps," which is played in casinos and back alleys throughout the world. The rules of the game are straightforward: A player rolls two dice. Each die has six faces. These faces contain 1, 2, 3, 4, 5, and 6 spots. After the dice have come to rest, the sum of the spots on...

  • 9. In the casino dice game Craps, players make wagers on a sequence of rolls of...

    9. In the casino dice game Craps, players make wagers on a sequence of rolls of a pair of dice. A sequence of rolls starts with the "shooter" making an initial roll of two dice called the "come-out” roll. If the sum of the dice on the initial roll is 7 or 11 then a player with a bet on the Pass Line wins. If the initial roll results in a sum of 2, 3, or 12 ("craps") then a...

  • 2. "Craps" is a game played by rolling two fair dice. To play one round of...

    2. "Craps" is a game played by rolling two fair dice. To play one round of this game, the player rolls the dice and the outcome is determined by the following rules: If the total number of dots is 7 or 11 (a "natural"), then the player wins. If the total number of dots is 2, 3, or 12 C'craps"), then the player loses. If the total number of dots is 4, 5, 6,8,9, or 10, then this number is...

  • Java programming Write a simulation of the Craps dice game. Craps is a dice game that...

    Java programming Write a simulation of the Craps dice game. Craps is a dice game that revolves around rolling two six-sided dice in an attempt to roll a particular number. Wins and losses are determined by rolling the dice. This assignment gives practice for: printing, loops, variables, if-statements or switch statements, generating random numbers, methods, and classes. Craps game rules: First roll: The first roll (“come-out roll”) wins if the total is a 7 or 11. The first roll loses...

  • Craps

    Write a C++ game that plays Craps. Craps is a game played with a pair of dice. The shooter (the player with the dice) rolls a pair of dice and the number of spots showing on the two upward faces are added up. If the opening roll (called the ‘come out roll’) is a 7 or 11, the shooter wins the game. If the opening roll results in a 2 (snake eyes), 3 or 12 (box cars), the shooter loses,...

  • Please i need helpe with this JAVA code. Write a Java program simulates the dice game...

    Please i need helpe with this JAVA code. Write a Java program simulates the dice game called GAMECrap. For this project, assume that the game is being played between two players, and that the rules are as follows: Problem Statement One of the players goes first. That player announces the size of the bet, and rolls the dice. If the player rolls a 7 or 11, it is called a natural. The player who rolled the dice wins. 2, 3...

  • in C++ This assignment is to use your knowledge of Chap 1-5 to write a simulation...

    in C++ This assignment is to use your knowledge of Chap 1-5 to write a simulation program to estimate the winning chance of the Craps game. Craps is a popular dice game played in casinos. Here is how to play: Roll two dice. Each die has 6 faces representing values 1,2,3,4,5,and 6, respectively. Check the sum of 2 dice. If the sum is 2, 3, or 12 (called craps), you lose; if the sum is 7 or 11 (called natural),...

  • This is for C++ #include <random> #include <iostream> #include <ctime> using namespace std; /* In the...

    This is for C++ #include <random> #include <iostream> #include <ctime> using namespace std; /* In the game of craps, a shooter rolls 2 dice and adds the dots on the upper most faces of the dice. 7 or 11 on the first roll wins, 2, 3, or 12 on the first roll loses, andthing else is call the point and the player rolls again The following program fragment uses 1-way if statements simulate the 1st roll of the dice. Replace...

  • Please write the program in python: 3. Design and implement a simulation of the game of...

    Please write the program in python: 3. Design and implement a simulation of the game of volleyball. Normal volleyball is played like racquetball, in that a team can only score points when it is serving. Games are played to 15, but must be won by at least two points. 7. Craps is a dice game played at many casinos. A player rolls a pair of normal six-sided dice. If the initial roll is 2, 3, or 12, the player loses....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT