Sie sind auf Seite 1von 19

9/7/2011

Introductionto Flowcharting

CSC103:IntroductiontoComputerandProgramming

START

WhatisaFlowchart?
Aflowchartisa diagramthatdepicts theflowofa program. Thefigureshownhere isaflowchartforthe paycalculating program

Displaymessage Howmany hoursdidyou work?

ReadHours

Displaymessage Howmuchdo yougetpaidper hour?

ReadPayRate

MultiplyHours byPayRate. Storeresultin GrossPay.

DisplayGross Pay

END

CSC103:IntroductiontoComputerandProgramming

BasicFlowchartSymbols
Noticetherearethree typesofsymbolsinthis flowchart:
roundedrectangles parallelograms arectangle

START
Displaymessage Howmany hoursdidyou work?

Rounded Rectangle

ReadHours

Displaymessage Howmuchdo yougetpaidper hour?

Parallelogram

ReadPayRate

Eachsymbolrepresents adifferenttypeof operation.

Rectangle

MultiplyHours byPayRate. Storeresultin GrossPay.

Rounded Rectangle

DisplayGross Pay

END

CSC103:IntroductiontoComputerandProgramming

9/7/2011

BasicFlowchartSymbols
Terminals
representedbyrounded rectangles indicate a starting or indicateastartingor endingpoint
START

START
Displaymessage Howmany hoursdidyou work?

Terminal

ReadHours

Displaymessage Howmuchdo yougetpaidper hour?

ReadPayRate

MultiplyHours byPayRate. Storeresultin GrossPay.

DisplayGross Pay

END

Terminal
END

CSC103:IntroductiontoComputerandProgramming

BasicFlowchartSymbols
Input/OutputOperations
representedby parallelograms indicate an input or indicateaninputor outputoperation
Displaymessage Howmany hoursdidyou work?

START
Displaymessage Howmany hoursdidyou work?

ReadHours

Displaymessage Howmuchdo yougetpaidper hour?

Input/Output Operation

ReadPayRate

MultiplyHours byPayRate. Storeresultin GrossPay.

ReadHours
DisplayGross Pay

END

CSC103:IntroductiontoComputerandProgramming

BasicFlowchartSymbols
Processes
representedbyrectangles indicatesaprocesssuchas amathematical a mathematical computationorvariable assignment
MultiplyHours byPayRate. Storeresultin GrossPay.
Process

START
Displaymessage Howmany hoursdidyou work?

ReadHours

Displaymessage Howmuchdo yougetpaidper hour?

ReadPayRate

MultiplyHours byPayRate. Storeresultin GrossPay.

DisplayGross Pay

END

CSC103:IntroductiontoComputerandProgramming

9/7/2011

SteppingThroughthe SteppingThroughthe Flowchart Flowchart


Howmany hoursdid youwork?

START
Displaymessage Howmany hoursdidyou work?

Output Operation

ReadHours

Displaymessage Howmuchdo yougetpaidper hour?

ReadPayRate

MultiplyHours byPayRate. Storeresultin GrossPay.

VariableContents: Hours:? PayRate:? GrossPay:?

DisplayGross Pay

END

CSC103:IntroductiontoComputerandProgramming

SteppingThrough SteppingThroughthe theFlowchart Flowchart


Howmany hoursdid youwork? 40

START
Displaymessage Howmany hoursdidyou work?

Input Operation (Usertypes 40)

ReadHours

Displaymessage Howmuchdo yougetpaidper hour?

ReadPayRate

VariableContents: Hours:40 PayRate:? GrossPay:?

MultiplyHours byPayRate. Storeresultin GrossPay.

DisplayGross Pay

END

CSC103:IntroductiontoComputerandProgramming

SteppingThrough SteppingThroughthe theFlowchart Flowchart


Howmuch doyouget paidper hour?

START
Displaymessage Howmany hoursdidyou work?

ReadHours

Output Operation

Displaymessage Howmuchdo yougetpaidper hour?

ReadPayRate

VariableContents: Hours:40 PayRate:? GrossPay:?

MultiplyHours byPayRate. Storeresultin GrossPay.

DisplayGross Pay

END

CSC103:IntroductiontoComputerandProgramming

9/7/2011

SteppingThrough SteppingThroughthe theFlowchart Flowchart


Howmuch doyouget paidper hour?20

START
Displaymessage Howmany hoursdidyou work?

ReadHours

Displaymessage Howmuchdo yougetpaidper hour?

Input Operation (Usertypes 20)

ReadPayRate

VariableContents: Hours:40 PayRate:20 GrossPay:?

MultiplyHours byPayRate. Storeresultin GrossPay.

DisplayGross Pay

END

CSC103:IntroductiontoComputerandProgramming

10

SteppingThroughthe Flowchart
Howmuch doyouget paidper hour?

START
Displaymessage Howmany hoursdidyou work?

ReadHours

Displaymessage Howmuchdo yougetpaidper hour?

ReadPayRate

VariableContents: Hours:40 PayRate:20 GrossPay:800

Process:The productof40 times20is storedin GrossPay

MultiplyHours byPayRate. Storeresultin GrossPay.

DisplayGross Pay

END

CSC103:IntroductiontoComputerandProgramming

11

SteppingThroughthe Flowchart
Yourgross payis800

START
Displaymessage Howmany hoursdidyou work?

ReadHours

Displaymessage Howmuchdo yougetpaidper hour?

ReadPayRate

VariableContents: Hours:40 PayRate:20 GrossPay:800

MultiplyHours byPayRate. Storeresultin GrossPay.

Output Operation

DisplayGross Pay

END

CSC103:IntroductiontoComputerandProgramming

12

9/7/2011

FourFlowchartStructures
Sequence Decision Repetition Case

CSC103:IntroductiontoComputerandProgramming

13

SequenceStructure
aseriesofactionsareperformedinsequence Thepaycalculatingexamplewasasequence flowchart.

CSC103:IntroductiontoComputerandProgramming

14

DecisionStructure
Oneoftwopossibleactionsistaken,dependingona condition.

CSC103:IntroductiontoComputerandProgramming

15

9/7/2011

DecisionStructure
Anewsymbol,thediamond,indicatesayes/noquestion.If theanswertothequestionisyes,theflowfollowsonepath.If theanswerisno,theflowfollowsanotherpath

NO

YES

CSC103:IntroductiontoComputerandProgramming

16

DecisionStructure
Intheflowchartsegmentbelow,thequestionisx<y?is asked.Iftheanswerisno,thenprocessAisperformed.Ifthe answerisyes,thenprocessBisperformed.

NO x<y?

YES

ProcessA

ProcessB

CSC103:IntroductiontoComputerandProgramming

17

DecisionStructure
Theflowchartsegmentbelowshowshowadecision structureisexpressedinCasanif/elsestatement.
Flowchart

CCode

NO x<y?

YES

if (x < y) a = x * 2; else

Calculateaas xplusy.

Calculateaas xtimes2.

a = x + y;

CSC103:IntroductiontoComputerandProgramming

18

9/7/2011

DecisionStructure
Theflowchartsegmentbelowshowsadecisionstructurewith onlyoneactiontoperform.Itisexpressedasanifstatement inCcode.
Flowchart CCode

NO x<y?

YES

if (x < y) a = x * 2;

Calculateaas xtimes2.

CSC103:IntroductiontoComputerandProgramming

19

RepetitionStructure
Arepetitionstructurerepresentspartoftheprogramthat repeats.Thistypeofstructureiscommonlyknownasaloop.

CSC103:IntroductiontoComputerandProgramming

20

RepetitionStructure
Noticetheuseofthediamondsymbol.Alooptestsa condition,andiftheconditionexists,itperformsanaction. Thenitteststheconditionagain.Iftheconditionstillexists, theactionisrepeated.Thiscontinuesuntiltheconditionno longerexists.

CSC103:IntroductiontoComputerandProgramming

21

9/7/2011

RepetitionStructure
Intheflowchartsegment,thequestionisx<y?isasked.If theanswerisyes,thenProcessAisperformed.Thequestion isx<y?isaskedagain.ProcessAisrepeatedaslongasxis lessthany.Whenxisnolongerlessthany,therepetition stopsandthestructureisexited.

YES x<y? ProcessA

CSC103:IntroductiontoComputerandProgramming

22

RepetitionStructure
Theflowchartsegmentbelowshowsarepetitionstructure expressedinCasawhileloop.

Flowchart

CCode

while (x < y)

YES x<y? Add1tox

x = x+1;

CSC103:IntroductiontoComputerandProgramming

23

ControllingaRepetitionStructure
Theactionperformedbyarepetitionstructuremust eventuallycausethelooptoterminate.Otherwise,aninfinite loopiscreated. Inthisflowchartsegment,xisneverchanged.Oncetheloop starts,itwillneverend. QUESTION:Howcanthis flowchartbemodifiedso itisnolongeraninfinite YES loop? x<y? Displayx

CSC103:IntroductiontoComputerandProgramming

24

9/7/2011

ControllingaRepetitionStructure
ANSWER:Byaddinganactionwithintherepetitionthat changesthevalueofx.

YES x<y? Displayx Add1tox

CSC103:IntroductiontoComputerandProgramming

25

APreTestRepetitionStructure
Thistypeofstructureisknownasapretestrepetition structure.TheconditionistestedBEFORE anyactionsare performed.

YES x<y? Displayx Add1tox

CSC103:IntroductiontoComputerandProgramming

26

APreTestRepetitionStructure
Inapretestrepetitionstructure,iftheconditiondoesnot exist,theloopwillneverbegin.

YES x<y? Displayx Add1tox

CSC103:IntroductiontoComputerandProgramming

27

9/7/2011

APostTestRepetitionStructure
Thisflowchartsegmentshowsaposttest repetitionstructure. TheconditionistestedAFTER theactions areperformed. Aposttestrepetitionstructurealways performsitsactionsatleastonce.

Displayx

Add1tox

YES x<y?

CSC103:IntroductiontoComputerandProgramming

28

APostTestRepetitionStructure
Theflowchartsegmentbelowshowsaposttestrepetition structureexpressedinCasadowhileloop.
Displayx
CCode Flowchart do {

Add1tox

YES x<y?

printf(%d, x); x = x +1; } while (x < y);

CSC103:IntroductiontoComputerandProgramming

29

CaseStructure
Oneofseveralpossibleactionsistaken,depending onthecontentsofavariable.

CSC103:IntroductiontoComputerandProgramming

30

10

9/7/2011

CaseStructure
Thestructurebelowindicatesactionstoperform dependingonthevalueinyears_employed.
CASE years_employed

1
bonus=100

2
bonus=200

3
bonus=400

Other
bonus=800

CSC103:IntroductiontoComputerandProgramming

31

CaseStructure

Ifyears_employed=2, bonusissetto200 Ifyears_employed=1, bonusissetto100 bonus is set to 100

Ifyears_employed=3, bonusissetto400 Ifyears_employedis anyothervalue,bonus any other value bonus issetto800

CASE years_employed l d

1
bonus=100

2
bonus=200

3
bonus=400

Other
bonus=800

CSC103:IntroductiontoComputerandProgramming

32

Connectors
Sometimesaflowchartwillnotfitononepage. Aconnector(representedbyasmallcircle)allows youtoconnecttwoflowchartsegments.

CSC103:IntroductiontoComputerandProgramming

33

11

9/7/2011

Connectors

TheAconnector indicatesthatthesecond flowchartsegmentbegins wherethefirstsegment ends.

START

END A

CSC103:IntroductiontoComputerandProgramming

34

Modules
Aprogrammodule(suchasafunctioninC)is representedbyaspecialsymbol.

CSC103:IntroductiontoComputerandProgramming

35

Modules

Thepositionofthemodule symbolindicatesthepointthe moduleisexecuted. Aseparateflowchartcanbe constructedforthemodule.

START

ReadInput.

Callcalc_pay function.

Displayresults.

END

CSC103:IntroductiontoComputerandProgramming

36

12

9/7/2011

CombiningStructures
Structuresarecommonlycombinedtocreatemorecomplex algorithms. Theflowchartsegmentbelowcombinesadecisionstructure withasequencestructure.

YES x<y? Displayx Add1tox

CSC103:IntroductiontoComputerandProgramming

37

CombiningStructures
Thisflowchartsegment showstwodecision structurescombined.

NO
x>min?

YES

Displayxisoutside thelimits.

NO
x<max?

YES

Displayxisoutside thelimits.

Displayxis withinlimits.

CSC103:IntroductiontoComputerandProgramming

38

Review
Whatdoeachofthefollowingsymbols represent?

(Answeronnextslide) CSC103:IntroductiontoComputerandProgramming
39

13

9/7/2011

Answer
Whatdoeachofthefollowingsymbols represent?
Terminal Input/Output Operation Decision

Connector

Process

Module

CSC103:IntroductiontoComputerandProgramming

40

Review
Namethefourflowchartstructures.

(Answeronnextslide) CSC103:IntroductiontoComputerandProgramming
41

Answer
Sequence Decision Repetition p Case

CSC103:IntroductiontoComputerandProgramming

42

14

9/7/2011

Review
Whattypeofstructureisthis?

(Answeronnextslide) CSC103:IntroductiontoComputerandProgramming
43

Answer
Repetition

CSC103:IntroductiontoComputerandProgramming

44

Review
Whattypeofstructureisthis?

(Answeronnextslide) CSC103:IntroductiontoComputerandProgramming
45

15

9/7/2011

Answer
Sequence

CSC103:IntroductiontoComputerandProgramming

46

Review
Whattypeofstructureisthis?

(Answeronnextslide) CSC103:IntroductiontoComputerandProgramming
47

Answer
Case

CSC103:IntroductiontoComputerandProgramming

48

16

9/7/2011

Whattypeofstructureisthis?

Review

(Answeronnextslide) CSC103:IntroductiontoComputerandProgramming
49

Answer
Decision

CSC103:IntroductiontoComputerandProgramming

50

SwitchonaLamp

CSC103:IntroductiontoComputerandProgramming

51

17

9/7/2011

Drawaflowcharttofind thesumoffirst50natural numbers.

CSC103:IntroductiontoComputerandProgramming

52

Drawaflowchartto findthelargestofthree numbersA,B,andC.

CSC103:IntroductiontoComputerandProgramming

53

Drawaflowchartfor computingfactorialN (N!)

CSC103:IntroductiontoComputerandProgramming

54

18

9/7/2011

Flowchartsandsubprocesses
Thesubprocess isusefulbecause:
Itprovidesameansofsimplifyingprogramsbymaking commonprocessesavailabletoawidenumberof programs. Itdividescomplexprogramsintomodules Itmakesformorereliableprogramssinceonceitisshown thataprocessworksthenitcanbemadeasubprocess and neednotbetestedagain.

CSC103:IntroductiontoComputerandProgramming

55

Example

CSC103:IntroductiontoComputerandProgramming

56

Usingnestedloopsinflowcharts

CSC103:IntroductiontoComputerandProgramming

57

19

Das könnte Ihnen auch gefallen