programming exercises - home - springer978-1-349-16258...programming exercises 1. convert the...

28
Programming Exercises 1. Convert the flowchart shown in figure A.l into a BASIC program. 2. Convert the flowchart shown in figure A.2 into a BASIC program and check that it works successfully. 3. The flowchart in figure A.3 represents an algorithm to print out the binomial coefficients. Write a program in BASIC from the flowchart and check that it works. 4. Write a program which will simulate a weekly draw in which three prize-winning numbers are selected by the random number generator. The winning numbers are then used to pick the names corresponding to these numbers held on a file of all the participants in the draw. You can use a serial or a random access file for this. 5. Write a program to reverse the order of the words in a sentence. 6. Write a program which will read in a sentence and then replace each occurrence of a particular word in the sentence by some other word. For example, if the input sentence was "THE VERY FAMOUS ACTOR IN THE THEATRE" replace the word 'THE' by 'A' each time it appears in the sentence. 7. The ancient game of 'The Tower of Hanoi' consists of three pegs on a board with discs of decreasing size threaded on one of them with the largest at the bottom. The problem is to move the discs to another peg so that they end up in the same order but without ever placing a disc on top of one of smaller diameter than itself. The strategy for the solution of the puzzle is always to move the smallest disc in a clockwise direction, that is, from peg 1 to peg 2, peg 2 to peg 3 or peg 3 to peg 1, and the largest exposed disc to the next available position. Write a program in BASIC to simulate the solution to the puzzle, printing out the position of the discs after each move.

Upload: duongdang

Post on 10-May-2018

229 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Programming Exercises - Home - Springer978-1-349-16258...Programming Exercises 1. Convert the flowchart shown in figure A.l into a BASIC program. 2. Convert the flowchart shown in

Programming Exercises

1. Convert the flowchart shown in figure A.l into a BASIC program.

2. Convert the flowchart shown in figure A.2 into a BASIC program and check that it works successfully.

3. The flowchart in figure A.3 represents an algorithm to print out the binomial coefficients. Write a program in BASIC from the flowchart and check that it works.

4. Write a program which will simulate a weekly draw in which three prize-winning numbers are selected by the random number generator. The winning numbers are then used to pick the names corresponding to these numbers held on a file of all the participants in the draw. You can use a serial or a random access file for this.

5. Write a program to reverse the order of the words in a sentence.

6. Write a program which will read in a sentence and then replace each occurrence of a particular word in the sentence by some other word. For example, if the input sentence was

"THE VERY FAMOUS ACTOR IN THE THEATRE"

replace the word 'THE' by 'A' each time it appears in the sentence.

7. The ancient game of 'The Tower of Hanoi' consists of three pegs on a board with discs of decreasing size threaded on one of them with the largest at the bottom. The problem is to move the discs to another peg so that they end up in the same order but without ever placing a disc on top of one of smaller diameter than itself. The strategy for the solution of the puzzle is always to move the smallest disc in a clockwise direction, that is, from peg 1 to peg 2, peg 2 to peg 3 or peg 3 to peg 1, and the largest exposed disc to the next available position. Write a program in BASIC to simulate the solution to the puzzle, printing out the position of the discs after each move.

Page 2: Programming Exercises - Home - Springer978-1-349-16258...Programming Exercises 1. Convert the flowchart shown in figure A.l into a BASIC program. 2. Convert the flowchart shown in

112 Continuing BASIC

B, "IS", L

Figure A.l

Page 3: Programming Exercises - Home - Springer978-1-349-16258...Programming Exercises 1. Convert the flowchart shown in figure A.l into a BASIC program. 2. Convert the flowchart shown in

Programming Exercises

EXECUTE FACT

(2N-1,F)

6 ,...------'-----,

s +- s + T * (-1 )N+l

Figure A.2

113

Page 4: Programming Exercises - Home - Springer978-1-349-16258...Programming Exercises 1. Convert the flowchart shown in figure A.l into a BASIC program. 2. Convert the flowchart shown in

114

EXECUTE FACT (N, F)

EXECUTE FACT

(X, F1)

EXECUTE FACT

(N-X,F 2 )

Figure A.3

Continuing BASIC

Page 5: Programming Exercises - Home - Springer978-1-349-16258...Programming Exercises 1. Convert the flowchart shown in figure A.l into a BASIC program. 2. Convert the flowchart shown in

Programming Exercises 115

8. An airline maintains a list of every flight available each day together with the number of seats available on that flight and the number of the next flight to the same destination. If there are no more flights to that destination then a next flight number of 999 indicates that this is so. For example, if flight 345 has 67 available seats and the next flight to that destination is flight 667 then the appropriate record on a file would contain the data 345,67,667. If flight 667 has 101 seats available and is the last flight of the day to that destination then its file record would contain 667,101 ,999. Devise a simple system that will accept as input a flight number and the number of seats required. The response should be an accep­tance of the booking or details of the next flight to that destination if there is one. The system should keep the data regarding the flight up to date as bookings are made. The data need not be kept on a file, three lists could keep the flight numbers, seats available and the next flight number.

9. Write a BASIC program which will accept as input a string consisting of a series of names each followed by four digits. The output is to consist of the names truncated to their first four characters followed by the four digits. If a name happens to contain less than four characters then asterisks are used to pack out the four characters. For example, if the input was

JONES2345KA Y5678BILLINGS7896H06543

the output would be

JONE2345KA Y*5678BILU~96H0**6543

10. Devise a BASIC program which will convert a message into the Morse code.

11. Convert the flowchart in figure A.4 into a BASIC program to solve algebraic equations of the form f(x) = (/J. Use the user-defined function in BASIC for the input of the left-hand side of the equation. XS, XEND and XINC are the starting values, finishing values and incremental steps in the search made for the solution of the equation.

12. Write a program in BASIC which will carry out the arithmetic operations of +, -, x and +on two given integers which may have up to 20 digits in them. In the case of multiplication the two numbers should be such that the product does not exceed 99 digits in length. (Hint: input should be in the form of a string.)

13. Write a program in BASIC to convert numbers into their equivalent in Roman numerals.

Page 6: Programming Exercises - Home - Springer978-1-349-16258...Programming Exercises 1. Convert the flowchart shown in figure A.l into a BASIC program. 2. Convert the flowchart shown in

116

14 ~-__J'-----. BITX +-XING J+-0 X+-X+BITX

2 .----L-----, J+-0 X+- XS XEND +- XEND2

BITX +-INC

X<-X-BITX BITX +- BITX/10 X<-X+BITX J<-J+1

Continuing BASIC

0

+

Figure A.4

Page 7: Programming Exercises - Home - Springer978-1-349-16258...Programming Exercises 1. Convert the flowchart shown in figure A.l into a BASIC program. 2. Convert the flowchart shown in

Programming Exercises 117

14. Devise a BASIC program which will accept and print the answers to a simple crossword puzzle. As each clue is answered there should be a printout showing the present state of the puzzle. There should also be a routine for amending wrong answers.

Page 8: Programming Exercises - Home - Springer978-1-349-16258...Programming Exercises 1. Convert the flowchart shown in figure A.l into a BASIC program. 2. Convert the flowchart shown in

Appendix

In this section, listings of seven programs illustrate a number of uses of the BASIC language. They are as follows

Program !-Statement print Program 2-Critical path analysis Program 3-Accounting program Program 4-Present values of series of cash flows Program 5-Invoice print Program 6-Text justification Program 7-Lorry tyre simulation

The programs are easy to follow and are annotated by REM statements. Programs 2 and 7 use the additional PRINT USING statement for formatting out­put and Program 6 uses the file called "$LPT" which is the line printer. For the use of these additional features the reader is referred to the manufacturer's reference manual of BASIC.

OOJ0 REM STATEMENT PRINT PRCmRAM O•:':~(' F;:I···M AU fHOr~ l! t ~~U!:;S!~.L..L

00 .. \0 l·(i:'M trWI.If TS FI'WM TWO FILE!:;: .... o040 I'~H·i , ,. ,~ cu:rrOM[I': F [LI:: W l ! H r~LJ<H OMLI'' NO •, NAME' o~l!Df(fe:SG (..JNli Ho~l '''r'CI. oo:•:;o l·:t:M ., A ·n~ANS.VTIDN l'lLE UTVINiJ cu;;nmu' NU., l'!tHEvTfiANS T'rPE r~Nn

0060 REM ANl! AN AMOUNT (!070 f~[M fHEF'F CAN BE m,t.•ERAL Tf(AN">I\t:lHIN!. I"'Ef': C!.n>TOME:H 0080 REM TRANSACTION TYPE IS I ~OR PURCHASE• J FOR PAYMFC:NT ANI:r 3 FOR 0090 REM CREl!IT NOTF 0100 REM l!ATE IS IN YYMMDD FORMAT 01 .i () I~ EM BOTH F.!L.Eli Af([ IN Ctn; TDMI'·.I'~ NUMBFI'( m:CJUENCE 0120 DIM NSC50J,Al$[50J,A2SC50J~A3Sl·~()],A4t[~0J 0130 OPEN FILErloJJ,'OLl!CUST' 0140 OPEN FILE[2o1Jo'NEWCLJST' 0150 OPEN FILEC3o3Jo'TRANS' 0160 REAli FILE[1JoNloNSoA1SoA2$oA3$oA4$oB 0170 READ FILEC3loN2oDoToA 0180 IF N2>N1 THEN GOTD 0510 0190 IF N1<>N2 ~HEN GOl~ 0580 O:!OO GDSLJ£< 0640 0210 PRINT "OPENING BALANCE'1 TABCJOI;D 0220 PIUNT · ()230 LET L~<l.

0240 IF T=1 THEN GDTD 0300 0250 IF T•2 TI~N GDTD 0350 0260 IF T=3 THEN GDTD OJOO 0270 f"[·dNT 'UNKNOWN rRANBACTION TYPE'

Page 9: Programming Exercises - Home - Springer978-1-349-16258...Programming Exercises 1. Convert the flowchart shown in figure A.l into a BASIC program. 2. Convert the flowchart shown in

Appendix

0280 STOF' 0290 CLOSE 0300 GOSUB 0790 0310 U:T B=B+A 0320 F'RINT D21M2$; TABI811AI TABI30liB 0330 LET L•~L.+l

0340 GOTO 0:390 0350 GOSUF.< 0790 0360 LET B'=B····A 0370 PRINT D21M2$1 TABI20liAI TABI30liB 0380 l..ET L."•l .. +1 0390 READ FILE[3JoN2oD•T•A 0400 IF EOF13l THEN LET N2•999999 0410 IF N2•N1 THEN GOTO 0240 0420 FOR I=1 TO 20-L. 0430 PRINT 0440 NEXT I

PRINT 'BALANCE DUE'I TABI30liB F'FnNT WRITE Fil..E[2JoN1,NI•Al$,A2$oA3$oA4$oB READ FILEl1JoN1oN$•A1$oA21•A3$,~4$oB IF EOF11l THEN GOTO 0590 GOTO 0180 IF B•O THEN GOTO 0470 GOSUB <1640 LET L'=O OOTO 04~~0

REM REM ERROR ROUTINE F1EM PRINT 'UNKNOWN CUSTOMER"IN2 REM REM CLOSE DOWN AND STOF' REM CLOSE STOP REM S/R TO PRINT HEADINGS PRINT 'STATEMENT' PfaNT PRINT PRINT PRINT PRINT PRINT PRINT PRINT Pf,INT PRINT

I::::::::;:::;:::;::::~:::::~-:::::: II

'CUSTOMER"; TABI10liN$ TABI1011A1$ TAB I 101 IA2$ TAB I 101 IA3$ TAB I 101 IA4$

119

0450 0460 0470 0480 0490 0500 0510 ()520 05;30 0540 0550 05b0 0570 0580 0590 0600 0610 0620 0630 0640 0650 0660 0670 0680 0690 0700 0710 0720 0730 0740 075() 0760 CE' 0770 0780 0790 0800 0810 0820 0830 084() 0850 0860

PRINT 'DATE'; TABI8li"INVOICES'I TABI20li'F'AYMENTS"I TABI30li'BALAN

* 0010 oo:w 0030 0040 0050 0060 0070

PF1INT RETURN REM S/R TO UNPACK DATE DIM M$[36JoM2$[3J LET M$="JANFEBMARAF'RMAYJUN~JL.AUGSEF~CTNOVDEC' l..ET D2•D-100*INTID/t001 LET D""INT I D/l. 00 l l..ET M2•D-IOO*INTID/100l l..ET M2$•Mil3*M2-2o3*M2J F1ETURN

REM CRITICAL PATH ANALYSIS PROGRAM REM AUTHOR D. RUSSELL DIM AIUOOJ DIM Al100J,p[100J,S[100JoT[100J DIM Xl100J,E[100loL[100J PRINT "<12> PRINT 'CRITICAL.. PATH PACKAGE'

Program 1

0080 PRINT 'ENTER PREDECESSOR, SUCCESSOR AND TIME - NEGATIVE PRED. TERMJ NATES'

Page 10: Programming Exercises - Home - Springer978-1-349-16258...Programming Exercises 1. Convert the flowchart shown in figure A.l into a BASIC program. 2. Convert the flowchart shown in

120

0090 I...ET N""O 0100 LET N"N+:l 0110 INPUT PCNJ,S[Nl•TI..NJ 0120 IF PCNJ<O THEN GUTU 01~0 0130 LET ArNJ"'N 0140 noTo o:too 0150 LET N'"N-:l 0160 REM VALIDATE INPUT AND FIND FIRST AND LAST EVENTS

Continuing BASIC

0170 REM CHECK THAT PREDECESSORS OF ALL. ACTIVITIES ARE SUCI..SSORS 0180 REM IN OTHEI'~ ACTJVITU·.!·; Al...l.. I .. XCTF'T ONI:::, WHTCH WILL BE:: H·IF I" ll'~~::·r 0190 REM EVENT IN THE NETWORK 0200 f~FM SIMILAI'~LY, CHECI\ THAT AI... I... SI.JCCE!:>!:>Cm:·; AI': I' PF':F .. fift:F!:;sOI'<::i 0210 f~EM ELSEWHEI'n:: EXCEPT HIF~ Ti·l[ LAS r EVDH 0220 REM Fl•FIRST ACTIVITY• Ll=LAST• I1~ABORT FLAG 0230 PIUNT 'ACT,", 'l"'f~FD. ', 'SI.JC, ',"TIME'; TAF<(40!; "Mf-!:>!'ii~OF:• 0240 LET F :L "" ... :1. 0250 LET l..l"''"-:l 0260 LET Il•O 0270 FOR I•l TO N 0280 REM Bl=NO OF ERRORS FOUND FOR THI!:> ACTIVITY FRROR CODES JN Bl\1

0290 LET E<t=O 0300 FOR J•1 TO 10 0310 LET BCJJ•O 0320 NEXT ,J 0330 REM CHECK PREDECCESSOR 0340 FOR J•l TO N 0350 IF PCIJ•S[JJ THEN GOTO 0450 0360 NEXT . .1 0370 IF Fl•-1 THEN GOTO 0420 0380 IF Ft•PCIJ THEN GOTO 0450 0390 LET 81•81+1 0400 LET 8C1J•1 0410 GOTO 0450 0420 LET F1•P[IJ 0430 LET Bt•B1+1 0440 LET BC2l•1 0450 REM CHECK SUCESSOR 0460 FOR J•1 TO N 0470 IF S[IJ•PCJJ THEN GDTO 0570 0480 NEXT .J 0490 IF L:L=-1 THEN GOTO 0540 0500 IF StiJ•L1 THEN GOTO 0570 0510 LET B1•B1+l 0520 LET 8[3J•1 0530 GDTD O~i'JO 0540 LET L1•SCIJ 0550 LET Bl=Bl+l 0560 LET 8[4]•1 0570 REM PRINT ACTIVITY AND ANY ERROR MESSAGES 0580 IF Bt<>O THEN GOTO 0610 0590 PRINT A[J],p[JJ,StiJ,TtiJ; TAB(40);'NO ERRORS' 0600 GCJTO 0720 0610 FOR J•1 TO 4 06?0 TF Rr 11=0 l~FN GDTD 0710 ~I,:,JO (IN ~ . .J) fHEN fHl'TU 0/d('~ ()(,f,()y O,~~:·h•·· ,_',/oc• ,\··4(' r·rn·Nr (~I ., .. ,,.r·rr Jy()l TJy·rr J'IV rhn(•h>>;,nr··h'I:.UI'l·r !.::rH• l·l,':r:·. (.1n ··;rre,r·::.·.rw

~\·,: .• ~ 1 r•UTl1 t,)/:1 (I (< .. ·/·(' r·r:nNr ."'lr rr,.r.··r. r:J~:n:T]yf'l J~ ·y()[-:{(4(J)~.··r·r··:l·:ur.~;~;r.n( ~~~-~~·;rrtHn ,.:,,,. .. :, ''"'

(~/·"·'(' c:rlTn o:··1o M>IW I'I([Nl ,~[[Jd'l 1'.1•">1.. J, II .. H ·1Al·!i10H "'::tri:l:~.;~;cn:: H,Y. Nil l''l·,:i \II t'l"','''ll·'

o6~~·o tillTn O?.J(' o:·-"'00 r··r·..:i.NJ ,:j[ J. J¥/·'1. I l¥~·:rJ J,. ff 1'1; TAB(40J;; ''FII.JCI··S::;rH~ A~:iHIH1F'II f't'l nr: IJ1!;'·1 rvr:xr"

07:1.0 NF'\1 .I 0 ?·;>o Nl Xl l 0 :·'JO lii'H !31": f Ill' Ni.lUE l·li'<I'(,~Y

\l/4(' I r I M·,· O?:';(' I I'T XI" :1. J .. r.·r· ·1 :1

Page 11: Programming Exercises - Home - Springer978-1-349-16258...Programming Exercises 1. Convert the flowchart shown in figure A.l into a BASIC program. 2. Convert the flowchart shown in

Appendix

<)?.',•:> I I. l 'I ;• .I · :;1 J.''l \.'T-"'0 F~l(l I'Yrlf .. U~ 1 11~ ,.~11. t)L'l[l,'Jl"IL~:; ()···no r flf~ I. 1 ·Til u ()}<'(\ l·\t:J1 !'iF·.r:·· H 1'1·:1 lJI ;:1. •;:;ill\ IN I..J::;I OB<W I (lh' I .J. ltl M O!.JH' H Xf.IJ· .. I·'[ 'I I TTIE t'j t'otiTO <IHt•<l Otl~-~(\ r-Jr:·x r .J (l(J:I() F\1 ~~ UOT' tli.F\F.MtY 1'1\Eo:iH~r lll:JU fll I lo.T 0:341' I. I. r l·l•·M I I OW-,(' ITT \I WI 1'1.1.:1 OBi· I' I\L 11 ~:;t: 1 .. fl !:ilJtT o';~·;rm DN I. J 'i 1 Ofl:·•o I OF·: .. J. 1 ·r II M ()f3fh> Jf-" XI:JJ ~·;r: r··r rl-11: ~~ G('ll() ~'930 OB'~O urxr J ()<;>()()

O't:I.O o9:··o O'l::lO

F~I:M Nill lll.w:,'J1tY LET M Mil

!\!tit 111 1 1::;r

LLT Xfli'J•·.:·>fTJ NF'X I I

0940 m:.M C'UNU[ F\T l'I·~F::I'IF t'T!:i!·>lm:·; AN \I !'>I ItT:;~·;,·~~-::; 1 II I ·n IN 1 I· I\~; I t'l NIHtF M~fMY 0950 FOR J~1 TO M 0960 IF F1~XLJJ THEN GilTO 09~0 0970 NEXT ,.J 09BO ST!JF' 0990 LET F1"•,.J 1000 FOR J•1 TO M 1010 IF L1~XLJJ THEN GOTO 1040 1020 NEXT ,J 1030 STOP 1040 LET U.''J 1050 FOR I•1 TO N 1060 FOR J•1 TO M 1070 IF PLIJ•XLJJ THEN GOlD 1100 10fJO NEXT J 1090 STOP 1100 LET P[IJ•J 1110 FOR J•1 TO M 1 :!20 11.30 1140 11~i0

1160 1170 11f10 1190 1 ~!00 1210 1220 1230 1?40 1250 1260 1270

IF SCIJ•XLJJ THEN GOTO 1.150 NEXT .J STOP LET !3[I ]'',.I

NEXT I REM MAIN LOOP FOR EARLY START' F(EM Y IS ITEf~ATION CDUNTE'f(, l J'3 !:iWM' REM SET UP FIRST TO o, ALL REST TO .. 1 Frm I"•:l TO M

LET ELIJ•, .. ··l NEXT I LET ELFlJ•"O I. ET y,,O LET z,,O FC:m I"'1 TO N

LET P:l,•PLIJ 1280 LET S1=SLIJ 1290 IF ELPlJ=-1 THEN GOTO 1340 1300 LET E1=ELPlJ+TCil 1310 IF E1<•E[SlJ THEN GOTO 1340 1320 LET ECSI.JmEt LBO LET l=l+1. 1340 NEXT I 1350 IF Z=O THEN GOTO 1390 1:l60 LET Y•=Y+:l 1370 IF Y<=N+2 THEN GOTO 12~0 13fJO PRINT 'TOO MANY ITERATIONS' 1390 f~EM 1400 REM LOOP FOR LATE STARTS 14-10 FOR 1'':1. TO N 1420 LET LLIJm-1 14:30 NEXT I 1440 LET L[L1J=E[L1l l4!:i0 LET y,,O 1460 LET z,,O 1470 FOR I~N fO 1 STEP -1

I LMi

121

Page 12: Programming Exercises - Home - Springer978-1-349-16258...Programming Exercises 1. Convert the flowchart shown in figure A.l into a BASIC program. 2. Convert the flowchart shown in

122 Continuing BASIC

1480 LET P1•PCIJ 1490 LET S!•S[IJ 1500 IF LlS1l•-1 THEN GOTO 1560 1510 LET L2•LCS1J-T[IJ 1520 IF L[P1l=-1 l~EN GOTD 1540 1530 IF L2>•LCP1J THEN GOlD 1560 1540 LET L[P1J•L2 1550 LET Z•Z+1 1560 NEXT I 1570 IF Z=O l~EN GOTO 1610 1580 LET Y•"Y+l 1590 IF Y<•Nt2 l~EN GOTO 1460 1600 PRINT 'TOO MANY ITERATJONS' 1610 fiFM i''I'UN'l I·,Ff>I.II...T':i 16:!0 PI'UNT "AI''TJ:\Jl li l'ilil'·:llTHJN :Ltdo f•f([Nl ' I -.1 1640 LET AS•" Ill Ill tllll~t :1.6~-~0 r·f~INT'

1 660 f t'll'< T ·'·I TO N 16 .. !0 I f: 1 1"1"·1·'1 l .I 1 61'10 I.I:::T ~;·l·•·:'il. ·1·.1

· ·· ·-FMa .. lE!'i.l···· fiTAf(f FINISH

·~ •l•t·H t •t.t •A,I•

··· ...... [ .. A TEST···· STAI'n f'INHiH' tiH•t II•Hi'

1690 l··fnNr IJ::;JNl.• n:I:•XI'f'.I:·I·\LS:IJ,l['[]•FLI'·'.J],,F[F"lJtTJ:'I:.I.t .. J:Hl.'I···TT.f.J,II ~31:1 1./00 NF XT I. ·1 ? 1 o ~·; rnr·

* Program 2

"'·'•!•• !'I ;·1 •H lllliNl !t!IJ i•f;:ti!H\(Ji1~ U::li:NG r~CCOUNr-!Ni·/ i .. :Ul1L') · l~;. \'• ~\] h ,·1: If I It :i-.o II. l<:l.l:;:;l ·I I ···.l '· 1 ~<·: r1 ! 111-1:: n1-:1 !1--'l'llf' kill I! ,:)1\! ,!)ccutrNr.I.Nt: cnrn ~ 1 rr h:-> .lUI loJ:>Il·'ll 11 !· 1 '

J'.·r ,, 1:111

, ''·, . , ' ! ~I n , · , ·., 1 \I ~~ 1 u! · 11 1 ,.);~ 11 · ;,w r r ·t1'·1 ~:I·

',! ~) : ~ ' I i ·: I t1 '' ~ 1 ' • • ·, 1 ! : r 1 , 1 ~ • 1 11 1 1 • 1 • 1 ' ~ 1 ~) 1

:.'ll"t'i ! ! 1 f! '' I I t • i i !: '111 t I ! .I • ,) I ,, I :·:.1 UH'!! r ! I,.,) I ._,.I : 1! !··'I fl I I NT! 1·111! f:l I" I I. f1 I~~ I II ,·,u t·• I •lt' I lth' ! · 1 Ill /J ·.•J:q• ' 1 l'l I I ,··r IIH"N lillfll ·,1/:·lc ~ '; I ' .. I iJ I ' I I •.II 'il tllt!ll t' · ·~,_1

,. "I'·'~-~ i" t lh' ,I I 1 i"l j,l ,, . 'I 'P I I·· ) l' [ . I I •.•.'! <' I I CI .. J I·· 1: I ~'.'. '1. 1 I I 1·· I. ·:. \ U .': \() I [ 1<. :::,~~I . I I ().~·h' 1 r (d ··' 1 .. ,.,., 0 .:~.i(l I 1·. ,:H ·<\ iL\',,J Nr: ·=CT .I o;.:.'<l 1 .. 1.'1 i'I·•Ut I 0.-.'BI' I L f i'LNJ .. =t·:r o::·:, L.l r f.llt··• 1 ··ti'J oJOO [1- f. I : :·'·''?'.;."/'~' TIIF"N UUTO O:L:.~o

\):J:lO fiF:M o·;·:.~o 1-.:1:.N r·r~ .1 ~~ r r·H,(.,· ,c \LBO fiFM \),140 I·U·.M SF' I l"'l!lNTt: I··· 11! » i'f'tl'(f OF /...I~:iT ANn GF'I FIRST TTFM 03~.;() l..E'T 1"'·<1 0360 l.ET Ct~FII"'J

0370 LE:T A:J. ... •f.l[/·'J 0380 kEM SFf TOTALS 10 ZERU 03'10 LL r ~. .... o 040(' l..F'l Y•"O 0410 Pf<IN'T 0420 PRINT 'CDOF'o'AMOUi'il'

Page 13: Programming Exercises - Home - Springer978-1-349-16258...Programming Exercises 1. Convert the flowchart shown in figure A.l into a BASIC program. 2. Convert the flowchart shown in

Appendix

043o t.E:T x"o 0440 REM PRINT TFEM 0450 PfnNT C:t.Al 0460 U:.T X"'XIA1 0470 m=M REMEMHER LAST IFEM•A AND GET NEXT ONE 04BO LET L:l""C:l 0490 LFT f''"'f''·l :l 0500 LET Cl"CLI"'J 0510 LET A:l""Al:PJ 0520 REM SEE IF SAME CODE AS l.ASF ONE• IF NOf PRINT TOTALISl 05:30 IF L:l"•Cl THEN bOTP 04:'i<) 0540 PRINT 'CODE '11..11' TOFAL'oX O~i~iO LET Y""YIX 0560 IF INTIC1/1001•INTIL1/:l00l IHFN bOFO 0430 0570 F'RINT 0580 PRINT '' ' ...... 0590 PRINT 'SUB TOTAL'••Y 0600 PRINT 0610 PFnNT 0620 l.E:T Z•~Z I Y 0630 IF C1~>99999 THEN GOTO 0400 0640 PfnNT OMiO PRINT 0660 PRINT 'GRAND TOlAL"•••Z

123

* Program 3

0010 REM FINDS PRESENT VALUES OF SE~IES OF CASH FLOWS 0020 REM AUTHOR D. RUSSELL 0030 DIM Y[100J,A[100J,C$CIOOJ 0040 PRINT 0050 PRINT 'PROJECT NAME'I 0060 INPUT C$ 0070 PRINT 'INTEREST RATE IZI'I 0080 INPUT R 0090 LET R•R/100 0100 f~EM

0110 REM READ IN CASH FLOWS 0120 REM 0130 LET N•1 0140 0150 0160 0170 0180 0190 0200 0210 0220 0230 0240 0250 0260 0270 0280 0290 0300 0310 0320 0330 0340 0350 0360 0370 0380 0390 0400 0410 0420 0430 0440

PRINT 'INPUT CASH FLOWS !NEGATIVE YEAR TERMINATES' PRINT 'YEAR' I INPUT YCNJI IF Y[NJ<O THEN GOTO 0220 PRINT ' AMCJUNT'I INPUT ACNJ LET N=N+1 GOTCJ 0150 LET N=N-1 REM REM FIND PROJECT LENGTH IN X REM LET X=-·100000 FOR I=1 TO N

IF YCIJ>X THEN LET X•Y[IJ NEXT I REM REM PRINT HEADINGS REM PRINT PRINT PRINT 'PROJECT APPRAISAL USING PRESENT VALUES' PRINT '======•==•••===•=••=•==••=••=•=••=•••=' PRINT PRINT C$ FOR !=1 TO LENIC$1

PRINT '•=' I NEXT I PRINT PRINT PRINT ••'DISCOUNT'o'DISCOUNTED'

Page 14: Programming Exercises - Home - Springer978-1-349-16258...Programming Exercises 1. Convert the flowchart shown in figure A.l into a BASIC program. 2. Convert the flowchart shown in

124 Continuing BASIC

PRINT 'YEAR"•'AMOUNT'•'~ACfUR'•'AMOUNT' PFliNT F~EM

04~l0

0460 0470 0480 0490 0~\00 0~)10

0520 o:'.>3o o::;4o 0~.)50

o:';6o 0~.\.70

o:"iBO

F~EM CYCI...E OVU~ l'II.JI~,;·r J CJN OF PI'W..JECT, W!.WK 1: NG 0/JT 1'·'/'~E!·>/'::NT W11.11F:S I'~ EM I...LT T""O n:m z .. ,o Til -x

/... E T I •·1. I ( :l I /·~ ) ''l FOf~ l•·"·i I'D N

IF Ylll~~~ THEN GOTO 0570 PI'([NT z,Af I lol •I.*AI: I :1

LFT T•,TH*AIIJ NEXT I

NFXT l 0~',90 l''l'n NT 0600 f''/·~ J N r 'NE r i"f~E!:>ENT tJAL.UE flf' F'I'W.IEC r" , ·r

*

OO:!.O !'~EM 0030 I'~E.H

0040 f':EM oo:•;o F~F:t1

0060 !'~EM

0070 F'E:M oono REM 0090 !'~EM

O:l<)O f~EM

Ali'! HUI'~ !I, /·a.l':ibl:'./...1 .. I NCI unr::.: l,J,"/ .. TIm r T liN /If i1Nl.l V,A, r, Ct1/.CIJ/.J.ll !tiN

THFd:·r /"'FW!.IUC T' C'Cil'IF:!i; :1. !3ANfi .. , !'il·lf.l/~1· !311N)'I :1 cm,•i'JFt.

lN/''U T, /ll.'it::t'JIIN I·; I Ill' I, d'li/ : :tn:: I"

Program 4

O:l :1.0 I'~E:H liSE!( CMJ INF'II'T ";r·•JI.T<M. ·rru,N:·;ACT ./. fJfJ:, F I'Jii.' I" (II :H f'.l /.':I 11111.1' ,·,r·!/1 0:1.20 f~EM THF F'I'WI<I'.'AM W II .. I.. tJCI:UMl.ll ,y['('' ,'\HOUr·! 1" 1 .: 1.11' F,\1 :·1-1 I'' ill :\11 J(' I (II{U

O.t:lO F~EM I''IUNTb AN frWO:UT 1"1.11'•' HH: f'ClT,~I •'•i1UIINI': 0140 F~EI1

01 ~.\0 I'( EM 0160 REM !3FT PR./.CES 0:1. 70 F~EM ()100 L.FT A==·:?.!:~

0190 I F'T I< .... . 020(1 I F'\ C ... ~.', 7~.\ o2:1.o mM 0220 REM UEl RATF UF U,A.l. o:•3o r~F:M

0240 LET 'J""fl 02:'iO !'~EM

0:~60 f'i'UN 1' "CW'iTC!MEJ< NUMHEI'' (EN T L:l'·: 'l'I'I•N I. r· IW i1t'H.:I': r:~l.l!'illlMI F·~; •" 0270 INPUT 1\ 0280 IF K•999Y9 THEN GUT() 0890 o;.:?o u::T x""o 0300 L.ET y,,O o;~1o u:T z,,o 0320 f''I'U NT 'ENTEF~ PI'WDUCT CODE AND I·IUMH J T i" 03.30 PfnNT 'ENTl.F~ PI'WIH.IC r CODE 9'!'?9'? 1.1- I hi r·lill·:l 1'/'(,,,NI'i,C,i'lli·l;;·: I llfi: I HI:,:. I 'U STOMEJ~) II

O:l40 INF'UT p,[l 0350 IF P•9'l999 THE.N GOTO 0620 0360 !'~EM 0370 REM VA/...I!IATE PRODUCT COPE 03ll0 F~EM

0390 IF P<1 THEN GOTO 04:1.0 0400 IF P<•3 THEN GOTO 0430 0410 f"RINT "INVA/...1!1 PRODUCT CODE' 0420 GOTCJ 0;540 0430 IF O<O THEN GOTCJ 0450 0440 IF 0<200 l~EN GOTO 0500 0450 PRINT 'QUANTITY OUlR.I.DE PERMITTED RANQE" 0460 CiCJTCI 0340 0470 f~EM

0480 REM ACCUMULATE AMCJUNTS 0490 f~EM

0500 IF P=1 THEN GOTCJ 0530 0510 If P•2 THEN GCJfO 0550

Page 15: Programming Exercises - Home - Springer978-1-349-16258...Programming Exercises 1. Convert the flowchart shown in figure A.l into a BASIC program. 2. Convert the flowchart shown in

Appendix

O~i20 1 F 1' .. ,<1 THE:N GIH 0 O~'i'lO

Oci30 l 1: T' X::kHl 0~',40 GCJTO 0340 o:~i!~iO LET Y::::Y+O 0~')60 GDTO 0340 Oo)JO LLT Z:•Z·H1 O~"iflO UOTO 0340 O~:i'?O 1·!1-.:M 0600 I·!I:'"M l'·'f!] NT' TNVIllCL 0<'> 1.0 i'!I::M o<>::~o r··l'nNT 0630 RLM !NVDIC[ FOR CUSTOMLR NO,'iK 0640 F''f!:UH 06~";0 F'I'!INT "IIr.:sc.:l'!li-''T'ION", "ClUANTJ.TY", "IINl'J f''i'(TCI.:", "AMUIINI" 0660 F'I'!INT 0670 I..TT f· '"'X*A 0680 PRINT 'SAND'•X•A•F 06'?0 I. CT (j:::Y*l< 0700 PRINT 'SHARP SAND'oYoB•G 0710 LET ll::l*C OJ?O PRINT "GRAV(~L_Mw!vC¥H

0730 I...ET T:~f'·Hi+H

0740 i'.:EM CALCULATE:: I'IJ'-ii'OUNT IJO% u:· Cll'':lil:·l< OWl·: •1:.1'>0: 0750 IF T(100 THlN GOlD 0800 0760 LET D'"', U T' 07'70 PI'UNT 0/flO PRTNl 'DISCOUNT',,,[! 0790 l.ET T:T··D 0800 REM CALCULTAT'E V,A,T'. 0810 LET U'"V*T/100 0 fl2 0 FRI'N'T 0830 PRINT 'V,A,T,"oooU 0840 I..ET ·r :THI Ofl50 PfUNT 0860 PRINT 'TOTAL"•••T Ofl/0 PI'!TNT OflfiO GOTO 0:-.'·60 Ofl90 STOP 0900 END

125

* Program 5

0010 RFM li"XT JUSTJF.ICAllON PROGRAM l)O:?O I'! EM D, r.:IYiSFI..L FT:B, 19/S OOJO DIM AIL1UO:IrlfiL100JoC$[100JoDSL100J 0040 DIM T;/:r:·.:o'l OO~.W HFM fH::.Nf:R(lf'F N,~ME.: or:· l" .. ll..E WJTH l .. .f'.TTFI'~ IN FI'WNT CHVING TEf(MJANl.. N!l 0060 I. E::T 1\;t>::: 'A!!C:I'ILFCJHJ ,.JI<I .. MNOP!.li'(STI.Ii,!WXY/" 0070 I. LT T$:::A$LSY::i ( 4), ~:iY~l ( 4) 'J • "H':XT" OOSO PRINT "lHlS PROGRAM ALLOWS YOU TU lYPE TN A PIECE OF TEXT," 00'10 PI'([ NT "IT WIL.I .. TI·H::N AD . .JU,;T EACH /.IN!:. rll DCC:Uf"Y ONE COMPLETE /.. lNf:, I r·. 01.00 PI'UNT 'NECE~iSAF.:Y BY MOVING WO/~D!:> FROM CINE I...'INE TO ANOHIF:f~'

0110 PRTNT' 'IT WILL THEN SPACE OUT FACH I..JNE TO OCCUPY A CCJMPLCTE" 0120 PRJNT 'LINE EXACTlY A PROCESS KNOWN AS JUSTIFTCAT]ON" 0130 PRINT 'PLEASE TYPE IN YOUR PIECE OF TEXT' 0140 F'RINT 'PRESS 'RETURN' AT THE END OF EACH LINE" 0150 PRINT "YOU MUST START EACH LINE WITH !.lUOTATION MARKS" 0160 PRINT 'WHEN YOU HAVE FINISHED PRESS THE K~Y MARKED 'ESCAPE'' 0170 DF'EN FII..EL:l.:lJoT$ 0180 ON F:SC T~iEN GOTO 0290 0190 ON ERR THEN GOTCJ 0230 0200 INPUT A$ 0210 WRITE FILELIJoA$ 02:'0 GDTO O:WO 0230 IF SYS(/);)46 THEN GCJTD 0260 0240 Pf(INT 'YOU FDI'WOT TO I'·'UT 01./CJTES AT THE f"I'WNT 02~;0 GOTO 0200 0260 PRINT 'SORRY AN FRRDR HAS DCCURFD' 0:!70 CU.J!:iF O:!BO STDF'

PL..EAfiF m:TYF'F'

Page 16: Programming Exercises - Home - Springer978-1-349-16258...Programming Exercises 1. Convert the flowchart shown in figure A.l into a BASIC program. 2. Convert the flowchart shown in

126

0290 CLOSE 0300 ON ERR TfEN GOTO 0490 031<)· LET z,=O 0320 PRINT 0330 PRINT 0340 GOSUE< O:BO 0350 PRINT 0360 PFUNT 0370 PRINT 'WOULD YOU LIKE A COPY ON THE LINE PRINTER'; 0380 INPUT A$ 0390 IF A$='NO' THEN OOTO 0430 0400 OPEN FILEC2o1lr'$LP1" 0410 LET Z=1 0420 GOSUI< 0580

Continuing BASIC

0430 PRINT 'DO YOU WANT fO fRY AGAlN WITH A DIFFERENT LINE WIDTH'; 0440 INPUT A$ 0450 IF A$='YES' l~EN GOTO 0310 0460 CLOSE 0470 DELETE T$ 0480 STOP 0490 IF SYSC7J~:10 THEN GOTO 0520 0500 PRINT 'SORRY l.INE PRINTER IN USE - TRY AGAIN LATER' o~; 10 GDTO 043() 0520 GOTO 0260 0530 DN ESC THEN GOTCl 1360 0540 PR1NT 'HOW MANY CHARACTERS DO YDU WANT PER LINE'; 0550 INPUT L 0560 F·RINT 0570 PRINT 0580 IF L<=SYS C 9) THEN GDTIJ O'b10 0590 PRINT 'BUT THIS IS WIDER THATN THE PAGE ON THE TERMINAL' 060() GDTO 0!540 0610 ClF~N FILEClr3loT$ 0620 LET C$,='' 0630 READ FilEC1JoA$ 0640 IF EOFC1J THEN GUTO 0930 0650 LET K'= 1 0660 GDSUB 0950 0670 IF B$·~· THEN 0010 Ob30 0680 IF B$=' ' THEN GOTO 0660 0690 LET C$,=B$ 0700 GOSUB 09~i0

0710 !F B$•"' THEN GOTO 0850 0720 IF B$=' ' THEN GDTO 0700 07:!0 IF LEN ( C$ J +l .. EN (F.<$ H L· L THEN l·iO fO (!/70 0740 LET [I$•C$•' 'oD$ 0750 LET C$,=[1$ 0760 GOTO 0700 0770 REM JUSTIFY liNE AND F~INT 0780 GOSUB 1090 0790 IF Z•1 THEN GOTO 0820 0800 PRINT C$ 081 o GOTO o8:so 0820 PRINT FILEC2JoC$ 0830 LET C$=B$ 0840 GOTO 0700 0850 READ FILEL1JrA$ 0860 IF EOFC1) THEN GOTO 0890 0870 LET K=l 0880 GOTO 0700 0890 IF Z=1 THEN GOTO 0920 0900 PRINT C$ 0910 GOTO 0930 0920 PRHH FilEUJoC$ 0930 CLOSE 0940 RETURN 0950 REM S/R TO GET WORD FROM STRING A$ INTO B$ - SCAN STARTS AT 0960 REM POSITION K OF STRING 0970 LET£<$"'' 0980 LET J=1 0990 IF K>LENCA$) THEN GOTO 1080 1000 IF A$[KrKJ=' ' THEN GOTO 1050

Page 17: Programming Exercises - Home - Springer978-1-349-16258...Programming Exercises 1. Convert the flowchart shown in figure A.l into a BASIC program. 2. Convert the flowchart shown in

Appendix

1010 LET B$EJoJJ•AIEKoKJ 1020 LET ,.J=Jt1 1030 LET K=K+1 1040 GOlD 0990 10~i0 LET K=K+l 1060 IF B$() THEN GOTO 1080 1070 LET B$=' ' 1080 RETURN 1090 REM S/R TO JUSTIFY LINE OF TEXT IN C$ 1100 REM L IS l~E LENGTH OF THE LINE REQUIRED 1110 REM GO THROUGH ADDING SPACES AFTER MJNCTUATION 1120 IF LENIC$l•L THEN GOTO 1340 1130 LET I•~1

1140 IF C$L..IoiJ•'•' THEN GOTO 1190 1150 IF CIL..Ioll='•' THEN GOTO 1190 1160 IF C$l..IoiJ=•;• THEN GOTO 1190 1170 IF C$l..IoiJ•':' THEN GOTO 1190 11f.JO (i(JTO 12~'0

1190 LET DI•C$l..1oiJo' 'oC$l..It1J 1200 LET C$,,[1$ 1210 IF LENIC$1>•L THEN GOTO 1340 1220 LET I'-'1+1 1230 IF I<LENIC$1 THEN GOTO 1140 1240 REM NOW GO THROUGH ADDING SPACES AFTER SPACES 12!50 LET I"':/. 1260 IF CIL..I•IJ~>· ' THEN GOTO 1310 1270 LET D$=C$L..1o!Jo" 'oC$li+11 1280 LET C'~""[l$ 1290 I .. ET l•'-'1+1 1300 IF LENCCII~=L THEN GOTO 1340 1310 LE:T l"l·f1 1320 IF I<LENIC.I THEN GOTO 1260 1330 IF I .. ENCI>~ > ··t.. Tl·lf:'N [lilT('! '1150 :l:l40 RET!.HW 1350 CI...DSE 1360 STIJI"'

127

* Program 6

i}t,'l I '·' ('l('l,,;l)

t•~) '""' ~) .,'lt,).;h)

' \ .. 1! ·; ~ 1

I·Cf.h I":H: I<I .. :M I·'' I 1·1 f<l' f-1

LlnmY TYI'~E: SIMULATION !·:I hUL ATLS L.OI~fiY WITH 6 WHEELS WHERE TYI~ES FA I u; 1\ r l'~o'·iiWfll·o ~; 1'!\'1 J!:d :u:::; OF TYRE LIFE ARE KNOWN

n HIJI'(~:;r 1 YI'~E. COSTS :lCIO POUNDS TO REPAIR IN Mrllll ICIN fill.. rYm::s ARE 1\EF'LACED AT 1\EGI.JI .. AR lNTH~'!r;l.!; oo I c\ . 1.1

·., T nr· , ;, :·.; c1 I·' UtJN fJ ~:~

''"'"' I··: I h I 'I f.: ::;r I' DF 6 '·'"·.·c, 1 .. 1 il IHI •:IMI.fl oYI I:UN J!'> t:rE!HGNFTr TIJ FINn THE I:II"'Tli11./M INlFI'I~'r'•l ,.:1 1:,11·.1: •'

).'(rlf'i I:NI: •.'t'•l·;•,.'~ l\1 f·i I f f\1" l~Ht:-.Nt"iF"S

00(>'•) 1-:1 n niJ llllH-: lr f PU!:)~:;r:·l .. L I FD JCJ},~)

i' I.(!() Jrl C1 I\ I ,;.r) I• t..:r. .lO I, I'T.lO"I ~;I "I() f~{iN\It'lMI !I": •.'I :·'() f,TAO i'J. •.' I ,\\1 F CW I .. , l r 1:1 N 0110 lo'FMI MT.I,!'<I.I-:1 o.l!',o rn··r r Olc.O l'l',.lrn 0 I/(\ 1·r;·1 Nl ,)HI\1 f'l·~li<l "I fiiWY T'YI~E Sli11./I..ATION" ill YO f'll IN I 0/00 f'ICI'rH (J:~to ':'l·([N r o:.>::•t) f'l·l 1.11'1 'WHAT T!:> TN .. II': .. I'IVAL. BETWEFIJ l'llli!TINE TYI'~E: CHt~Ni'lf'S';

0::!.30 HW!ll l o:'40 l'·'l'ntn 0~'~)() r·HHH "r·f~F:!3!'> E!:>CAI''E. HI STDF' F'ROi.mAM' 0260 ON fSC lHEN GUlU 0870 0270 F'l'nNT o::>EJ() PF~INT

0290 PRHH

Page 18: Programming Exercises - Home - Springer978-1-349-16258...Programming Exercises 1. Convert the flowchart shown in figure A.l into a BASIC program. 2. Convert the flowchart shown in

128 Continuing BASIC

0:300 FK[NT " ·· ··· .. ,.,, .. , .. ,.,, .. ,,., I I 1;: I·

0:310 PFUNT " L:I.UCI·~

COST" 0320 REM SET QOCK TO ?ERO o;no LET C<,() 0340 J'(EM !:iET IJF' J NJTH1I.. TYI'(E~:;

0350 FUR K=1 TIJ 6 0360 GOSIJB 0750 0370 LET C[KJ=JNI<Ml 03~10 Nt::Xr 1\ 0390 DIM F<~[72J

~ .~ 1 ."! f.,

0400 LET FS•'tllttll llltl ttltl IIIII ltlfl IIIII IIIII' 0410 PRINT IJSJNG F$oOoCI1loCL2JoC[3loCL4JoC[5JoCL6J 0420 LET f'•,,z 0430 f(EM FIND TYRE: WITH !:iMM .. LEST LIFE: 0440 LLT X=1E:+08 0450 FOR I=1 TO 6 0460 IF CLIJ>X !'HEN GOTO 0490 0470 LET !.l<<I 0480 LET X•~LlJ

0490 NEXT I 0500 REM SEE IF fYRE WILL BURST' BEFORE NEXT SERVICE 0510 IF X>=P THEN GOIO 0630 0520 REM FIT NEW TYRE 0530 IJOSUF< 07Ci0 0540 LET C,,X 0550 PRINT USING 'lllllll',c;; 0560 PRINT I'Af<<10t7*<0-tll; 0570 PRINT USING 'lllll"oM; 0580 LET C[!.lJ•C[QJtM

PRINT TABC53);•BLJRST •; PRINT USING 'tltll'o350 LET T•T+350 GIJTIJ 0430 REM ROUTINE I'll CHANGE ALL lYRES LET C<•F' PRINT USTNG "1111111 •,c;; FIJF( ~·'1 TCI /,

G Cl S Llf< 0 7 ~.)() PRINT USJNG 'ltllt ',M; LEI' c:r:r;::t,~C+M

NEXT I\ F'fUNT ' f((JIHTNE' •;J(I()

LEI' f'TI<lOO LET F'"'P+Z GOTO 04:l0 LET J•INT<100*(RND(0))) FDF~ l>, l TD N

IF . .J<AL ['] HIF'N OUTO 0790 NEXT T

0590 0600 0610 0620 0630 0640 06~i0 0660 06'70 06EIO 0690 0700 0710 ono 0730 0740 0750 0760 0770 0780 0'790 0800 OEI10 OB20 0830 0840 OB~iO

0860 0870 OB80 08'?0 0900 0910

LET M•F<t::I· .. ·:IJI·(,I·-·Al'J .... lJ!/(AJ::J]·-Ar:J-·l"I)*<BI .. TJ .. ·t<[J .. lll f(ETI.IF':N DATA 6 DATA o,5000,15,10000,35v1~000~8C>,~0000~9~y2500Clv100~~(l(\()0

PFUNT "C:L.[)(;~; '; C:; "F' ";I'' FIJf( I«<·:J. TCI 6

F'IUNT HC:t::I"I NEXT I PFntn Pf~INT

FK[NT I.J::iiND 'TilT AI.. r:DS I PRINT USING 'I'OTAL MILFS PRINT USTNG "COGf PFR MILE

0920 STOP

*

t,t,~<~<t,n. • •. , ,g,,,~,,..,, •• c: t<,<tUI'' ollt"

[t)ENT

Program 7

Page 19: Programming Exercises - Home - Springer978-1-349-16258...Programming Exercises 1. Convert the flowchart shown in figure A.l into a BASIC program. 2. Convert the flowchart shown in

Answers to Flowcharting Exercises

Yes No

Glossary

X- Integer input Y -Number tested for remainder

Problem l(a)

Page 20: Programming Exercises - Home - Springer978-1-349-16258...Programming Exercises 1. Convert the flowchart shown in figure A.l into a BASIC program. 2. Convert the flowchart shown in

130

No

"NON INTEGER INPUT"

Continuing BASIC

INT(X) gives us the integer, or whole number, part of X

Glossary

X-lnteger input Y -Number tested for remainder

Problem 1 (b)

Page 21: Programming Exercises - Home - Springer978-1-349-16258...Programming Exercises 1. Convert the flowchart shown in figure A.l into a BASIC program. 2. Convert the flowchart shown in

Answers to Flowcharting Exercises

L+-L+1

I+-I+1

J+-J+1

K+-K+1

Problem 2

Glossary

X-lnput number

I, J, K keep the totals of each group L is the total of all those which are greater than 30 or less than 1 -1 acts as a "trigger" to indicate the end of the I ist

131

Page 22: Programming Exercises - Home - Springer978-1-349-16258...Programming Exercises 1. Convert the flowchart shown in figure A.l into a BASIC program. 2. Convert the flowchart shown in

132

Glossary

X-Used to count the number of times the loop is executed

Glossary

Problem 3

A, 8-Successive numbers in Fibonacci series

!-Index to number of elements of series printed

No

Problem 4

Continuing BASIC

Page 23: Programming Exercises - Home - Springer978-1-349-16258...Programming Exercises 1. Convert the flowchart shown in figure A.l into a BASIC program. 2. Convert the flowchart shown in

Answers to Flowcharting Exercises

Glossary

P-Present pay (£)

No

Problem 5

133

Page 24: Programming Exercises - Home - Springer978-1-349-16258...Programming Exercises 1. Convert the flowchart shown in figure A.l into a BASIC program. 2. Convert the flowchart shown in

134

The resu Its are held in a table, L, with N rows and 6 columns. Each column records for one team the number of matches played, home wins, away wins, goals for, goals against and points.

L(T1, 2) ~ L(T1, 2) + 1

L(T1,6)~

L(T1, 6) + 2

L(T1, 1) ~ L(T1, 1) + 1 L(T2, 1) +­L(T2, 1) + 1

L(T1, 4) ~ L(T1, 4) + G1 L(T2,4) ~ L(T2, 4) + G2

L(T1, 5) ~ L(T1, 5) + G2 L(T2, 5) ~ L(T2, 5) + G1

Problem 6

Continuing BASIC

Glossary

N-Number of teams in league T1-Number of home team T2-Number of away team G 1-Goals scored by home team G2-Goals scored by away team

L-League table

I, L(I, 1), L(I, 2), L(I, 3) L(I, 4), L(I, 5) L(I, 6)

L(T1, 6) ~ L(T1, 6) + 1 L(T2,6) +­L(T2, 6) + 1

No L(T2, 3) ~ L(T2, 3) + 1

L(T2, 6) +­L(T2, 6) + 2

Page 25: Programming Exercises - Home - Springer978-1-349-16258...Programming Exercises 1. Convert the flowchart shown in figure A.l into a BASIC program. 2. Convert the flowchart shown in

Answers to Flowcharting Exercises

JNT(P) means the integer, or whole number, part of P

Glossary

M-Sum of money (£, p) T-Total number of coins

of 10p or 2p denomination P-Working s~nre

Problem 7

135

Page 26: Programming Exercises - Home - Springer978-1-349-16258...Programming Exercises 1. Convert the flowchart shown in figure A.l into a BASIC program. 2. Convert the flowchart shown in

136 Continuing BASIC

Glossary

M-Two-dimensional array of marks I-} J- Indexes to array entries

L-Largest entry in columns 1-6 K-lndex to column with highest deviation T-Total of each row in array M

Page 27: Programming Exercises - Home - Springer978-1-349-16258...Programming Exercises 1. Convert the flowchart shown in figure A.l into a BASIC program. 2. Convert the flowchart shown in

Answers to Flowcharting Exercises

L +- M(I, J) -M(I, 7) K <-- J

L +- M(l, 1) -M(I, 7) K +- 1

No

Problem 8

137

No

M(K, (M(K, 9))), K, M(K, 9)

Page 28: Programming Exercises - Home - Springer978-1-349-16258...Programming Exercises 1. Convert the flowchart shown in figure A.l into a BASIC program. 2. Convert the flowchart shown in

Index

Access vector 61 Algorithm 2, 3 Arithmetic operations 30 ASCII codes 44

Binary search 93 Bubble sort 43

Commands 31 Concatenation of strings 60

End-of-ftle marker 91, 103

Flag 13 Flowchart 4

conversion to BASIC 40 Functions 30

GIGO 10

Indexed list 19 Iterative process 15

LEN function 51 Logical statements 36

Matrix statements 63 Merging 23, 79, 96, 106

Multiple branching statements 32, 75

Null string 51

Pi, .value of 35 Polyphase sort 100, 110 Procedure 3 Process 3

Random access files 85, 91, 105 Random numbers 3 7 Records 85 Recursive subroutines 77 Relational operators 30

Serial files 85, 90, 103 Statements in BASIC 30 String sort 60 Strings 48 Subroutines 21 Subroutines in BASIC 72 Substrings 51

Trace table 5, 16

Updating offtles 87, 96, 106

VAL function 57

139