Introduction to Computing Dr. Nadeem A Khan
Lecture 2
Read Chapter 3
Program Planning 1. Analyze 2. Design 3. Choose the Interface 4. Code 5. Test and Debug 6. Complete the Documentation
The Problem-Solving Process Input Processing Output
Program Design Tools ► Pseudo-code ► Flow Charts ► Hierarchy Charts
Postage stamp problem: How many stamps to put on a envelop given one stamp is needed for every five sheets of paper or a fraction thereof How many stamps to put on a envelop given one stamp is needed for every five sheets of paper or a fraction thereof
Pseudocode ► Abbreviated version of actual computer code in English-like statements
Pseudo code: Postage Stamp Problem ► Program – Determine the number of stamps for a letter Read Sheets Set the number of stamps to sheets / 5 Round the number of stamps Display the number of stamps
Flowcharts ► Special geometric symbols connected by arrows
Postage stamp problem Start Read sheets Set stamps = sheets/ 5 Display stamps Round stamps up to next whole number End input processing output processing
Elements of Flowcharts Symbol Name Flowline Terminal Input/Output Processing Decision
Continued… Symbol Name Connector Off page Connector Predefined process Annotation
Hierarchy chart ► Shows overall program structure
Hierarchy Charts: Postage Stamp Problem Postage Stamp Problem Read Sheets Calculate stamps Display stamps Set stamps = sheets/5 Round stamps to next whole number
Another problem: Given a street number of one-way street in New York, decide the direction of the street, either eastbound or westbound Given a street number of one-way street in New York, decide the direction of the street, either eastbound or westboundNote: Even numbered street: Eastbound Odd numbered street: Westbound
Decisions ► Sequence Structure – a sequence followed without skipping any line. ► Decision Structure – a structure which requires a decision for any lines of code to be executed.
Decision Structure: Pseudocode IF condition is TRUE THEN Process step(s) 1 ELSE Process step(s) 2 Process step(s) 2 END IF
Decision Structure: Flowchart Process Step (s) 2 Is Condition True Process Step (s) 1
Decision Structure: Pseudocode What in case of multiple conditions?
Decision Structure: Pseudocode IF condition 1 is TRUE THEN Process step(s) 1 ELSE IF condition 2 is TRUE THEN Process step(s) 2 Process step(s) 2ELSE Process step(s) 3 END IF
Pseudo-Code: Street Direction Problem Get Street IF Street is EVEN Display Eastbound ELSE Display Westbound END IF
Flow Chart: Street Direction Problem End Start Get Street Is street even? Display Westbound Display Eastbound
Hierarchy Chart: Street Direction Problem Street Direction Program Get Street Number Decide whether street number is odd or even Display direction
Still Another Problem ► Calculate and report the grade-point average of a class.
The Loop Structure ► A programming structure that executes instructions many times.
Flow chart: loop structure No Process Step(s) Is condition true ? Yes
Pseudo code: loop structure ► DO WHILE condition is TRUE Process Step(s) LOOP
Draw the Flowchart for the class average problem
Pseudo code: Class Average Problem INITIALIZE Counter and Sum to 0 DO WHILE there are more data Get the next Grade Add the Grade to the Sum Increment the Counter LOOP Compute Average = Sum/Counter Display Average
Hierarchy Chart: Class Average Problem Class average program Get grade Compute sum Display average Calculate average