CS 108 Computing Fundamentals Notes for Tuesday, February 24, 2015.

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

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
1 Chapter Eight Designing Libraries. 2 Programming Complexity As long as we continue to solve problems of ever-increasing sophistication, the process.
EGR 141 Computer Problem Solving in Engineering and Computer Science
Lets Play Catch! Keeping Score in Alice By Francine Wolfe Duke University Professor Susan Rodger May 2010.
Tonight’s JavaScript Topics 1 Conditional Statements: if and switch The Array Object Looping Statements: for, while and do-while.
Overview Reference parameters Documenting functions A game of craps. Design, code, test and document.
Games of probability What are my chances?. Roll a single die (6 faces). –What is the probability of each number showing on top? Activity 1: Simple probability:
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 16 – Craps Game Application Introducing Random-Number.
Page 1 of 26 Javascript/Jscript Ch 7,8,9,10 Vadim Parizher Computer Science Department California State University, Northridge Spring 2003 Slides from.
16-Jun-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 2007 Pearson Education, Inc. All rights reserved C Functions.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 12 – Craps Game Application: Introducing Random.
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.
In this chapter we introduce the idea of a random variable as well as looking at its shape, center, and spread.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
My game… You pay £1 to play I roll a dice If it lands on 1 or 2 you win £1.50 If it lands on 3, 4, 5, 6 you lose Will this game make me a profit if 10.
HOW TO CALCULATE MEAN AND MEDIAN
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,
Presented by Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 12 Boolean Expressions, Switches, For-Loops Chapter 7.
Chapter 3 Section 3.5 Expected Value. When the result of an experiment is one of several numbers, (sometimes called a random variable) we can calculate.
Brian Duddy.  Two players, X and Y, are playing a card game- goal is to find optimal strategy for X  X has red ace (A), black ace (A), and red two (2)
USING MAPS IN 2 nd GRADE Michael Bodag ED 417 – 01 P.P. Lesson October 23, 2001.
CS 108 Computing Fundamentals Notes for Thursday, February 12, 2015.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
In this chapter we introduce the idea of what it means for something to be truly random. We also investigate techniques for simulating randomness.
CS 108 Computing Fundamentals Notes for Thursday, February 19, 2015.
Understanding Randomness
Previously Repetition Structures While, Do-While, For.
More on Logic Today we look at the for loop and then put all of this together to look at some more complex forms of logic that a program will need The.
Dr. Soha S. Zaghloul2 Let arr be an array of 20 integers. Write a complete program that first fills the array with up to 20 input values. Then, the program.
1-Dec-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
Not So Great Expectations! Which game do you think has the highest return per dollar?
Lesson 10.5 AIM: The Game of Pig. DO NOW What is a strategy? Give an example from your daily life of how you use strategies.
Chapter 6 Methods: A Deeper Look. Objectives In this chapter you will learn: How static methods and fields are associated with an entire class rather.
EPSII 59:006 Spring Call-by-value example #include void increment(int); //prototype for increment function int main(void) { int a=1; printf("Value.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
CS 108 Computing Fundamentals Notes for Tuesday, September 22, 2015.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Craps Game Application Introducing Random-Number Generation and Enum.
 2000 Prentice Hall, Inc. All rights reserved. 5.2Program Modules in C Functions –Modules in C –Programs combine user-defined functions with library functions.
Project. Research Project Due: – Project report due Monday midnight Delayed a bit due to popular demand Can accept an even higher delay (Tuesday at noon)
Random Numbers Random numbers are extremely useful: especially for games, and also for calculating experimental probabilities. Formula for generating random.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 16 – Craps Game Application Introducing Random-Number.
More on Logic Today we look at the for loop and then put all of this together to look at some more complex forms of logic that a program will need The.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
CS 108 Computing Fundamentals Notes for Thursday, February 18, 2016.
Computer Programming 12 Lesson 6 – Loop structure By: Dan Lunney.
Expected Value Standards: MM1D2. Students will use the basic laws of probability. d. Use expected value to predict outcomes. Lesson Essential Question:
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Recap: If, elif, else If <True condition>:
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Methods Chapter 6.
Number guessing game Pick a random number between 1 and 10
CS 108 Computing Fundamentals Notes for Thursday, September 14, 2017
The while Looping Structure
Loops A portion of a program that repeats a statement or a group of statements is called a loop. The statement or group of statements to be repeated is.
1) C program development 2) Selection structure
IPC144 Introduction to Programming Using C Week 3 – Lesson 1
We’re moving on to more recap from other programming languages
The while Looping Structure
Unit 7 - Short-Circuit Evaluation - De Morgan’s Laws - The switch statement - Type-casting in Java - Using Math.random()
Agenda Warmup Lesson 1.9 (random #s, Boolean variables, etc)
Fun… Tree Diagrams… Probability.
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
The while Looping Structure
Presentation transcript:

CS 108 Computing Fundamentals Notes for Tuesday, February 24, 2015

Out of 170 possible points  High score: 162 (95.3%)  Low score: 80 (47.1%)  Average score: ( 77.1% or a C+ grade )  Median score: 130  Let's Review Exam #1

Will be grading all day tomorrow Let's talk about GHP #6 GHP #5

Some/many of you have abandoned (or never embraced) the "inside-out one-step-at-a-time" methodology Many of you do not spend enough time developing your algorithm completely Many of you over-think you solutions… simpler is usually better  Use a paper, pencil, and material things (like dice) to help you develop a simple algorithm.  Don't even THINK about C programming when you are developing your algorithm General Observations on Your Programming Technique

A dice game Craps Let's talk about how the game is played Let's Code a Game

User rolls two dice Read the total of the two dice If the roll is 7 or 11, then the user wins and the user is done with the current roll If the roll is 2, 3, or 12, then the user loses and the user is done with the current roll If the roll is 4, 5, 6, 8, 9, or 10, then that becomes the user's “point” The user rolls again and again until the user rolls either his/her point to and win OR the user rolls a 7 to lose... both winning and losing causes the user to be done with the current roll Craps

Notice that specifying the specifics of the challenge (in this case the game of Craps) clarifies exactly WHAT needs to be done... we can take this WHAT and translate it into an algorithm that formalizes our understanding of the requirements and our plan (the algorithm forms the blueprint for our coding effort). We can then test the algorithm Only after successfully testing the algorithm should we can start coding Craps

1.User rolls two dice 2.Read the total of the two dice 3.If the roll is 7 or 11, then the user wins and the user is done with this current roll 4.If the roll is 2, 3, or 12, then the user loses and the user is done with the current roll 5.If the roll is 4, 5, 6, 8, 9, or 10, then that becomes the user's “point” 6.The user rolls again and again until the user rolls either his/her point to and win OR the user rolls a 7 to lose... both winning and losing causes the user to be done with the current roll Notice: some of the steps are not "simple" enough Craps

Craps step 0: Introduce the game Craps step 1: Roll the two dice Craps step 2: Read the total of the two rolled dice Craps step 3a: If the roll is 7 or 11, then the user wins and the user is done Craps step 3b: If the roll is 2, 3, or 12, then the user loses and the user is done Craps step 3c1: If the roll is 4, 5, 6, 8, 9, or 10, then that becomes the user's “point” Craps step 3c2: The user rolls Craps step 3c3: If the roll is equal to the user's “point” then the user wins and the user is done Craps step 3c4: If the roll is 7 then the user loses and the user is done Craps step 3c5: If the user does not roll his "point" or a 7, then the user rolls again (go to step 3c2) Craps

Place the algorithm into the template

Work on algorithm step 1  Don't forget to add the preprocessor directive necessary Craps

Work algorithm step 1  Don't forget to add the preprocessor directive necessary #include printf( "\n\n\n Welcome to the game of Craps. \n\n" ) ; Craps

Work algorithm step 2  Don't forget to add variables, preprocessor directives, and test your code thoroughly Craps

Work on algorithm step 2  Don't forget to add variables, preprocessor directives, and test your code thoroughly include int die_1 = 0, die_2 = 0 ; srandom ( (unsigned) time (NULL) ) ; die_1 = random ( ) % 6 + 1; die_2 = random ( ) % 6 + 1; printf( "\n\nTest Test die_1 value: %d \n\n\n", die_1 ) ; printf( "\n\nTest Test die_2 value: %d \n\n\n", die_2 ) ;

Work on algorithm step 3

 Don't forget to add variables and test your code thoroughly printf( "\n\nYou rolled a : %d \n\n\n", die_1 + die_2 ) ;

Work on algorithm step 3A

switch ( die_1 + die_2 ) { case 7 : case 11 : printf( "You are a WINNER!!\n\n") ; break ; }

Work on algorithm step 3B

case 2 : case 3 : case 12 : printf( "Sorry, but you lose.\n\n") ; break ;

Work on algorithm step 3C1

default : point = die_1 + die_2; printf( "\n\nYour point is: %d \n\n\n", point ) ; Need to declare point as variable of data type int Discover that our algorithm needs another step to make sure each step is "simple" Add a new algorithm step 3C2 Adjust the algorithm step numbers

Work on algorithm step 3C3

die_1 = random ( ) % 6 + 1; die_2 = random ( ) % 6 + 1; Discover that our algorithm needs another step to make sure each step the program communicates the value of the roll to the user Add a new algorithm step 3C4 Adjust the algorithm step numbers

Work on algorithm step 3C5

if (point == die_1 + die_2) { printf( " You Win!! \n\n") ; break ; }

Work on algorithm step 3C6

if (die_1 + die_2 == 7) { printf( " Sorry, you lose.\n\n") ; break ; }

Work on algorithm step 3C7

#define TRUE 1 default : point = die_1 + die_2 ; printf( "\n\nYour point is: %d ", point ) ; while (TRUE) { die_1 = random ( ) % 6 + 1; … printf( " Sorry, you lose.\n\n") ; break ; }

The Whole Program