Download presentation
Presentation is loading. Please wait.
Published bySusanna Howard Modified over 9 years ago
1
CS 108 Computing Fundamentals Notes for Tuesday, February 24, 2015
2
Out of 170 possible points High score: 162 (95.3%) Low score: 80 (47.1%) Average score: 131.5 ( 77.1% or a C+ grade ) Median score: 130 Let's Review Exam #1
3
Will be grading all day tomorrow Let's talk about GHP #6 GHP #5
4
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
5
A dice game Craps Let's talk about how the game is played Let's Code a Game
6
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
7
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
8
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
9
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
10
Place the algorithm into the template http://web.cs.sunyit.edu/~urbanc/cs_108_feb_24a.txt
11
Work on algorithm step 1 Don't forget to add the preprocessor directive necessary Craps
12
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" ) ; http://web.cs.sunyit.edu/~urbanc/cs_108_feb_24b.txt Craps
13
Work algorithm step 2 Don't forget to add variables, preprocessor directives, and test your code thoroughly Craps
14
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 ) ; http://web.cs.sunyit.edu/~urbanc/cs_108_feb_24c.txt
15
Work on algorithm step 3
16
Don't forget to add variables and test your code thoroughly printf( "\n\nYou rolled a : %d \n\n\n", die_1 + die_2 ) ; http://web.cs.sunyit.edu/~urbanc/cs_108_feb_24d.txt
17
Work on algorithm step 3A
18
switch ( die_1 + die_2 ) { case 7 : case 11 : printf( "You are a WINNER!!\n\n") ; break ; } http://web.cs.sunyit.edu/~urbanc/cs_108_feb_24e.txt
19
Work on algorithm step 3B
20
case 2 : case 3 : case 12 : printf( "Sorry, but you lose.\n\n") ; break ; http://web.cs.sunyit.edu/~urbanc/cs_108_feb_24f.txt
21
Work on algorithm step 3C1
22
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 http://web.cs.sunyit.edu/~urbanc/cs_108_feb_24g.txt
23
Work on algorithm step 3C3
24
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 http://web.cs.sunyit.edu/~urbanc/cs_108_feb_24h.txt
25
Work on algorithm step 3C5
26
if (point == die_1 + die_2) { printf( " You Win!! \n\n") ; break ; } http://web.cs.sunyit.edu/~urbanc/cs_108_feb_24i.txt
27
Work on algorithm step 3C6
28
if (die_1 + die_2 == 7) { printf( " Sorry, you lose.\n\n") ; break ; } http://web.cs.sunyit.edu/~urbanc/cs_108_feb_24j.txt
29
Work on algorithm step 3C7
30
#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 ; } http://web.cs.sunyit.edu/~urbanc/cs_108_feb_24k.txt
31
http://web.cs.sunyit.edu/~urbanc/cs_108_feb_24L.txt The Whole Program
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.