Overview Reference parameters Documenting functions A game of craps. Design, code, test and document.

Slides:



Advertisements
Similar presentations
Craps. /* * file : Craps.java * file : Craps.java * author: george j. grevera, ph.d. * author: george j. grevera, ph.d. * desc. : program to simulate.
Advertisements

C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
实验11.28.
Programming Functions: Passing Parameters by Reference.
Page 1 of 26 Javascript/Jscript Ch 7,8,9,10 Vadim Parizher Computer Science Department California State University, Northridge Spring 2003 Slides from.
1 CIS 205 Practice Test George Lamperti A word that has a predefined meaning in a C++ program and cannot be used as a variable name is known as.
Programming Assignment #3 CS-2301, B-Term Programming Assignment #3 User-defined Functions Due, November 18, 11:59 PM (Assignment adapted from C:
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
Overview creating your own functions calling your own functions.
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering 3 October 2007.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 12 – Craps Game Application: Introducing Random.
Practice for Midterm 1. Practice problems These slides have six programming problems for in-class practice There are an additional seven programming problems.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
Craps!. Example: A Game of Chance Craps simulator Rules – Roll two dice 7 or 11 on first throw, player wins 2, 3, or 12 on first throw, player loses 4,
Programming in C++ Lecture Notes 6 Void Functions (Procedures) Andreas Savva.
Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.
1 Lecture 3 Part 1 Functions with math and randomness.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
1 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.
1 Chapter 9 Scope, Lifetime, and More on Functions.
Chapter 5: Control Structures II (Repetition)
Recursion Chapter Nature of Recursion t Problems that lend themselves to a recursive solution have the following characteristics: –One or more.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
1 Programming in C++ Dale/Weems/Headington Chapter 7 Functions.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
Announcements 1st homework is due on July 16, next Wednesday, at 19:00 Submit to SUCourse About the homework: Add the following at the end of your code.
Functions in C Outline 1Introduction 2Program Modules in C 3Math Library Functions 4Functions 5Function Definitions 6Function Prototypes 7Header Files.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
Chapter 5 Functions For All Subtasks. Void functions Do not return a value. Keyword void is used as the return type in the function prototype to show.
Functions Why we use functions C library functions Creating our own functions.
Introduction to C++ // Program description #include directives int main() { constant declarations variable declarations executable statements return.
Cosc175/testing1 Testing Software errors are costly GIGO Preventing Errors –Echo checking –Range and limit checking –Defensive programming.
1 Functions Chapter 7 2 Hope you can function! What is R2D2 doing here? What is his function? Who is Nibble? Can he function? IS he a function? Who is.
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
CSE1222: Lecture 7The Ohio State University1. logExample.cpp // example of log(k) for k = 1,2,..,8... int main() { cout
GAME102 - INTRO WHILE LOOPS G. MacKay. Fundamental Control Structures  STRAIGHT LINE  CONDITIONAL  LOOPS.
111/15/2015CS150 Introduction to Computer Science 1 Summary  Exam: Friday, October 17,  Assignment: Wednesday, October 15, 2003  We have completed.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 - Functions.
 2003 Prentice Hall, Inc. All rights reserved. Outline 1 fig02_07.cpp (1 of 2) 1 // Fig. 2.7: fig02_07.cpp 2 // Class average program with counter-controlled.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
1 10/18/04CS150 Introduction to Computer Science 1 Functions Divide and Conquer.
An Introduction to C++ A First Look. void Functions #include #include void main( ) { cout
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
 2000 Prentice Hall, Inc. All rights reserved. 5.2Program Modules in C Functions –Modules in C –Programs combine user-defined functions with library functions.
1 Chapter 9 Scope, Lifetime, and More on Functions.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 16 – Craps Game Application Introducing Random-Number.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
 2000 Prentice Hall, Inc. All rights reserved Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 5: Control Structures II (Repetition)
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Introduction Program Components in C++ Math Library Functions Functions.
Basic concepts of C++ Presented by Prof. Satyajit De
Repetitive Structures
Bill Tucker Austin Community College COSC 1315
Number guessing game Pick a random number between 1 and 10
Chapter 9 Scope, Lifetime, and More on Functions
توابع در C++ قسمت اول اصول كامپيوتر 1.
We’re moving on to more recap from other programming languages
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Fundamental Programming
Scope of Identifier The Scope of an identifier (or named constant) means the region of program where it is legal to use that.
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
Functions in C Math Library Functions Functions Function Definitions
Presentation transcript:

Overview Reference parameters Documenting functions A game of craps. Design, code, test and document

reference parameters What do you do if you do to get two outputs from a function? GetMinAndMax int (smallest value) int (largest value) Have caller provide address of memory locations in which function may place data.

reference parameters int main () { int minimum = 0, maximum = 0; GetMinAndMax (minimum, maximum); } void GetMinAndMax (int &min, int &max) { min = INT_MIN; max = INT_MAX; } Walk through this. variables

int main() { int minimum = 0, maximum = 0; GetMinAndMax (minimum, maximum); cout << "Minimum value is " << minimum << endl; cout << "Maximum value is " << maximum << endl; return 0; } void GetMinAndMax (int &min, int &max) { int input; min = INT_MAX; max = INT_MIN; for (int i = 0; i <= 5; i++) { cout << “Enter an integer: “; cin >> input; if (min > input) min = input; if (max < input) max = input; } } // Walk through this. Variables.

Exercise Change the GetMinAndMax function so that it validates the user’s input. Only integers between 0 and 1000 are allowed (including 0 and 1000). If invalid input is detected, GetMinAndMax stops processing input and returns false to the caller. If GetMinAndMax successfully processes the input and calculates the minimum and maximum values, it returns true to the caller. GetMinAndMax int (smallest value) int (largest value) bool true if smallest and largest contain valid data false if smallest and largest not valid

More Exercises 1) Change the main function so that it calls your new version of GetMinAndMax properly 2) Fix the error in the following program segment void sum (int n) { if (0 == n) return 0; else n = n + n; } 3) Rewrite the following function prototype to return the result as a parameter instead of as a return value int Square (int y);

Documentation Standard Each function should begin with a comment of the following form: /* * * PRE: <list of pre-conditions, usually stated in terms of * input arguments> * POST: <list of post-conditions, usually stated in terms * of output arguments> * */ pre-conditions are conditions that must hold prior to the invocation of the function. post-conditions are those conditions that are true after the function returns.

Documentation Example Cube y int /* * Cube * * PRE: Input parameter integer y. Function will use this as * the base for the mathematical calculation. * POST: Return value is base y to the 3rd power. * */ y 3 int

Documentation Example /* * IsDigit - Function will determine if the input character ch is * is a digit (0 - 9) or not. * * PRE: Input parameter - char ch. * * POST: Return value true if input character ch is a digit from * * false if input character ch is not a digit * from */ IsDigit bool true if ch is 0 to 9 false if ch is not 0 to 9 ch char

Exercises 1) Produce the documentation header for the GetMinAndMax function 2) Write a C++ function that satisfies the following pre and post conditions: /* * IsUppercase * * PRE: Input character ch. Function will determine if this * is an uppercase letter or not. * POST: Return value true if input character ch is an upper * case letter in the range A - Z. * false if input character ch is not an upper * case letter in the range A - Z. */

Game of craps Design it Code it Test it Document it

Game of craps Problem statement - Write a C++ program that simulates playing a game of craps. Rules of the game. A player makes a bet then rolls the dice. If the sum is - 7 or 11 They win! - 2 or 3 or 12 They lose! - other. This is the player’s point. They roll again until: - they match the point. If so, They win! - they roll a 7. They lose! All bets are even odds. If they bet $10.00 and win, they win $ If they bet $10.00 and lose they lose $10.00.

Top level design Find out how much money the player has to bet with. Get the player’s bet for this game. Play the game. If they win the game add their bet to the money they have left to play with, otherwise subtract their bet from the money they have left to play with. If they have any money left ask them if they want to play again otherwise say Goodbye! Thanks for the loot! Functions: GetMoneyLeft GetBet PlayGame AskPlayAgain

Find out how much money the player has to bet with. int GetMoneyLeft() prompts user for the amount of money they have to play with. Validates number provided to make sure it is > 0. Get the player’s bet for this game. int GetBet (int moneyLeft) prompts user for the amount of money they want to bet on this game. Validates number provided to make sure it is less than or equal to moneyLeft. Play the game. bool PlayGame (); returns true if user wins, false if they lose If they win the game add their bet to the money they have left to play with, otherwise subtract their bet from the money they have left to play with. If they have any money left ask them if they want to play again otherwise say Goodbye! Thanks for the loot! bool AskPlayAgain(); returns true if they want to play again.

Detailed design of PlayGame bool PlayGame (); returns true if user wins, false if they lose rolls the dice. If the sum is - 7 or 11 They win! - 2 or 3 or 12 They lose! - other. This is the player’s point. They roll again until: - they match the point. If so, They win! - they roll a 7. They lose! Functions needed: int RollDice () - returns the sum of two randomly generated numbers, each in the range 1 to 6.

Next step code main program using stubs for the functions test it with stubs

AskPlayAgain code and test bool AskPlayAgain () { char answer; while (true) { cout << “Do you want to play again? “; cin >> answer; if ((answer == ‘y’) || (answer == ‘Y’)) return true; if ((answer == ‘n’) || (answer == ‘N’)) return false; } Test cases: N, n, Y, y, a, B

RollDice - code int RollDice() { int die1, die2; die1 = 1 + rand() % 6; die2 = 1 + rand() % 6; cout << “Player rolled a “ << die1 + die2 << endl; return (die1 + die2); } Testing: cout statement should always print a number less than or equal to 12;

PlayGame bool PlayGame() { Test Cases int sum, point; First roll (7, 11), (2, 3, 12), (point) switch (sum) { Point roll (7, match) case 7: case 11: In main: return true; - make sure moneyLeft is case 2: case 3: case 12: calculated properly based return false; on win or loss. default: - make sure user is never point = sum; offered the opportunity of while (true) { playing if moneyLeft is 0 sum = RollDice(); if (sum == 7) return false; if (sum == point) return true; } } // end switch }

GetMoneyLeft int GetMoneyLeft() { int money; while (true) { cout << “How much would money do you have to play with? “; cin >> money; if (money > 0) return money; cout 0” << endl; } Test cases: -1, 0, 1000

GetBet int GetBet (int moneyLeft) { int bet; while (true) { cout << “How much do you want to bet? “; cin >> bet; if (bet == 0) cout $0 allowed” << endl; else if (bet <= moneyLeft) return bet; else cout << “You only have “ << moneyLeft << “ left” << endl; Test cases: number < moneyLeft, number = moneyLeft, number > moneyLeft, Should not allow a bet of 0

Documentation and cleanup cleanup - delete couts used for debugging comments at top of file, variables comments ahead of complicated sections of code. pre and post conditions for GetMoneyLeft, GetBet, PlayGame, RollDice, AskPlayAgain.

Documentation for GetBet /* * GetBet - Prompts the player for the amount they * would like to wager on the next game * * PRE: Input parameter. int moneyLeft. The total * amount of money the player has left to wager. * Assumes this number is a non-negative * number > 0. * * POST: Return value is the amount the player would * like to wager on the next game. Will be greater * than 0 and less than or equal to the total * amount the player has left to wager. */