Number guessing game Pick a random number between 1 and 10

Slides:



Advertisements
Similar presentations
Exercise 4 1. Write a program that simulates coin tossing. For each toss of the coin the program should print Heads or Tails. Let the program toss the.
Advertisements

Overview Reference parameters Documenting functions A game of craps. Design, code, test and document.
Chapter 5 C Functions The best way to develop and maintain a large program is to divide it into several smaller program modules, each of which is more.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.1Introduction 3.2Program Components in C++ 3.3Math Library Functions.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 2007 Pearson Education, Inc. All rights reserved C Functions.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 12 – Craps Game Application: Introducing Random.
CS 117 Spring 2002 April 5, Exam 3 April 10 –files, arrays, strings, classes –practice exams are up –review on Monday.
1 Random numbers Random  completely unpredictable. No way to determine in advance what value will be chosen from a set of equally probable elements. Impossible.
C Lecture Notes Functions (Cont...). C Lecture Notes 5.8Calling Functions: Call by Value and Call by Reference Used when invoking functions Call by value.
Functions. 3Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece more manageable than the original program.
 2000 Prentice Hall, Inc. All rights reserved. Functions in C Outline 1Introduction 2Program Modules in C 3Math Library Functions 4Functions 5Function.
The game of Craps Rules of play: 1. Played with two dice (six faces to a die – numbers 1-6 per face) 2. Sequence of betting rounds (or just rounds) 3.
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,
1 Lecture 3 Part 1 Functions with math and randomness.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Functions in C Outline 1Introduction 2Program Modules in C 3Math Library Functions 4Functions 5Function Definitions 6Function Prototypes 7Header Files.
DiceRoller DiceRoller (object class) and DiceRollerViewer client class: Write a DiceRoller class (similar to Yahtzee) to: Allow the person to initially.
CMSC 1041 Functions II Functions that return a value.
Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions 5.5Function Definitions 5.6Function Prototypes.
 2008 Pearson Education, Inc. All rights reserved Case Study: Random Number Generation C++ Standard Library function rand – Introduces the element.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.1Introduction 3.2Program Components in C++ 3.3Math Library Functions.
C++ Programming Lecture 10 Functions – Part II
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 - Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions (Header files and Library Functions) Outline.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
1 Chapter 3 - Functions Outline 3.1Introduction 3.2Program Components in C++ 3.3Math Library Functions 3.4Functions 3.5Function Definitions 3.6Function.
EPSII 59:006 Spring Call-by-value example #include void increment(int); //prototype for increment function int main(void) { int a=1; printf("Value.
Random numbers in C++ Nobody knows what’s next....
 2000 Prentice Hall, Inc. All rights reserved. 5.2Program Modules in C Functions –Modules in C –Programs combine user-defined functions with library functions.
UNIT 11 Random Numbers.
Dale Roberts CSCI 230 Functions Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions.
Function Call Stack and Activation Frame Stack Just like a pile of dishes Support Two operations push() pop() LIFO (Last-In, First-Out) data structure.
1 Generating Random Numbers Textbook ch.6, pg
Revision. Generating a pseudo-random number Necessary libraries: and Seeding: srand(time(NULL))  We set the seed based on the current time  NB! Time-dependent.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Introduction Program Components in C++ Math Library Functions Functions.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Functions Course conducted by: Md.Raihan ul Masood
CISC181 Introduction to Computer Science Dr
C Characters and Strings
Modelling and Simulating Social Systems with MATLAB
Chapter 3 - Functions Outline 3.1 Introduction
TMF1414 Introduction to Programming
Functions, Part 2 of 2 Topics Functions That Return a Value
CSC113: Computer Programming (Theory = 03, Lab = 01)
Deitel- C:How to Program (5ed)
CS1010 Programming Methodology
Lab Session-9 CSIT-121 Spring 2005
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
توابع در C++ قسمت اول اصول كامپيوتر 1.
Chapter 6 - Functions Outline 5.1 Introduction
C Characters and Strings – Review Lab assignments
We’re moving on to more recap from other programming languages
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
CS150 Introduction to Computer Science 1
ECE 103 Engineering Programming Chapter 51 Random Numbers
Characters and Strings Functions
Review Lab assignments Homework #3
Revision.
C Characters and Strings
Assignment Operators Topics Increment and Decrement Operators
Chapter 3 - Functions Outline 3.1 Introduction
Functions that return a value
Functions in C Math Library Functions Functions Function Definitions
Presentation transcript:

Number guessing game Pick a random number between 1 and 10 Ask the user to guess a number Compare- verify that it is a number Output: You win or Try again (need a conditional loop) Hints: char someChar = '5';  int myInt = atoi(&someChar);  int isdigit(int c); Parameters c -- This is the character to be checked. Return Value This function returns nonzero value if c is a digit, else 0

5.9 Random Number Generation rand function Load <stdlib.h> Returns "random" number between 0 and RAND_MAX (at least 32767) i = rand(); Pseudorandom Preset sequence of "random" numbers Same sequence for every function call Scaling To get a random number between 1 and n 1 + ( rand() % n ) rand() % n returns a number between 0 and n - 1 Add 1 to make random number between 1 and n 1 + ( rand() % 6) number between 1 and 6

isdigit() Function The isdigit() function is part of the character-handling library <ctype.h> #include <stdio.h> #include <ctype.h> main() { char cResponse = '\0'; printf("\nPlease enter a number: "); scanf("%c", &cResponse); if ( isdigit(cResponse) != 0 ) printf("\nThank you for entering a number\n"); else printf("\nYou did not enter a number\n"); }

Craps!

What do we need? Random number generator User input Looping With error checking Looping

5.9 Random Number Generation srand function <stdlib.h> Takes an integer seed and jumps to that location in its "random" sequence srand( seed ); srand( time( NULL ) ); //load <time.h> time( NULL ) Returns the time at which the program was executed in seconds “Randomizes" the seed

2 Randomizing die-rolling program */ 3 #include <stdlib.h> 1 /* Fig. 5.9: fig05_09.c 2 Randomizing die-rolling program */ 3 #include <stdlib.h> 4 #include <stdio.h> 5 6 int main() 7 { 8 int i; 9 unsigned seed; 10 11 printf( "Enter seed: " ); 12 scanf( "%u", &seed ); 13 srand( seed ); 14 15 for ( i = 1; i <= 10; i++ ) { 16 printf( "%10d", 1 + ( rand() % 6 ) ); 17 18 if ( i % 5 == 0 ) 19 printf( "\n" ); 20 } 21 22 return 0; 23 } 1. Initialize seed 2. Input value for seed 2.1 Use srand to change random sequence 2.2 Define Loop 3. Generate and output random numbers

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, 5, 6, 8, 9, 10 - value becomes player's "point" Player must roll his point before rolling 7 to win Prompt the user how much to bet on each throw Verify that they enter a number