Download presentation
Presentation is loading. Please wait.
Published byBob Ewell Modified over 6 years ago
1
Problem Solving and Programming CS140: Introduction to Computing 1 8/21/13
2
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
3
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
4
Steps in programming Conceptual Computational Coded problem problem problem design phase implementation phase The better the design, the easier the implementation. 4
5
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
6
Specific problem(s) / General solution Specific Problem 2, 10 5, 9 42, 42 Input values 6 Specific Solution 12 14 84 Resulting values General Problem Sum 2 values one general solution
7
From Initial State to Final State Programs determine the state changes Sequences of statements Selection (branching) Repetition (looping) Subprogram 7
8
Job 1: Design developing a solution to the problem If you can’t solve the problem, you can’t write the code 8
9
Procedural Design Representations Flowcharts Activity Diagrams Pseudo code 9 hand-written solutions are good!
10
Activity Diagram src : http://xkcd.com/210/ 10
11
Activity Diagram ElementSymbol Start Stop Statements Order of activities 11 start end
12
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
13
Hello 13 Print “Hello” Hello start end
14
Sum 2 values 14 Print “enter two values” Input num1, num2 sum = num1 + num2 Print sum enter two values 2 10 12 enter two values 2 10 12 end start
15
Activity Diagram ElementSymbol Start Stop Statements Branching Order of activities 15 start end
16
Branch structure 16 TRUE FALSE condition either or !!
17
Branching example 17 num1 > num2 TRUE FALSE Print “the 1st value is larger” Print “the 2nd value is larger” start Input num1, num2 end 2 10 20 1 20 10 the 1st value is larger 20 10 the 1st value is larger 2 10 the 2nd value is larger 2 10 the 2nd value is larger
18
Activity Diagram ElementSymbol Start Stop Statements Branching Repetition Order of activities 18 start end
19
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;
20
Repetition (looping) while (expression) { ; …; } 20 do { ; …; } while (expression); for (initialization; condition; increment or decrement) { ; …; }
21
Repetition structure 21 condition FALSE TRUE
22
Repetition example 22 FALSE TRUE branching repetition
23
Activity Diagram ElementSymbol Start Stop Statements Branching Repetition Subprograms Order of activities 23 start end
24
Subprogram 24 We haven’t mastered programs yet, so let’s leave subprograms till later.
25
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
26
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
27
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
28
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
29
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
30
31
Now, a game Elements of Tic Tac Toe: Space – empty, X, O Board – 9 Spaces Game – the board – moves – win/loss/tie src: http://xkcd.com/832/ 31
32
Tic Tac Toe – structure code design 32
33
A move – activity code design Decision diagram Input: by player Output: was move made T/F 33
34
The game – activity code design Input: by two players Output: Win/Loss/Tie 34
35
Next Time Work on your homework Ask questions 35
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.