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

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

Embed Size (px)

Citation preview

Page 1: CS 108 Computing Fundamentals Notes for Tuesday, February 24, 2015

CS 108 Computing FundamentalsNotes for Tuesday, February 24, 2015

Page 2: 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: 131.5 ( 77.1% or a C+ grade ) Median score: 130 Let's Review

Exam #1

Page 3: CS 108 Computing Fundamentals Notes for Tuesday, February 24, 2015

• Will be grading all day tomorrow

• Let's talk about GHP #6

GHP #5

Page 4: CS 108 Computing Fundamentals Notes for Tuesday, February 24, 2015

• 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

Page 5: CS 108 Computing Fundamentals Notes for Tuesday, February 24, 2015

• A dice game

• Craps

• Let's talk about how the game is played

Let's Code a Game

Page 6: CS 108 Computing Fundamentals Notes for Tuesday, February 24, 2015

• 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

Page 7: CS 108 Computing Fundamentals Notes for Tuesday, February 24, 2015

• 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

Page 8: CS 108 Computing Fundamentals Notes for Tuesday, February 24, 2015

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

Page 9: CS 108 Computing Fundamentals Notes for Tuesday, February 24, 2015

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

Page 10: CS 108 Computing Fundamentals Notes for Tuesday, February 24, 2015

• Place the algorithm into the template

http://web.cs.sunyit.edu/~urbanc/cs_108_feb_24a.txt

Page 11: CS 108 Computing Fundamentals Notes for Tuesday, February 24, 2015

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

Craps

Page 12: CS 108 Computing Fundamentals Notes for Tuesday, February 24, 2015

• 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

Page 13: CS 108 Computing Fundamentals Notes for Tuesday, February 24, 2015

• Work algorithm step 2 Don't forget to add variables, preprocessor directives, and test

your code thoroughly

Craps

Page 14: CS 108 Computing Fundamentals Notes for Tuesday, February 24, 2015

• 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

Page 15: CS 108 Computing Fundamentals Notes for Tuesday, February 24, 2015

• Work on algorithm step 3

Page 16: CS 108 Computing Fundamentals Notes for Tuesday, February 24, 2015

• 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

Page 17: CS 108 Computing Fundamentals Notes for Tuesday, February 24, 2015

• Work on algorithm step 3A

Page 18: CS 108 Computing Fundamentals Notes for Tuesday, February 24, 2015

• 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

Page 19: CS 108 Computing Fundamentals Notes for Tuesday, February 24, 2015

• Work on algorithm step 3B

Page 20: CS 108 Computing Fundamentals Notes for Tuesday, February 24, 2015

• 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

Page 21: CS 108 Computing Fundamentals Notes for Tuesday, February 24, 2015

• Work on algorithm step 3C1

Page 22: CS 108 Computing Fundamentals Notes for Tuesday, February 24, 2015

• 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

Page 23: CS 108 Computing Fundamentals Notes for Tuesday, February 24, 2015

• Work on algorithm step 3C3

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

• 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

Page 25: CS 108 Computing Fundamentals Notes for Tuesday, February 24, 2015

• Work on algorithm step 3C5

Page 26: CS 108 Computing Fundamentals Notes for Tuesday, February 24, 2015

• 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

Page 27: CS 108 Computing Fundamentals Notes for Tuesday, February 24, 2015

• Work on algorithm step 3C6

Page 28: CS 108 Computing Fundamentals Notes for Tuesday, February 24, 2015

• 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

Page 29: CS 108 Computing Fundamentals Notes for Tuesday, February 24, 2015

• Work on algorithm step 3C7

Page 30: CS 108 Computing Fundamentals Notes for Tuesday, February 24, 2015

• 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

Page 31: CS 108 Computing Fundamentals Notes for Tuesday, February 24, 2015

http://web.cs.sunyit.edu/~urbanc/cs_108_feb_24L.txt

The Whole Program