Problem Solving and Programming CS140: Introduction to Computing 1 8/21/13
Last Time Computing is more than programming Programming is more than coding If you can’t solve the problem, you can’t write the code Design before code Computers are stupid, you are not Practice, practice, practice Document 2
Going from problem to solution Problems are expressed in terms of values The specific problem is the set of input values The result is the final values computed 3
Steps in programming Conceptual Computational Coded problem problem problem design phase implementation phase The better the design, the easier the implementation. 4
Values are stored as “variables” A variable is – a name – for a place – to store a value Called a “Variable” because the value “varies” Value of a variable is sometimes referred to as the “state” of the variable Changing a variable’s value is changing its state 5
Specific problem(s) / General solution Specific Problem 2, 10 5, 9 42, 42 Input values 6 Specific Solution Resulting values General Problem Sum 2 values one general solution
From Initial State to Final State Programs determine the state changes Sequences of statements Selection (branching) Repetition (looping) Subprogram 7
Job 1: Design developing a solution to the problem If you can’t solve the problem, you can’t write the code 8
Procedural Design Representations Flowcharts Activity Diagrams Pseudo code 9 hand-written solutions are good!
Activity Diagram src : 10
Activity Diagram ElementSymbol Start Stop Statements Order of activities 11 start end
What is a statement? Some example statements: – Input, read, get a value – Print, display – Assignment Statement algebra with variables, storing the result in a variable 12
Hello 13 Print “Hello” Hello start end
Sum 2 values 14 Print “enter two values” Input num1, num2 sum = num1 + num2 Print sum enter two values enter two values end start
Activity Diagram ElementSymbol Start Stop Statements Branching Order of activities 15 start end
Branch structure 16 TRUE FALSE condition either or !!
Branching example 17 num1 > num2 TRUE FALSE Print “the 1st value is larger” Print “the 2nd value is larger” start Input num1, num2 end the 1st value is larger the 1st value is larger 2 10 the 2nd value is larger 2 10 the 2nd value is larger
Activity Diagram ElementSymbol Start Stop Statements Branching Repetition Order of activities 18 start end
Branching / Selection If (conditional_expression) { ; …; } else { ; …; } 19 if (conditional_expression) { ; …; } switch (control_expression) { case expression 1: ; case expression 2: ; case expression n: ; default: ; } break; break label; continue; return; return expression;
Repetition (looping) while (expression) { ; …; } 20 do { ; …; } while (expression); for (initialization; condition; increment or decrement) { ; …; }
Repetition structure 21 condition FALSE TRUE
Repetition example 22 FALSE TRUE branching repetition
Activity Diagram ElementSymbol Start Stop Statements Branching Repetition Subprograms Order of activities 23 start end
Subprogram 24 We haven’t mastered programs yet, so let’s leave subprograms till later.
Pseudo code read num1, num2 if num1 > num2 then print “the 1st value is larger” else print “the 2nd value is larger” 25 num1 > num2 TRUE FALSE Print “the 1st value is larger” Print “the 2nd value is larger” start Input num1, num2 end
Tracing / Desk-checking Find (and fix) design problems before they become code problems Play the computer – Simulate the program you have designed by following the design you wrote – Follow the design you wrote, not the the one in your head 26
Branching example 27 num1 < num2 TRUE FALSE Print “the 1st value is larger” Print “the 2nd value is larger” start Input num1, num2 end 200 the 2nd value is larger 200 the 2nd value is larger
Exercise problem - Barista 28 take order place cup make coffee milk Y prep milk pour in cup N syrup Y pour in cup N give customer more customers Y N take 5 loop take order place cup make coffee if milk then begin prep milk pour milk end if syrup then add syrup give customer if more orders then goto loop else break loop endloop take 5
Summary (not the end of class) Programs transform the initial input values (problem statement) to the final output values (solution) Solutions are composed of – Sequence of statements – Branching – Repetition Design before code – Flowcharts – Activity Diagrams – Pseudo Code 29
30
Now, a game Elements of Tic Tac Toe: Space – empty, X, O Board – 9 Spaces Game – the board – moves – win/loss/tie src: 31
Tic Tac Toe – structure code design 32
A move – activity code design Decision diagram Input: by player Output: was move made T/F 33
The game – activity code design Input: by two players Output: Win/Loss/Tie 34
Next Time Work on your homework Ask questions 35