Upload
susanna-howard
View
220
Download
0
Embed Size (px)
Citation preview
CS 108 Computing FundamentalsNotes for Tuesday, February 24, 2015
• 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
• 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 betterUse 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 dice2. Read the total of the two dice3. If the roll is 7 or 11, then the user wins and the user is done with
this current roll4. If the roll is 2, 3, or 12, then the user loses and the user is done
with the current roll5. 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 gameCraps step 1: Roll the two diceCraps 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
http://web.cs.sunyit.edu/~urbanc/cs_108_feb_24a.txt
• 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 <stdio.h>
printf( "\n\n\n Welcome to the game of Craps. \n\n" ) ;
http://web.cs.sunyit.edu/~urbanc/cs_108_feb_24b.txt
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 <stdlib.h>include <time.h>
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
• Work on algorithm step 3
• 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 ) ;
http://web.cs.sunyit.edu/~urbanc/cs_108_feb_24d.txt
• Work on algorithm step 3A
• Work on algorithm step 3A
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
• Work on algorithm step 3B
• Work on algorithm step 3B
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
• Work on algorithm step 3C1
• 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
http://web.cs.sunyit.edu/~urbanc/cs_108_feb_24g.txt
• Work on algorithm step 3C3
• 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
http://web.cs.sunyit.edu/~urbanc/cs_108_feb_24h.txt
• Work on algorithm step 3C5
• Work on algorithm step 3C5
if (point == die_1 + die_2) { printf( " You Win!! \n\n") ; break ; }
http://web.cs.sunyit.edu/~urbanc/cs_108_feb_24i.txt
• Work on algorithm step 3C6
• Work on algorithm step 3C6
if (die_1 + die_2 == 7) { printf( " Sorry, you lose.\n\n") ; break ; }
http://web.cs.sunyit.edu/~urbanc/cs_108_feb_24j.txt
• Work on algorithm step 3C7
• 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 ; } }
http://web.cs.sunyit.edu/~urbanc/cs_108_feb_24k.txt
http://web.cs.sunyit.edu/~urbanc/cs_108_feb_24L.txt
The Whole Program