introduction to computing dr. nadeem a khan. lecture 2

Post on 18-Jan-2016

218 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Introduction to Introduction to ComputingComputing

Dr. Nadeem A KhanDr. Nadeem A Khan

Lecture 2Lecture 2

Read Chapter 3Read Chapter 3

Program PlanningProgram Planning

1.1. AnalyzeAnalyze

2.2. DesignDesign

3.3. Choose the Choose the InterfaceInterface

4.4. CodeCode

5.5. Test and DebugTest and Debug

6.6. Complete the Complete the DocumentationDocumentation

The Problem-Solving Process The Problem-Solving Process

Input

Processing

Output

Program Design ToolsProgram Design Tools

►Pseudo-codePseudo-code

►Flow ChartsFlow Charts

►Hierarchy Hierarchy ChartsCharts

Postage stamp problem:Postage stamp problem:

How many stamps to put on a How many stamps to put on a envelop given one stamp is envelop given one stamp is needed for every five sheets of needed for every five sheets of paper or a fraction thereofpaper or a fraction thereof

PseudocodePseudocode

►Abbreviated version of actual Abbreviated version of actual computer code in English-like computer code in English-like statementsstatements

Pseudo code: Postage Stamp Pseudo code: Postage Stamp ProblemProblem

►Program – Determine the number of Program – Determine the number of stamps for a letterstamps for a letter

Read SheetsRead Sheets

Set the number of stamps to sheets / 5Set the number of stamps to sheets / 5

Round the number of stamps Round the number of stamps

Display the number of stamps Display the number of stamps

FlowchartsFlowcharts

►Special geometric symbols Special geometric symbols connected by arrows connected by arrows

Postage Postage stamp stamp

problemproblem

Start

Read sheets

Set stamps = sheets/ 5

Display stamps

Round stamps up to next

whole number

End

input

processing

output

processing

Elements of FlowchartsElements of Flowcharts

Symbol Name

Flowline

Terminal

Input/Output

Processing

Decision

Continued…Continued…

Symbol Name

Connector

Off page Connector

Predefined process

Annotation

Hierarchy chartHierarchy chart

►Shows overall program structureShows overall program structure

Hierarchy Charts: Postage Hierarchy Charts: Postage Stamp ProblemStamp Problem

Postage Stamp

Problem

Read

Sheets

Calculate

stamps

Display

stamps

Set stamps = sheets/5

Round stamps to next whole

number

Another problem:Another problem:

Given a street number of one-way Given a street number of one-way street in New York, decide the street in New York, decide the direction of the street, either direction of the street, either eastbound or westboundeastbound or westbound

Note: Note: Even numbered street: EastboundEven numbered street: EastboundOdd numbered street: WestboundOdd numbered street: Westbound

DecisionsDecisions

►Sequence StructureSequence Structure – a sequence – a sequence followed without skipping any line.followed without skipping any line.

►Decision StructureDecision Structure – a structure – a structure which requires a decision for any lines which requires a decision for any lines of code to be executed.of code to be executed.

Decision Structure: Decision Structure: PseudocodePseudocode

IF condition is TRUE THENIF condition is TRUE THENProcess step(s) 1Process step(s) 1

ELSEELSE Process step(s) 2Process step(s) 2

END IFEND IF

Decision Decision Structure:Structure:FlowchartFlowchart Process

Step (s) 2

Is Condition True

Process Step (s)

1

Decision Structure: Decision Structure: PseudocodePseudocode

What in case of multiple What in case of multiple conditions?conditions?

Decision Structure: Decision Structure: PseudocodePseudocode

IF condition 1 is TRUE THENIF condition 1 is TRUE THENProcess step(s) 1Process step(s) 1

ELSE IF condition 2 is TRUE ELSE IF condition 2 is TRUE THENTHEN Process step(s) 2Process step(s) 2

ELSEELSEProcess step(s) 3Process step(s) 3

END IFEND IF

Pseudo-Code: Street Direction Pseudo-Code: Street Direction ProblemProblem

Get Street

IF Street is EVEN

Display Eastbound

ELSE

Display Westbound

END IF

Flow Flow Chart: Chart: Street Street DirectioDirectionnProblem Problem

End

Start

Get Street

Is street even?

Display Westbound

Display Eastbound

Hierarchy Chart: Street Hierarchy Chart: Street Direction ProblemDirection Problem

Street Directio

n Program

Get Street

Number

Decide whether street

number is odd or even

Display direction

Still Another ProblemStill Another Problem

► CCalculate and report the grade-point alculate and report the grade-point average of a class.average of a class.

The Loop StructureThe Loop Structure

► A programming structure that A programming structure that executes instructions many times.executes instructions many times.

Flow chart: Flow chart: loop structureloop structure No

Process Step(s)

Is condition true ?

Yes

Pseudo code: loop structurePseudo code: loop structure

► DO WHILEDO WHILE condition is TRUE condition is TRUE

Process Step(s)Process Step(s)

LOOPLOOP

Draw the Flowchart for the class Draw the Flowchart for the class average problemaverage problem

Pseudo code: Class Average Pseudo code: Class Average ProblemProblem

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 Hierarchy Chart: Class Average ProblemProblem

Class average program

Get grade

Compute sum

Display average

Calculate

average

top related