lab5

Home Up

 

 Lab1 ] Lab2 ] Lab3 ] lab4 ] [ lab5 ] lab6 ] Lab7 ] Lab8 ] Lab9 ] Lab10 ]

CP107  LAB 5

 

LAB OBJECTIVES:

Be able to write functions that process arguments and return a value

Call function and pass values by reference

Use Loops do/while loop

Use enum operator

IO manipulators

Use relational operators and compound conditions such as and, or , not

Recognizing Boolean expressions as true or false

Use a switch statement for decision making

Use an if statement for decision making

Write pseudo code

Use random number generator

 

LAB PROCEDURE:

Read the problem description carefully and create pseudo code from the information that is provided below. Submit pseudo code as part of the assignment. Using the pseudo that you just created, develop a computer program. Test the program and submit it.

              

LAB ASSIGNMENT

1) LAB 5A

Write a complete C++ program with two alternate functions specified below, of which each simply triples the variable count defined in main. Then compare and contrast the two approaches. 
These two functions are:

  1. Function tripleByBalue that receives a copy of count by value, triples the copy and returns the new value.

  2. Function tripleByReference that receives count by reference via a reference parameter and triples the original value of count through its alias (i.e., the reference parameter).

Submit the program as lab5a.cpp

 

2) Lab5B

Modify the craps program to allow wagering. Package as function the portion of the program that runs on game of craps. Initialize variable bankBalance to 1000 dollars. Prompt the player to enter a wager.  Use a while loop to check that wager is less than or equal to bankBalance and, if not, prompt the user to reenter wager until a valid wager is entered. After a correct wager is entered, run on game of craps. If the player wins, increase bankBalance by wager and print the new bankBalance. If the player loses, decrease bankBalance by wager, print the new bankBalance, check whether the bankBalance has become zero and, if so, print the message “Sorry, you busted!”,  As the game progress, print various message to create some “chatter”. The exact messages are listed below.

Submit the program as craps5b.cpp

Function “void chatter( void )”

Use switch statement with argument ( 1 + rand() % 9 ) and print the following messages.

      case 1,   "Oh, you're going for broke, huh?"

      case 2,   "Aw cmon, take a chance!"

      case 3,   "Hey, I think this guy is going to break the bank!!"

      case 4,   “You're up big. Now's the time to cash in your chips!"

      case 5,   "Way too lucky! Those dice have to be loaded!"

      case 6,   "Bet it all! Bet it all!"

      case 7,   "Can I borrow a chip?"

      case 8,   "Let's try our luck at another table."

      case 9,   "You're a cheat! It is just a matter of time before I catch you!!!"

 

int rollDice( void );

Simulates roll of two dice, prints the following message and returns the sum of the two dice rolls.

Use mod operator to get a random number in range 1 to 6.  Dice will be rolled twice.

Prints out "Player rolled " << die1 << " + " << die2 << " = "  << workSum << '\n';

 

 

Status craps( void );

call rollDice and then determines if the game is WON, LOSS or CONTINUE. Use a switch statement.
return the result of the game as a enumerated type gameStatus

 

 

Main Program

int result, wager = 0, bankBalance = 1000;  
char playAgain;

use srand to randomize time.

Use a do-while loop as the main control structure of the program
do

      Prompt the user to enter a wager and also display the bankBalance.

 

     Do not accept an invalid wager

          While user enters invalid response, keep prompting the user to enter a valid wager, read the response.

    Call function craps, which plays the game and get result. Check the result.
    If user won then added wager to the balance, if user lost then subtract the wager from the balance.
    Check the balance. If balance is greater than 0 then continue.

    If LOST, display message and display the bankBalance.

    If bankBalance is zero, then inform use that you are busted and end the game.

    If WON, update the bankBalance.

    Prompt user if he/she would like to continue again.

While user's response is to continue, keep going

 

 

 

Copyright (c) Yusuf Family Website 2001