You are on page 1of 12

A comput.ei :Ls d mechanism uapabie of rnanlpuiatrng rrumbers and symbols.according to a well-defined prograrn.

For exampIe,

I. 2. 3. 4-

input a number, input another number, add the two numbers, output the result,

is the sort of Prolr4il a computer can perfonn. Obviousl.l," a computer would not be particularly useful if this were all 1t could do; a computer is useful because it can be progranrned to do many different jobs.

! Flowcharts
A flowchart is simply a way to dj-agram a program. fhe idea is to represent what is going on in a form that is easy to read 'and work with. It is most helpf-uJ- to flowchart a problem before trying to write a program to solve it. This procedure is almost indi.spensable for proglarns that are lengthy or comPlex. Various shapes are used to rePresent different types of operations. For example, an input operation is represented by a parallelogram. To read in values for A and B, we would use the following
diagram:

A parallelogram is also used for output operatlons.


F^+ -^ noE as ^^-,,ai-a c()nlusJ-ng i+ -a rt as nrrr aa^llld,y seeln, -inca 5rl--r nr^dr:fitnor:l-'_"-.--

fhls rs
wllLcs
following:

either Input or Output at the top of the box to indicate which is


'.^r*! rrredrru. mh,,c !11ur, vsr ^,1, vss}/ee ^rrf6r1+ i ^n< vYvrsurvrrJ ^h6':f a16 ranrcsented by the

--w4y>

|"**I 1_:t
The next mosl important box is where we do our actual work. m'^:^ I !eij!cselrLq r-: L., - lcLLdlr9rc, and the inscruccicns i-cr wnat uy d -^-!---r^ l'lII5 t5 r written i-nside it are i-s to be done

lL

To ind::ate Ll:e orCer in 'vhich che opera'-ions are tc be perfo:med, the boxes are connected wi'th arrows ti'iat point in the drrectiorr of flow. For e:<anple, to read in tlvo nr.:.mbers, aCd them, and writ"e out the result, we could use t'he flowchart shown j-n
r Ic. l--I.

ilee Start and Stoo ter:rural-s are an esserltial .carL oi euery fIc'c:rarc. The Star: --:rnrr.el- :rclcaces c::e rirs: instrlc:ron :t:

that no more be performed, and the StoP ternr-inaL indicaies rt' reaching after tions should be Processed

rhcfrrlc-

FTGURE 1-1

a dj-amond-shaped box' A decision statement i-s represented by in the form of a decision every For the Present, we sha]l write prosirnpli'fy question; later we shall introduce notation io "his cedure:
Here we wxlte

sse questlon.

(two of which would not shal1 now construct a few problems !L^r i 1 lric+72tF the aPPlicatj-ons normaliy be soived by a cornput-er) of flowcharts.
We
Llld I !!{uv E'e --

Problem

Suppose you are the president of conpany X' T\do of your You look at them and employees, A and B, come into your office. decide to gi,ve the better-looking one a pay raise. Then they leave' How would you flowchart this decision?

Solution. We must, of course, use a Start box' lhis night correspond to your arrival at the office in the norning. The next part of the ffowchart ls ttie Input box, representing wh-n the two The Decision box comes next. This is employees enter your office, where you make the decision: "Is employee E better-looking tJran is when you empl0vee A?', You then do the actual "arithmeticr'-this when the emgive a or B the raise. The next step is the output, shown one the would be flowchart Your floyu." leave your office.

I ^: ,
/
ra

,n"u.,'
I

;.1

No -rl,"

i\

Yes f-"--;;-l

/ lo,'"1
outoot,

TfGUKL

f'Z 7

FLowchart for ProbJ.em

Problen

This is the same as Problem 1, except now you must apply the process to many pairs of eiTrployees. SoLution- We can use the flowchart for Problem 1, but this tj-me we must add a Decislon box after the Output box, and we must test to see if we are done. For now, lec us just ask the question: "Are we done?" If so, we shall terminate. If not, we shall want to input trro more enoployees; so ltre draw a l-rne of flow back to t'he Input box to indicate that this is the next instruction.
Note that every employee in the flowchart in Fig. 1-3 is caIled A or B. This is alright because we have the Output box in or:r loop that allows the old pair to leave. If we had put the Output box after the last Decision box, we would eventually fiII the office with people!

better the'

3 gets
rai- s e

FIGURE ]-- 3

Fl-owchart for Problem

ProbLem

Input three unequal numbers and output the largest one' Sol-uticn- Since we can compare only two numbers at a Eine' rve shaLl need three Decision boxes to compare A to Bt I to C' and A to C. The flowchart woul-d be the one shown in Fig' I-4'

IUUKL

L-A 3

FLowchart for Probfen

Lt' a-lrl+i' ts:; --r,,!i Dy aSSI9n.-.IS- Solutl-on ^varrurua ^ urc ..^l Convince yourself of !L^ -' fi-owchart. ing values to A, Bt and C, and tracing these tirrough the

We have been placing a question in each Decj-sion box and extending two brancheS from each, one for a YeS answer and one for a No. Actually, there are several ways to make a Decision box' We could write A:B in the box, meaning "compare A xo Br" and then put a greater than, Iess than, or equal to sign on each of three branches extending from the box.

means

For example, the Decision box shown in the following diagram to take the left branch if A < B, the right branch if A > B' or the lower branch if A = Bz

If we wanted to go to the same Place when A = B as we do +'"'^'nrennhoq and F nlli qr.Y $e Lrvv Lrf L > A B, we could combine fha

l^rhen

Another way to do this is to use a quescicn fcrrn. as we been doing:

j-rave

Either of these represents exactly the


m:+-l6r nf nerqon: I nreforencp Ye!
v!

same

thing; it is just
useo.

Fq to which

one is

portant fact is that for any particular result derived the comparison, there ls exactly one branch out of the box' we could not have

Thg infrorn makinqr

Hence'

because tf A = B, there would be no place to go. Even if we knew in a particular problem that A would never be equal to 8, we would have to construct somethinq like

By now it should be clear how a flowchart represents an actual program: The boxes are the instructions, and the lines of flow speclfy the order in which the instructions are to be processed. Furthermore, flowcharts visually j-llustrate the principle that after one instruction j-s performed it nust be clear which instruction is to be performed next, and that only one instruction at a time can be
performed. The flowchart should be a flexible and useful tool. In a completed flowchart, the differentl-y shaped boxes help to icientify types of instructions, and the Lines of flow are easy to follow to Ceter:nine rvhat actually happens and when'

A few conventions involved in writing floi',rcharts should be mentioned. Usua11y, lines of flow are drawn either verticalfy or horizontally. ft is often convenient to have the main line of flow proeeed vertieallv from the tog of the page to the bottom. The only Purpose of these restrj-ctions is to improve legibility. a program loop is represented (that j-s, when the program returns to a group of instructions in order to perform them more than once), the Line of flow can return to i]re box representing the first instruction in the loop, as shown in Fig. 1-5.
When

fn some situations it is inconvenient to draw a line of flow to an instructicn, so a connector (a snail circle with an identifier unit) cas be used. This inoicaies that the next instruction to be performeo is ident.ified by' a simila:: connector ;a-'her than by a line of f1ow. Figure I-6 gives an example of a situation where the use of a connector j-s called for to avoid crossing lines. If a flowchart requires more than one page' a connecLor can be used to indicate that the next instruction is on a different page.
'Ihe purpose of the flowchart is to help tire progranmer' In the encl, clarity and convenience should be the governing factors wh':rn a flowchart is prepared. Flowcharts also help us construct the prcAram as we g'o along. Usr.:allyr a flowchart is not drawn from start to finish; the nrain instructions are done first and then the rest- is fill.ed. in. Even an experienced progranmer may make Several false starts before a saiisfactory flowchart is mapped ouc. A littl-e practice will give you a feeling for the method'

Itlethod

af showing a program

FIGURE 1- 5

Loop

t"

lU L.1L L- a

L;se

of a ccnnectot

ExanPTe 7

the suni of the integers from L to d the formula S = N(N + 7)/2J' We in programming, of taxing a pa:not uncommon nethod, the shall use tial sum; that is, at some step we have a location caLled SUM which of this location, contains I + 2 + ... + (r - 1). To the contents 1 is: instruction mai-n f. the So we shall add
suppose we want to compute [and suppose we do not remember

After that, we must test to see if we are finished; so we compare f to /v. If r = N, we output the answer SUM. If npt' we add I to f and go back to the computation- So far, our flowchart, which represents the essential part of the program, looks like Fig ' I-7 '

fr-^tr^A llruxL

1 A t- /

lNoti." tre equals sign in the instruction SUM = sul'I + f' Thrs nhrrinrrqlw.annot have the normal arithmecic m'eaninE' Instead'
it has the computer meaning "is replaced by' " We can read the "replace the value of the variable suM by stat-ement as follows: the old rr:Itre nltts J'" Scrne authors use the syr''bol<-tc lndicar'e replaceniert.
v +vs! &l vqrt.

e:l! !a:

:talrt

!:l:rl--'

.1.

I Averaging

Numbers

Letusnowconsiderasimpleproblenrcfapracticalnature.
ExamPl-e 2

SupposewehaveaSequenceofitemsandwewanttoreadthem inoneatatime,sumthern,andfindtheiraverage'Todothis' we need to know how many nurnlcers lhere are and to find their sum' one way to L,ai.:i]le this is to read in. the number of items, N, and then follow a procedure similar to that of Example 1. In this case, we would input /r/r and then input the items and find their sum by performing a loop iV times. After this j-s done, we would compute the and then output th!-s average. The exact procedure is given "rr"tag. by the flowchart in Fig. 1-9, where each nudcer in turn is cal1ed x'

QA .d'-h))P
SUM=0

ogq)

) F

f tNl

,,no',

,q d o.'J aF.olro OOrd lr tn o r+r.rr+J tr'3 ..{ U|o x {).trGO d t' >'.t 0, rd .-l .lJ .l C t{g'Jq-{r0 CJ d 5'.{

a,>tolm t{d+J(Jd ou3 rl.,tct4C)06 o -Q .C C'-l +rx .q

/ rnput, t-l x,

ETO F4 !o0).p di .tJ rdtl 6..r.r ..'| .q d cl..r>3fa)

.o.(n Z-lJttOq)Ft

,-J t+r rd cJ

)*

SUM = SUM +,1

doEc5C) ..r.i 3 o > rd oJ tr O > t c .aord {JESECJIdAJ nrdEl\

0)

I<N?

o.rJr+{-CO>.c ..1 C ..r lJ -q rd lJ


>d

o c o qJ od o tr.qocJH O1 Or OciOlJ p"q.q c.J(! +JlJ+JOgO n n!.J

\r"
'f to

q-.1

AV=SUM+rV

/ o..o'., /ou

>>cn ulo 4J!COOE5 oJ-{ rd d o ll 3 .p.-{ o o 0{ -c +J O q -.Ft--t 0.)'.1 O o oo .q'.lom +rcJ(1)-rr-c.q +r!q oc.q z, ttPt{-iObO. .-i c.-tEAE o d o' .rr..{ OOEX .AEeEr,tFl

a,oqo.lJ.QCJ lOat) v'n E H v iu r! .-{ +r .u l.t .r{ +J C Oltr.l

-6rrO'ort

FICURE 1-9

Ffowchart for ExantPTe

In this example, .notice that cne work box has been used tc represent the two instruci.icns SUI'I = 0 and f = O' This i-s occaqinr='l lrr rlone to conserve space when the tt"'o (or more) instructions are not separated by a Dec:-sj-on or cther box affecting the

Fiow*ii$rtins
S;r:mb*i

Fieaning

l_-__} tt

tt

It 'J !t

./\ \

,',\ /\ \-/v .,'

rti
l{"
1 -i

13

i+ tt il YI
4---_ -.F

You might also like