To speed up the initial display of the chart, I have included a parameter "res" to indicate the resolution of the pie chart. If res is large, 0.2, say, the resulting chart may not be very accurate. Therefore once the chart has been drawn there is the option of refining the picture.
It would also be interesting to plot the data on the screen. That is left as an exercise for the reader.
COMMANDS
Key in program and type RUN.
Enter data as required.
100 REM Program P53 - Pie Chart 110 DIM item(20) 120 MODE 6 130 140 PRINT "This program depicts a PIE chart based" 150 PRINT "on data which you enter, one at a time" 160 PRINT "Different areas of the pie chart are" 170 PRINT "painted in different colours, thus we" 180 PRINT "will plot the data in MODE 5" 190 200 PRINT '''"Enter the data one item at a time" 210 PRINT "Enter 0 to finish" 220 230 I=0 240 REPEAT 250 I=I+1 260 INPUT item(I) 270 total=total+item(I) 280 PRINT CHR$(11);SPC(40);CHR$(11); 290 REM CHR$(11)=up 300 UNTIL item(I)=0 310 INPUT "Resolution in radians",res 320 330 REPEAT 340 MODE 5 350 VDU 19,0,7;0;19,3,4;0; 360 PRINT ''SPC(5);"Pie CHART" 370 380 IF (I-1) MOD 3 =1 THEN flag=-1 ELSE flag=0 390 IF flag=-1 THEN last_item=I-2 ELSE last_item=I-1 400 410 last_angle=0:angle=0 420 MOVE 639,511:MOVE 1039,511 430 440 FOR J=1 TO last_item 450 GCOL 0,(J MOD 3)+1 460 PROCsegment 470 last_angle=last_angle+2*PI*item(J)/total 480 NEXT J 490 500 IF flag=-1 THEN GCOL0,3:PROCsegment 510 PRINT TAB(0,30);"New resolution (Y/N)"; 520 INPUT ans$ 530 IF ans$="Y" OR ans$="y" THEN INPUT "Resolution",res: again=-1 ELSE again=0 540 UNTIL NOT again 550 END 560 570 580 DEF PROCsegment 590 REPEAT 600 MOVE 639,511 610 PLOT 85,400*COS(angle)+639,400*SIN(angle)+511 620 angle=angle+res 630 PLOT 85,400*COS(angle)+639,400*SIN(angle)+511 640 angle=angle+res 650 UNTIL angle>last_angle+2*PI*item(J)/total 660 ENDPROC
P54 Bar Chart
This program can draw a chart of up to thirty bars onto the screen. The bars are automatically scaled to fit onto the screen. The chart is not labelled. This is left as an exercise for the reader.
COMMANDS
Key in program and type RUN.
Enter number of bars, less than 30.
Enter the value of each bar as requested.
100 REM Program P54 - Bar Chart 110 MODE 6 120 VDU 23,224,126,126,126,126,126,126,126,126 130 VDU 23,225,24,24,24,24,24,24,24,24 140 VDU 23,226,0,0,0,0,0,0,0,255 150 @%=3 160 INPUT "Enter number of bars (<30) "bars 170 DIM val(bars) 180 max=0 190 FOR I=1 TO bars 200 INPUT "Value of bar " val(I) 210 IF val(I)>max THEN max = val(I) 220 NEXT I 230 240 CLS 250 scale=1 260 IF max>20 THEN scale=max / 20 270 280 FOR I=1 TO bars 290 val(I)=INT(val(I)/scale) 300 NEXT I 310 320 FOR I=1 TO 20 STEP 2 330 PRINT TAB(0,22-I) INT(I*scale); 340 NEXT I 350 360 FOR I=0 TO 22 370 PRINT TAB(4,I)CHR$(225); 380 NEXT I 390 400 410 FOR I=1 TO bars+2 420 PRINT TAB(4+I,22)CHR$(226); 430 NEXT I 440 450 FOR I=1 TO bars 460 FOR J=1 TO val(I) 470 PRINT TAB(6+I,22-J) CHR$(224); 480 NEXT J 490 NEXT I 500 PRINT TAB(0,23) "" 510 Z=GET
P55 Mean and Standard Deviation
This program, with some error-correcting routines, is used to find the mean and standard deviation of a list of data items.
Notice that the data are stored in the form of a string, therefore as the program stands it will only handle a limited number of data items.
If more data items have to be handled, then the program should be amended to change num$ into an array.
COMMANDS
Key in program and type RUN.
Enter data items, one at a time.
Make first character of terminating data item non-numeric to finish.
If a printer is required, ensure that correct *FX commands have been entered.
100 REM Program P55 - Mean and Standard Deviation 110 MODE 6 120 REMON ERROR RUN:REM catches effect of first item being non-numeric 130 PROCsetup 140 REM define window 150 PRINT"This program can be used to" 160 PRINT "calculate the mean and standard" 170 PRINT "deviation of a set of numeric" 180 PRINT "data. Enter your data one item" 190 PRINT "at a time, terminating " 200 PRINT "the data with any non-numeric" 210 PRINT "character" 220 PRINT'' 230 240 REPEAT 250 PRINT TAB(10,20);"Next item ";SPC(16); 260 PROCgetnumb 270 IF NOT end THEN PROCcalcs 280 UNTIL end 290 300 310 REM calculate and output results 320 CLS 330 mean=total/no_of_numbers 340 deviation=SQR(sum_of_squares/no_of_numbers-mean*mean) 350 PRINT "The mean of the data is " 360 PRINT mean 370 PRINT "And the standard deviation is" 380 PRINT deviation 390 CLS 400 PRINT " " 410 PRINT " " 420 PRINT "The data were: " 430 PRINT " " 440 PRINT LEFT$(numb$,LEN(numb$)-1) 450 PRINT " " 460 PRINT "The mean is "mean 470 PRINT " " 480 PRINT "The standard deviation is "deviation 490 500 END 510 REM The procedures 520 530 DEF PROCgetnumb 540 LOCAL B$,I,K 550 K=0 560 REPEAT B$=GET$: UNTIL ASC(B$)<>13 570 PRINT TAB(25,20);B$ 580 IF FNcheck(B$) end=-1: ENDPROC 590 A$=B$ 600 FOR I=1 TO 14:REM max no of digits=14 610 620 REPEAT 630 B$=GET$ 640 IF B$="." THEN K=K+1 650 B=VAL(B$)=0 AND NOT(B$="0" OR B$="." OR ASC(B$)=13 ) 660 IF B THEN SOUND 2,-15,33,2 670 IF K>1 THEN SOUND 2,-15,33,2:K=K-1:B=-1 680 UNTIL B=0 690 700 A$=A$+B$ 710 IF ASC(B$)=13 THEN I=14:A$=LEFT$(A$,LEN(A$)-1):FLAG= 1 720 PRINT TAB(25+I,20);B$ 730 NEXT I 740 IF FLAG=0 REPEAT SOUND 2,-15,33,2:B$=GET$:UNTIL ASC(B$ )=13 750 ENDPROC 760 770 780 DEF FNcheck(B$) 790 IF B$="." THEN K=1 800 = VAL(B$)=0 AND NOT((B$="0") OR (B$="-") OR (B$="+") O R (B$=".")) 810 820 830 DEF PROCcalcs 840 A=VAL(A$) 850 total=total+A 860 no_of_numbers=no_of_numbers+1 870 sum_of_squares=sum_of_squares+A*A 880 numb$=A$+","+numb$ 890 ENDPROC 900 910 920 DEF PROCsetup 930 end=0:FLAG=0:mean=0:total=0 940 no_of_numbers=0:deviation=0 950 sum_of_squares=0 960 A$="" 970 ENDPROC
P56 Bubble Sort
This is a demonstration to show how the classical Bubble Sort works. The program sorts ten numbers on the screen using colours and large characters.
You are asked to input the ten numbers, and the speed of processing; the higher the number the slower the speed on the screen.
Note that numbers displayed in yellow are being compared, and those in red are being swapped.
COMMANDS
Key in program and type RUN.
100 REM rogram P56 - Bubble Sort 110 DIM key%(10) 120 MODE 6 130 REM program sorts array of numbers into order on scree n 140 REM The next section takes in the numbers 150 PRINT "How many digits does each no. have" 160 INPUT L 170 PRINT "Enter 10 numbers one at a time" 180 FOR I=1 TO 10 190 200 INPUT key%(I) 210 IF LEN(STR$(key%(I))) <> L THEN SOUND 1,-15,53,2:I=I -1 220 230 NEXT I 240 INPUT "Speed of processing (1 to 10)",SPEED 250 SPEED=SPEED*50 260 REM Print array onto screen 270 MODE 2 280 FOR I=1 TO 10 290 PRINT TAB(5,I*2);key%(I); 300 NEXT I 310 PRINT TAB(0,25) "PRESS ANY KEY" 320 L=GET 330 PRINT TAB(0,25) " " 340 PROCsort(10) 350 PRINT 360 END 370 380 390 DEF PROCsort(N) 400 FOR I=1 TO (N-1) 410 FOR J=I+1 TO N 420 PROCprint(3) 430 X=INKEY(SPEED) 440 IF key%(J)>key%(I) THEN PROCswap 450 PROCprint(7) 460 NEXT J 470 NEXT I 480 ENDPROC 490 500 DEF PROCswap 510 PROCprint(1) 520 X=INKEY(SPEED) 530 temp%=key%(J) 540 key%(J)=key%(I) 550 key%(I)=temp% 560 X=INKEY(SPEED) 570 PROCprint(7) 580 ENDPROC 590 600 DEF PROCprint(X) 610 COLOUR X 620 PRINT TAB(5,I*2);key%(I); 630 PRINT TAB(5,J*2);key%(J); 640 ENDPROC
P57 Shell Sort
This is the classical fast Shell Sort program. Similar to the bubble sort, the sorting is carried out on the screen. It can be a very useful program to try to figure out why the Shell routine works.
The sort routine itself is given in lines 310 through line 450, with a swap routine given in lines 470 through 550. These parts of the program could be used within a disc based sort program.
COMMANDS
Key in program and type RUN.
Follow instructions.
Note that SPEED=10 gives a slow sort, SPEED=1 gives a fast sort.
100 REM Program P57 - Shell Sort 110 MODE 6 120 N=10 130 DIM array(N-1) 140 REM This program uses the Shell algorithm to sort an a rray into order on the screen 150 PRINT "Enter "STR$(N)" numbers one at a time" 160 FOR I=0 TO N-1 170 INPUT "Number",array(I) 180 NEXT I 190 INPUT "Speed of processing (1 to 10) "SPEED 200 SPEED=SPEED*10 210 MODE 2 220 FOR I=0 TO N-1 230 PRINT TAB(6,(I+1)*2);array(I); 240 NEXT I 250 PROCsort(N) 260 PRINT 270 END 280 290 DEF PROCsort(N) 300 dist%=(N-1)/2 310 REPEAT 320 FOR I=dist%+1 TO N-1 330 FOR J=I-dist% TO 0 STEP -dist% 340 point=J+dist% 350 PROCprint(3) 360 Z=INKEY(SPEED) 370 IF array(J)>array(point) THEN PROCswap(J,point) 380 PROCprint(7) 390 NEXT J 400 NEXT I 410 dist%=dist%/2 420 UNTIL dist%=0 430 ENDPROC 440 450 DEF PROCswap(J,point) 460 PROCprint(1) 470 Z=INKEY(SPEED) 480 temp=array(J) 490 array(J)=array(point) 500 array(point)=temp 510 Z=INKEY(SPEED) 520 PROCprint(7) 530 ENDPROC 540 550 DEF PROCprint(X) 560 COLOUR X 570 PRINT TAB(6,(point+1)*2);array(point);" "; 580 PRINT TAB(6,(J+1)*2);array(J);" "; 590 ENDPROC
P58 Merge
A common need in data processing is the ability to merge two sorted files to produce a third sorted file.
It is quicker to sort small files and then to merge those files to form larger ones. In this program, we mimic file handling by using arrays. The array elements are entered at the keyboard but the program could be amended to allow the elements to be entered via tape files.
COMMANDS
Key in program and type RUN.
Enter array elements when prompted in increasing order.
100 REM Program P58 - Merge 110 MODE 6 120 PRINT "This program is used to merge two" 130 PRINT "arrays of data. Each array can hold up" 140 PRINT "to 100 data items. This program would" 150 PRINT "be used with a disc based sort routine." 160 PRINT "Each array must be entered in" 170 PRINT "increasing order."'' 180 DIM array1(100),array2(100),merge(200) 190 200 I=0 210 REPEAT 220 I=I+1 230 PRINT TAB(0,10)STRING$(40," ") 240 INPUT TAB(0,10)"Array 1 element (0 to finish) "array 1(I) 250 IF array1(I)<>0 AND array1(I-1)>array1(I) THEN PRINT TAB(0,10)"OUT OF ORDER";STRING$(28,".":I=I-1:Z=INKEY(200) 260 UNTIL array1(I)=0 OR I=100 270 n1=I-1 280 290 I=0 300 REPEAT 310 I=I+1 320 PRINT TAB(0,12)STRING$(40," ") 330 INPUT TAB(0,12)"Array2 element (0 to finish) "array2 (I) 340 IF array2(I)<>0 AND array2(I-1)>array2(I) THEN PRINT TAB(0,10)"OUT OF ORDER";STRING$(28,".":I=I-1:Z=INKEY(200) 350 UNTIL array2(I)=0 OR I=100 360 n2=I-1 370 380 I=1:J=1 390 REPEAT 400 IF array1(I)<array2(I) THEN merge(I+J-1)=array1(I):I =I+1 ELSE merge(I+J-1)=array2(J):J=J+1 410 UNTIL array1(I)=0 OR array2(J)=0 420 IF array2(J)=0 THEN PROCrunoutI 430 IF array1(I)=0 THEN PROCrunoutJ 440 CLS 450 PRINT "Array 1 consisted of :" 460 FOR I=1 TO n1 470 PRINT array1(I); 480 NEXT I 490 PRINT '"Array 2 consisted of :" 500 FOR I=1 TO n2 510 PRINT array2(I); 520 NEXT I 530 PRINT '"The merged array is :" 540 FOR I=1 TO n1+n2 550 PRINT merge(I); 560 NEXT I 570 END 580 590 DEF PROCrunoutI 600 FOR K=I TO n1 610 merge(K+J-1)=array1(K) 620 NEXT K 630 ENDPROC 640 650 DEF PROCrunoutJ 660 FOR K=J TO n2 670 merge(K+I-2)=array2(K) 680 NEXT K 690 ENDPROC
P59 Binary Search
If you have a mass of data sorted into order, then it is very inefficient to search for a particular item in a sequential manner. For example, when looking for a word in a dictionary you would not consider every word in sequence until you found the required entry.
A more efficient search method is to open the dictionary in the middle and decide which half of the book holds your word. You then take half of the book and half it again. This routine is repeated until the page holding your word is found.
This program performs a similar search on a set of data - a price list. It could of course be used in any searching problem.
COMMANDS
Key in program and type RUN.
Enter item required from price list.
100 REM Program P59 - Binary Search 110 DIM item$(100),price(100) 120 MODE 6 130 PRINT TAB(7,12)"B I N A R Y S E A R C H" 140 Z=INKEY(300) 150 CLS 160 PRINT "This program shows how the computer" 170 PRINT "can be used to quickly look up a set of" 180 PRINT "data. We assume that the data have been" 190 PRINT "sorted into alphabetical order, for the" 200 PRINT "purposes of this program, the data are" 210 PRINT "held in data statements. In this case" 220 PRINT "the data constitute a price list." 230 PRINT '"Press any key to continue" 240 Z=GET 250 CLS 260 I=0 270 REPEAT 280 I=I+1 290 READ item$(I),price(I) 300 UNTIL item$(I)="ZZZ" 310 N%=I-1 320 330 REPEAT 340 INPUT ''"Which item do you require "item$ 350 360 M%=N%/2 370 L%=M% 380 found=FALSE 390 not_there=FALSE 400 REPEAT 410 IF item$<item$(M%+1) AND item$>item$(M%-1) AND NOT found THEN not_there=TRUE 420 IF item$=item$(M%) THEN found=TRUE 430 IF item$<item$(M%) THEN L%=L%/2:M%=M%-L%+(L%=0) 440 IF item$>item$(M%) THEN L%=L%/2:M%=M%+L%-(L%=0) 450 REPEAT 460 IF M%>N% THEN L%=L%/2:M%=M%-L%+(L%=0) 470 UNTIL NOT(M%>N%) 480 UNTIL found OR not_there 490 500 IF found THEN PRINT '''item$(M%),price(M%) ELSE PRIN T ''"Item not in list" 510 INPUT '''"Another",res$ 520 UNTIL LEFT$(res$,1)<>"Y" 530 END 540 550 DATA APPLES,30.1,BAG,12.09,BAG-BLUE,56.34,BANANA,3.10, BANGR,123.23,BEER-EXPORT,0.45,BEER-LAGER,0.55 560 DATA BEER-STOUT,0.67,BOOZE,4.34,BOTTLE,90.2,BOTTLE-GRE EN,3.45,BOVERIL,0.75 570 DATA CABBAGE,0.76,CANADA DRY,0.78,CANADIAN CLUB,7.35,C ARROTS,0.30,COD LIVER OIL,0.76,COMPUTERS,299.99 580 DATA COOKERS,0.65,CRAB-APPLES,0.00 590 DATA DIGGERS,4567.34,DISC DRIVES,250.67,DISC DDDS,2.55 ,DISCS SSSS,1.80,DOZERS,123456.67 600 DATA EMPIRE BISCUITS,.70,EMULATOR,5677,ENAMEL-RED,7.35 ,ENAMEL-WHITE,8.35,ENGINE-CAR,50.56,ENGINE-SCOOTER,45.76,ET CETERA,0,EWE,36.25,EXPLOSIVE,105.67,FABRIC-BLACK,12.45 610 DATA FABRIC-BLUE,9.67,FABRIC-RED,111.6,FABRIC-WHITE,56 .0,FAN,5.79,FARE,0.98,FARM,1000000,FEATHER PILLOW,78.67 620 DATA ZZZ,0
P60 Permutations
This program can be used to find the number of permutations of n objects taken r at a time. This is a very useful routine in statistics.
COMMANDS
Key in program and type RUN.
Follow instructions.
100 REM Program P60 - Permutations 110 MODE 6 120 PRINT "This program can be used to find the" 130 PRINT "number of permutations of n objects" 140 PRINT "taken r at a time." 150 PRINT '"For example, suppose there are 4 people" 160 PRINT "in a race, then how many way can the" 170 PRINT "first three positions be filled?" 180 PRINT ''"Suppose the racers are called A,B,C & D" 190 PRINT "then possible finishing positions " 200 PRINT "would be:" 210 PRINT '"ABC BAC BCA CAB CBA ACB" 220 PRINT "ABD ADB BDA BAD DBA DAB" 230 PRINT "ACD ADC CAD CDA DAC DCA" 240 PRINT "BCD BDC CBD CDB DCB DBC" 250 PRINT '"In this example there are 24 ways of" 260 PRINT "placing the first 3 in the race" 270 PRINT ''"PRESS ANY KEY TO CONTINUE" 280 X=GET 290 CLS 300 PRINT "This program will allow the user to" 310 PRINT "calculate the number of permutations" 320 INPUT '''"Number of objects "n 330 INPUT "Value of r - the size of the group "r 340 perms=FNfact(n)/FNfact(n-r) 350 PRINT ''''"Number of permutations="STR$(perms) 360 END 370 380 DEF FNfact(r) 390 IF r>33 THEN PRINT "TOO BIG":=0 400 IF r=1 THEN =1 ELSE =r*FNfact(r-1)
P61 Combinations
This program finds the number of combinations of n objects taken r at a time.
COMMANDS
Key in program and type RUN.
Follow instructions.
100 REM Program P61 - Combinations 110 MODE 6 120 PRINT "This program can be used to find the" 130 PRINT "number of combinations of n objects" 140 PRINT "taken r at a time." 150 PRINT ''"For example, suppose we wish to make up" 160 PRINT "a committee of 3 people out of a" 170 PRINT "pool of 4 people. How many ways can" 180 PRINT "this be done?" 190 PRINT ''"Suppose the people are called A,B,C and" 200 PRINT "D, then possible combinations would be" 210 PRINT '"ABC ABD ACD BCD" 220 PRINT'"In this example there are 4 ways of" 230 PRINT "making up the committee" 240 PRINT ''"PRESS ANY KEY TO CONTINUE" 250 X=GET 260 CLS 270 PRINT "This program will allow the user to" 280 PRINT "calculate the number of combinations" 290 INPUT '''"How many objects are to be selected from "n 300 INPUT "Value of r - the size of the group "r 310 combs=FNfact(n)/FNfact(n-r)/FNfact(r) 320 PRINT ''''"Number of combinations="STR$(combs) 330 END 340 350 DEF FNfact(r) 360 IF r>33 THEN PRINT "TOO BIG":=0 370 IF r=1 THEN =1 ELSE =r*FNfact(r-1)
P62 Least Squares
This program uses the method of least squares to find the best straight line through a set of data points.
The straight line found is in the form
Y=MX+B
with the parameters M and B found in lines 280 and 290. When the equation is found, it is plotted on the screen along with the data points.
COMMANDS
Key in program and type RUN.
Enter the data items in the form X,Y.
100 REM Program P62 - Least Squares 110 @%=&20205 120 MODE 6 130 PRINT "LEAST SQUARES" 140 INPUT ''"How many data points",N 150 PRINT '' 160 DIM X(N),Y(N) 170 FOR I=1 TO N 180 INPUT "X=",X(I),"Y=",Y(I) 190 sum_x=sum_x + X(I) 200 sum_x_sq=sum_x_sq + X(I)*X(I) 210 sum_y=sum_y + Y(I) 220 sum_xy=sum_xy + X(I)*Y(I) 230 NEXT I 240 250 D=N*sum_x_sq - sum_x*sum_x 260 IF D=0 THEN PRINT "NO FIT POSSIBLE!!!":END 270 280 M=(N*sum_xy - sum_x*sum_y)/D 290 B=sum_y/N - M*sum_x/N 300 REM plotting routine 310 max_x=X(1):min_x=X(1) 320 max_y=X(1):min_y=Y(1) 330 FOR I=2 TO N 340 IF max_x<X(I) THEN max_x=X(I) 350 IF min_x>X(I) THEN min_x=X(I) 360 IF max_y<Y(I) THEN max_y=Y(I) 370 IF min_y>Y(I) THEN min_y=Y(I) 380 NEXT I 390 MODE 4 400 IF min_x>0 THEN min_x=0 410 IF min_y>0 THEN min_y=0 420 range_x=max_x - min_x 430 range_y=max_y - min_y 440 REM plot will not work for all negative data 450 scale_x=1200/range_x 460 scale_y=1000/range_y 470 VDU 29,ABS(min_x*scale_x);ABS(min_y*scale_y); 480 MOVE min_x*scale_x,0:DRAW max_x*scale_x,0 490 MOVE 0,min_y*scale_y:DRAW 0,max_y*scale_y 500 VDU 5 510 FOR I=1 TO N 520 MOVE X(I)*scale_x,Y(I)*scale_y 530 PRINT CHR$(8);"+"; 540 NEXT I 550 MOVE min_x*scale_x,(M*min_x + B)*scale_y 560 DRAW max_x*scale_x,(M*max_x + B)*scale_y 570 VDU 4 580 PRINT TAB(0,0)"Line is Y="M"*X+"B 590 END