|
Post by ivartyn on Dec 24, 2006 17:33:02 GMT
Hi, I've being try 2 calculating two dimensional array. As it is my code, I've succeeded in calculating the rows but finding it difficult to calculate the column and get a grand total.
Help needed
Below is the code I've written:
DIM array(5, 6) AS INTEGER DIM i, j AS INTEGER DIM reply AS STRING DIM total AS INTEGER DIM nettotal AS INTEGER DIM coltotal AS INTEGER
1 :
SLEEP 1
CLS REM ******Calculating the values*******
FOR i = 1 TO 4 LET total = 0 FOR j = 1 TO 5 LET total = total + array(i, j) NEXT j LET array(i, j) = total NEXT i
REM******End of calculation**********
LOCATE 1, 20
PRINT "End Of Day Sales Report" PRINT TAB(9); "Mon"; SPC(5); "Tues"; SPC(5); "Wed"; SPC(5); "Thurs"; SPC(5); "Fri"; SPC(5); "Total"
PRINT : PRINT : PRINT
FOR i = 1 TO 5 FOR j = 1 TO 6 PRINT TAB(9 * j); array(i, j); NEXT j PRINT : PRINT NEXT i
LOCATE 19, 5
INPUT "Do you want to change a Value "; reply
IF reply = "y" OR reply = "Y" THEN
LOCATE 21, 5 INPUT "Enter row number: ", i LOCATE 21, 35 INPUT "Enter column number: ", j
LOCATE 23, 35 INPUT "Enter new value: ", array(i, j) GOTO 1: END IF
REM ******Calculating the values*******
|
|
|
Post by Mikrondel on Dec 29, 2006 3:02:49 GMT
I'm not exactly sure what you want, as you haven't described your problem very well, but this might help you:
FOR i = 1 TO 4 FOR j = 1 TO 5 LET array(i, 6) = array(i, 6) + array(i, j) LET array(5, j) = array(5, j) + array(i, j) NEXT j NEXT i
Instead of
FOR i = 1 TO 4 LET total = 0 FOR j = 1 TO 5 LET total = total + array(i, j) NEXT j LET array(i, j) = total NEXT i
|
|
|
Post by ivartyn on Jan 7, 2007 23:06:15 GMT
Thanks for the help so far. The problem I'm experiencing with the code is calculating the four rows and displaying the total on the fifth row as well as calculating the five columns and displaying the total on the sixth column. The grand total should be shown on the fifth row sixth column.
1. At the moment the total shown after calculating is wrong. 2. The grand total isn't displayed.
Mikrondel: I've used your piece of the code and result is still the same. I've attached the entire code so that you can run the program and see what I'm saying.
DIM array(5, 6) AS INTEGER DIM i, j AS INTEGER DIM reply AS STRING DIM total AS INTEGER DIM nettotal AS INTEGER DIM coltotal AS INTEGER
1 :
SLEEP 1
CLS REM ******Calculating the values*******
'FOR i = 1 TO 4 ' LET total = 0 ' FOR j = 1 TO 5 ' LET total = total + array(i, j) ' NEXT j ' LET array(i, j) = total 'NEXT i
FOR i = 1 TO 4 FOR j = 1 TO 5
LET array(i, 6) = array(i, 6) + array(i, j) LET array(5, j) = array(5, j) + array(i, j) NEXT j NEXT i
REM******End of calculation**********
LOCATE 1, 20
PRINT "End Of Day Sales Report" PRINT TAB(9); "Mon"; SPC(5); "Tues"; SPC(5); "Wed"; SPC(5); "Thurs"; SPC(5); "Fri"; SPC(5); "Total"
PRINT : PRINT : PRINT
FOR i = 1 TO 5 FOR j = 1 TO 6 PRINT TAB(9 * j); array(i, j); NEXT j PRINT : PRINT NEXT i
LOCATE 19, 5
INPUT "Do you want to change a Value "; reply
IF reply = "y" OR reply = "Y" THEN
LOCATE 21, 5 INPUT "Enter row number: ", i LOCATE 21, 35 INPUT "Enter column number: ", j
LOCATE 23, 35 INPUT "Enter new value: ", array(i, j) GOTO 1: END IF
|
|
|
Post by Ildûrest on Jan 9, 2007 7:44:55 GMT
Sorry, I did not notice that you had a loop going (the GOTO 1)
I would strongly advise you to AVOID GOTO AS MUCH AS POSSIBLE. Use a DO-LOOP instead. (Suggestion: rather than IF reply = "y" OR reply = "Y" THEN use IF reply <> "y" AND reply <> "Y" THEN EXIT DO )
Anyway, this means that you have to zero your totals each time you recalculate them. So replace your 'Calculating the values' code with this:
FOR j = 1 TO 6 'Reset bottom totals array(5, j) = 0 NEXT
FOR i = 1 TO 4 'Reset side totals LET array(i, 6) = 0 FOR j = 1 TO 5 'add current value to side total LET array(i, 6) = array(i, 6) + array(i, j) 'add current value to bottom total LET array(5, j) = array(5, j) + array(i, j) NEXT j 'add current side total to grand total array(5, 6) = array(5, 6) + array(i, 6) NEXT i
|
|
|
Post by ivartyn on Jan 21, 2007 15:05:53 GMT
;D Thanks a million for helping me with this. Its now working.
|
|