Page 1 of 1

Subscript out Of Range error (UPDATED CODE)

Posted: Thu Nov 15, 2012 8:29 pm
by JasonQuinn1992
Why would I be getting an Subscript out of range error on this line?

IF NumPersons(N) <= 2 THEN

My Code:


DIM IDNum(14), AnIncome(14), NumPersons(14)


GOSUB InitializeVariables
GOSUB LoadTables
GOSUB CalcPoverty
GOSUB CalcAvg


InitializeVariables:
LET TotIncome = TotIncome + AnIncome(N) 'Calc total
LET TotBelowPov = TotBelowPov + BelowPov(N) 'Calc total below pov
LET AveIncome = TotIncome / 14 'Calc average income
LET PercentBelowPov = TotBelowPov / 14 * 100 'Calc % below pov
LET AboveAve(N) = 48
LET PageCt = 0
LET IDNum = 0
LET NumPersons = 0
PRINT T1$ = " Income Survey PAGE ##"
LET H1$ = ""
PRINT H2$ = " ID Number ANNUAL INCOME NUMBER OF PERSONS "
PRINT D1$ = " #### $#,###.## #"
PRINT ST$ = " / \ SUBTOTAL #"
PRINT TL$ = " TOTAL #"
LET MaxLines = 30
RETURN

'******************************
' Read numbers into the array
' *****************************


LoadTables:
FOR N = 1 TO 14
READ IDNum(N), AnIncome(N), NumPersons(N)
NEXT N

DATA 2497, 12,500, 2
DATA 3323, 13,00, 5
DATA 4521, 18,210, 4
DATA 6789, 8,000, 2
DATA 5476, 6,000, 1
DATA 4423, 16,400, 3
DATA 6587, 25,000, 4
DATA 3221, 10,500, 4
DATA 5555, 15,000, 2
DATA 0085, 19,700, 3
DATA 3097, 20,000, 8
DATA 4480, 23,400, 5
DATA 0265, 19,700, 2
DATA 8901, 13,000, 3

CalcPoverty:
IF NumPersons(N) <2> 2 THEN
PovertyLevel(N) = 8000 + (2000 * (NumPersons(N) - 2))
END IF
IF AnIncome(N) <PovertyLevel> AveIncome THEN
AboveAve(N) = 1
ELSE
AboveAve(N) = 0
END IF
NEXT N
RETURN

Posted: Fri Nov 16, 2012 12:37 am
by burger2227
That is not what appears in your code.

Code: Select all

DIM IDNum(14), AnIncome(14), NumPersons(14) 


GOSUB InitializeVariables 
GOSUB LoadTables 
GOSUB CalcPoverty 
GOSUB CalcAvg 


InitializeVariables: 
LET TotIncome = TotIncome + AnIncome(N) 'Calc total 
LET TotBelowPov = TotBelowPov + BelowPov(N) 'Calc total below pov 
LET AveIncome = TotIncome / 14 'Calc average income 
LET PercentBelowPov = TotBelowPov / 14 * 100 'Calc % below pov 
LET AboveAve(N) = 48 
LET PageCt = 0 
LET IDNum = 0 
LET NumPersons = 0 
PRINT T1$ = " Income Survey PAGE ##" 
LET H1$ = "" 
PRINT H2$ = " ID Number ANNUAL INCOME NUMBER OF PERSONS " 
PRINT D1$ = " #### $#,###.## #" 
PRINT ST$ = " / \ SUBTOTAL #" 
PRINT TL$ = " TOTAL #" 
LET MaxLines = 30 
RETURN 

'****************************** 
' Read numbers into the array 
' ***************************** 


LoadTables: 
FOR N = 1 TO 14 
READ IDNum(N), AnIncome(N), NumPersons(N) 
NEXT N 
RETURN '<<<<<< ???

DATA 2497, 12,500, 2 
DATA 3323, 13,00, 5 
DATA 4521, 18,210, 4 
DATA 6789, 8,000, 2 
DATA 5476, 6,000, 1 
DATA 4423, 16,400, 3 
DATA 6587, 25,000, 4 
DATA 3221, 10,500, 4 
DATA 5555, 15,000, 2 
DATA 0085, 19,700, 3 
DATA 3097, 20,000, 8 
DATA 4480, 23,400, 5 
DATA 0265, 19,700, 2 
DATA 8901, 13,000, 3 

CalcPoverty: 
IF NumPersons(N) <= 2 THEN 
PovertyLevel(N) = 8000 + (2000 * (NumPersons(N) - 2)) 
END IF 
IF AnIncome(N) < PovertyLevel THEN 
AboveAve(N) = 1 '<<<<<<<<<<< DIM?
ELSE 
AboveAve(N) = 0 
END IF 
NEXT N  ' <<<<<<<<< NO FOR?
RETURN
Why is your code always incomplete? Check your work.

Posted: Fri Nov 16, 2012 12:42 pm
by JasonQuinn1992
The code is complete the NEXT is for the next number

Posted: Sat Nov 17, 2012 6:51 pm
by burger2227
Look at the bottom of the code! <<<<<<NO FOR

NEXT N should not be there.

Posted: Wed Nov 21, 2012 1:56 pm
by JasonQuinn1992
I was able to fix it:

part of it:


CalcPoverty:
IF NumPersons(N) <2> 2 THEN
PovertyLevel(N) = 8000 + (2000 * (NumPersons(N) - 2))
END IF
IF AnIncome(N) < PovertyLevel(N) THEN
LET TotBelowPov = TotBelowPov + 1
END IF
RETURN

PrintDetail:
PRINT H2$
FOR N = 1 TO 14
PRINT USING D1$; IDNum(N); AnIncome(N); NumPersons(N) 'Print details
NEXT N
RETURN

PrintSubtotals:
LET AvgIncome = TotIncome / 14 'Calc average income
LET PercentBelowPov = TotBelowPov / 14 * 100 'Calc % below pov
RETURN

PrintTotals:
PRINT USING "$##,###.## \ \"; AvgIncome; "is the average income"
GOSUB PrintAboveAverage
PRINT USING "##.#% \ \"; PercentBelowPov; "of households are below the poverty line"
RETURN

PrintAboveAverage:
PRINT H2$
FOR N = 1 TO 14
IF AnIncome(N) > AvgIncome THEN
PRINT USING D1$; IDNum(N); AnIncome(N); NumPersons(N)
END IF
NEXT N
RETURN