25 Years of Programming
An open source source for C, C++, OWL, BASIC, MDB, XLS, DOT, and more...
Home   Projects   Up   Sitemap   Search   Blog   Forum+Chat   About Us   Privacy   Terms of Use   Feedback   FAQ   Images   Services   Ads   Donate   Humor
MATRIX

A common bookkeeping task is that of summing all the rows
and columns of a two dimensional matrix.  Examples of such
matrices are payroll records and sales recaps.  This program will
sum for you all the rows and all the columns of a matrix.

The program is self-prompting, and will take you through
the initial input routine.  Before starting, you need to know
only how many rows and how many columns there are in the matrix.
After you have input the numbers once, you have the option of
storing them on disk for future reference.  If you do that, you
can later reload them, thus saving the work of re-entering them.

If you mis-key a number, then enter '9E9' for the next
number.  The program will back up one entry and allow you to 
re-input the number.  If you need to back up more than one
entry, just type '9E9' in response to as many prompts as you need to.
After backing up, you must re-enter in forward order any numbers
that you passed over on your way backwards.

After computation has been done, you have several options.
If you note an error in one of the entries, there is a capability
to change it.  After making changes, you can recompute the entire
matrix.  You can get a listing of all your raw data entries.
You can save the entire matrix to a disk file for future reference.

One important thing to note is that since you are only
inputting each number once, there will be no inherent error checking
as computation is done.  A wrong entry will produce an erroneous
sum for both the row and the column which contain it, and there
will be no signal that the error has been made.  Therefore, be
careful when entering the numbers or when checking them.  
Fortunately, many matrices have inherent summing in them; for 
example, entries in payroll records all add up to gross pay.
If you run all the data except gross pay through this program,
then each of your row totals must equal a corresponding gross
pay amount.

10 REM MATRIX.BAS 
11 'COPYRIGHT (C)1982 STEVEN WHITNEY.  
12 'Published under GNU GPL (General Public License) Version 2, with ABSOLUTELY NO WARRANTY.
20 ON ERROR GOTO 1570
30 L$=STRING$(75,"-")
40 PRINT CHR$(27)"E";"This program inputs a two dimensional array (rows and columns), and then ":PRINT "totals all the rows and columns for you.":PRINT L$:PRINT
50 PRINT "IF NOT ALREADY DONE, PLEASE SET YOUR KEYBOARD TO 'CAPS LOCK'.":PRINT "ALL YOUR RESPONSES MUST BE IN UPPER CASE.":PRINT STRING$(75,"=")
60 PRINT "Do you want to use a PRINTER for the output (Y/N)?";:Z$=INPUT$(1):PRINT Z$:PRINT L$
70 IF Z$<>"Y" AND Z$<>"N" THEN 60
80 PRINT:PRINT "Do you want to get the numbers from a file already created by":PRINT "this program (Y/N)?";:X$=INPUT$(1):PRINT X$
90 IF X$<>"Y" AND X$<>"N" THEN 80
100 IF X$<>"Y" THEN 210
110 PRINT:INPUT "Which file (I will automatically assume the .MAT extension)";Q$
120 OPEN "I",1,Q$+".MAT"
130 INPUT #1,R,C 
140 DIM A(R,C)
150 FOR I=1 TO R
160 FOR J=1 TO C
170 INPUT #1,A(I,J)
180 NEXT J,I
190 CLOSE
200 GOTO 480
210 PRINT:PRINT:PRINT
220 PRINT L$
230 INPUT "How many rows";R
240 INPUT "How many columns";C
250 PRINT L$
260 PRINT,,"*** Method Menu ***":PRINT
270 PRINT "1 = Read DOWN the numbers in each column, one column after the other."
280 PRINT "2 = Read ACROSS the numbers in each row, one row after the other.":PRINT
290 PRINT:PRINT "Which method do you want to use?";:T$=INPUT$(1):PRINT T$
300 IF VAL(T$)<1 OR VAL(T$)>2 THEN 260
310 IF T$="2" THEN 810
320 PRINT L$:PRINT:PRINT "You have chosen to read DOWN the numbers in each column.":PRINT STRING$(75,"=")
330 DIM A(R,C)
340 FOR J=1 TO C
350 PRINT CHR$(7)
360 FOR I=1 TO R
370 P=P+1: IF P MOD 20=1 THEN PRINT,,"==>  To correct error, enter '9E9'"
380 PRINT "Next --A(";I;",";J;") = ";
390 INPUT A(I,J)
400 IF A(I,J)<>9E+09 THEN 430
410 PRINT:PRINT "Backing up one entry.":PRINT:I=I-1:IF I=0 THEN I=I+R:J=J-1
420 GOTO 380
430 NEXT I
440 NEXT J
450 PRINT:PRINT "Was the very last number (";A(I-1,J-1);") correct (Y/N)?";:T$=INPUT$(1):PRINT T$
460 IF T$<>"Y" AND T$<>"N" THEN 450
470 IF T$<>"Y" THEN INPUT "Change to: ";A(I-1,J-1)
480 REM - ADDITION
490 PRINT L$:PRINT,,"*** System Working ***":PRINT
500 REM FIRST, ERASE TOTALS FROM PREVIOUS RUNS
510 FOR I=1 TO R : A(I,0)=0 : NEXT I
520 R2=0 : REM ERASE GRAND TOTAL, TOO
530 FOR I=1 TO C : A(0,I)=0 : NEXT I
540 REM END OF ERASURE
550 FOR I=1 TO R
560 FOR J=1 TO C
570 A(I,0)=A(I,0)+A(I,J)
580 NEXT J
590 NEXT I
600 FOR I=1 TO C
610 FOR J=1 TO R
620 A(0,I)=A(0,I)+A(J,I)
630 NEXT J
640 NEXT I
650 P=0
660 PRINT CHR$(27)"E";L$:PRINT,,"*** Row Totals ***":PRINT:IF Z$="Y" THEN LPRINT L$:LPRINT,,"*** Row Totals ***":LPRINT
670 REM OUTPUT
680 FOR I=1 TO R
690 P=P+1 : IF P MOD 15=0 THEN PRINT:PRINT "Press Any Key To Continue":PRINT:X$=INPUT$(1)
700 PRINT "Total of row ";I;"=",A(I,0):IF Z$="Y" THEN LPRINT "Total of row ";I;"=",A(I,0)
710 NEXT I
720 PRINT L$ : IF Z$="Y" THEN LPRINT L$
730 PRINT,,"*** Column Totals ***":PRINT:IF Z$="Y" THEN LPRINT,,"*** Column Totals ***":LPRINT
740 FOR I=1 TO C
750 P=P+1 : IF P MOD 15=0 THEN PRINT:PRINT "Press Any Key To Continue":PRINT:X$=INPUT$(1)
760 PRINT "Total of column";I;"=",A(0,I):IF Z$="Y" THEN LPRINT "Total of column";I;"=",A(0,I)
770 R2=R2+A(0,I) : REM GRAND TOTAL BY COLUMNS
780 NEXT I
790 PRINT,,,"Grand Total =";R2:PRINT STRING$(75,"="):IF Z$="Y" THEN LPRINT,,,"Grand Total = ";R2: LPRINT STRING$(75,"=")
800 GOTO 970
810 DIM A(R,C)
820 PRINT L$:PRINT:PRINT "You have chosen to read ACROSS the numbers in each row.":PRINT STRING$(75,"=")
830 FOR I=1 TO R
840 PRINT CHR$(7)
850 FOR J=1 TO C
860 P=P+1 : IF P MOD 20=1 THEN PRINT,,"==>  To correct error, enter '9E9'"
870 PRINT "Next -- A(";I;",";J;")=";:INPUT A(I,J)
880 IF A(I,J)<>9E+09 THEN 910
890 PRINT "Backing up one entry.":PRINT:J=J-1:IF J=0 THEN J=J+C:I=I-1
900 GOTO 870
910 NEXT J
920 NEXT I
930 PRINT:PRINT "Was the very last number (";A(I-1,J-1);") correct (Y/N)?";:T$=INPUT$(1):PRINT T$
940 IF T$<>"Y" AND T$<>"N" THEN 930
950 IF T$<>"Y" THEN INPUT "Change to: ";A(I-1,J-1)
960 GOTO 480
970 REM MENU SECTION TO CHANGE OR PRINT
980 PRINT:PRINT "Press Any Key To Continue":X$=INPUT$(1)
990 PRINT CHR$(27)"E":PRINT STRING$(75,"=") : IF Z$="Y" THEN LPRINT L$
1000 PRINT,,"*** Option Menu ***":PRINT
1010 PRINT "1 = CHANGE an Element of the Matrix"
1020 PRINT
1030 PRINT "2 = RECOMPUTE the matrix and print results to the SCREEN only."
1040 PRINT "3 = RECOMPUTE the matrix and print results to the PRINTER."
1050 PRINT
1060 PRINT "4 = REPRINT raw data on SCREEN only."
1070 PRINT "5 = REPRINT raw data on PRINTER."
1080 PRINT
1090 PRINT "6 = SAVE the matrix to a DISK file."
1100 PRINT
1110 PRINT "7 = RERUN the program with completely NEW DATA."
1120 PRINT "8 = END"
1130 PRINT:PRINT L$:PRINT:PRINT "Which do you want to do (1,2,3,4,5,6,7,8)?";:X$=INPUT$(1):PRINT X$:IF VAL(X$)<1 OR VAL(X$)>8 THEN 970
1140 PRINT L$
1150 IF X$="7" THEN CLEAR : GOTO 10
1160 IF X$="8" THEN END
1170 IF X$="6" THEN 1440
1180 IF X$="2" THEN Z$="N": GOTO 480
1190 IF X$="3" THEN Z$="Y" : GOTO 480
1200 IF X$="4" THEN Z$="N" : GOTO 1300
1210 IF X$="5" THEN Z$="Y" : GOTO 1300
1220 IF X$="1" THEN PRINT:PRINT "Elements are referred to in the format A(R,C), where R is the row that":PRINT "contains the element and C is the column.  Both rows and columns start at ONE.  That is, the upper left element is A(1,1)."
1230 PRINT
1240 INPUT "What ROW holds the element to be changed";I
1245 IF I<1 OR I>R THEN PRINT:PRINT,"==> ONLY 1 THROUGH";R;"ALLOWED.":PRINT:GOTO 1240
1250 INPUT "What COLUMN holds the element to be changed";J
1255 IF J<1 OR J>C THEN PRINT:PRINT,"==> ONLY 1 THROUGH";C;"ALLOWED.":PRINT:GOTO 1250
1260 PRINT
1270 PRINT "Element A(";I;",";J;") was";A(I,J);".  Change to: ";
1280 INPUT A(I,J)
1290 GOTO 990
1300 REM RAW DATA PRINT ROUTINE
1310 P=0
1320 PRINT L$ : IF Z$="Y" THEN LPRINT L$
1330 PRINT,,"Raw Data Entered":IF Z$="Y" THEN LPRINT,,"Raw Data Entered"
1340 PRINT"Format is A(Row,Column).": IF Z$="Y" THEN LPRINT "Format is A(Row,Column)."
1350 FOR I=1 TO R
1360 PRINT : IF Z$="Y" THEN LPRINT
1370 FOR J=1 TO C
1380 P=P+1 : IF P MOD 15=0 THEN PRINT:PRINT "Press Any Key To Continue":PRINT:X$=INPUT$(1)
1390 IF Z$="Y" THEN LPRINT "A(";I;",";J;")=",A(I,J) 
1400 PRINT "A(";I;",";J;")=",A(I,J)
1410 NEXT J,I
1420 PRINT "End of listing"
1430 GOTO 980
1440 REM SAVE MATRIX TO DISK
1450 PRINT
1460 INPUT "What should I name the data file (up to 8 CAPITAL letters)";Q$
1470 IF LEN(Q$)>8 THEN 1460
1480 OPEN "O",1,Q$+".MAT"
1490 WRITE #1,R,C
1500 FOR I=1 TO R
1510 FOR J=1 TO C
1520 WRITE #1,A(I,J)
1530 NEXT J,I
1540 CLOSE
1550 PRINT:PRINT "If you ever use this file with a program other than this one,":PRINT "please remember that the first two numbers in the file are":PRINT "1) the number of rows in the matrix, and 2) the number of columns."
1560 GOTO 980
1570 IF ERR=53 AND ERL=120 THEN PRINT L$:PRINT "ONLY THESE FILES ARE ON THE DISK::PRINT L$:FILES:PRINT L$:RESUME 70
1580 PRINT "AN ERROR WAS ENCOUNTERED.  PLEASE CONSULT YOUR BASIC-80 MANUAL REGARDING":PRINT"THE FOLLOWING ERROR MESSAGE.  THE ERROR OCCURRED AT PROGRAM LINE #";ERL
1590 ON ERROR GOTO 0
1600 REM COPYRIGHT (C)1982 STEVEN WHITNEY.  

 

 

Valid HTML 4.01 Transitional Valid CSS
View content labeling at ICRA.
Copyright ©2007 Steven Whitney. Last modified 09/25/2007.