Page 1 of 1

Subscript out Of Range error

Posted: Thu Nov 01, 2012 3:58 pm
by JasonQuinn1992
Why am I getting an subscript out of range error on this line?

AboveAve(N) = 0

My code:


DIM AnnIncome(40) AS INTEGER

' Variables used:
' T1$ H1$, H2$, D1$ Print images
' ST$, TL$ Print images
' PrevTotal Previous Total
' LineCt Line number
' PageCt Page number
' TotHours Total
' IDNum ID Number
' AnnIncome$ Annual Income
' NumPersons Number of Persons


' ***********************************************************************
' * PROGRAM MAINLINE *
' ***********************************************************************

CLS
GOSUB InitializeVariables
GOSUB PrintHeadings
GOSUB ProcessDetail
GOSUB CalculatePovertyLevel
GOSUB CalculateAboveAve
END

' ***********************************************************************
' * INITIALIZE VARIABLES *
' ***********************************************************************

InitializeVariables:
LET PageCt = 0
LET Rate = 0
LET Hours = 0
LET T1$ = " Income Survey PAGE ##"
LET H1$ = ""
LET H2$ = " ID Number ANNUAL INCOME NUMBER OF PERSONS "
LET D1$ = " #### $#,###.## #"
LET ST$ = " / \ SUBTOTAL #"
LET TL$ = " TOTAL #"
LET MaxLines = 30
RETURN

' ***************************************************************************
' * LPRINT HEADINGS *
' ***************************************************************************

PrintHeadings:
LET PageCt = PageCt + 1 'Add to page counter
PRINT CHR$(12); 'Advance to tope of page
PRINT USING T1$; PageCt 'Print title line
PRINT
PRINT H1$ 'Print column headings, line 1
PRINT H2$ 'Print column headings, line 2
PRINT
LET LineCt = 0 'Reset line counter
RETURN

' **********************************************************************************
' PROCESS DATA
' ***********************************************************************************
ProcessDetail:

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


' *************************************************************************
' * READ DATA *
' *************************************************************************

ReadData:
READ IDNum, AnnIncome, NumPersons
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
RETURN


CalculatePovertyLevel:
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







Also, how would I print this program as a 3-part report

Posted: Fri Nov 02, 2012 12:35 am
by burger2227
DIM the array to use it. It has to be sized if it holds 10 or more values.

Posted: Fri Nov 02, 2012 12:01 pm
by JasonQuinn1992
Just "DIM"ed it, but am still getting the eror

Posted: Fri Nov 02, 2012 2:54 pm
by burger2227
Your code is incomplete. What is NEXT N doing?

Posted: Fri Nov 02, 2012 5:38 pm
by JasonQuinn1992
Counting the next number..... I have to print a 3 part report about income


I added a print statement after the calculations

Posted: Fri Nov 02, 2012 9:25 pm
by burger2227
NEXT requires a FOR statement before it.

Posted: Fri Nov 02, 2012 10:51 pm
by JasonQuinn1992
I believe I corrected it:

My full code:

DIM AnIncome(100)



' Variables used:
' T1$ H1$, H2$, D1$ Print images
' ST$, TL$ Print images
' PrevTotal Previous Total
' LineCt Line number
' PageCt Page number
' TotHours Total
' IDNum ID Number
' AnIncome$ Annual Income
' NumPersons Number of Persons


' ***********************************************************************
' * PROGRAM MAINLINE *
' ***********************************************************************

CLS
GOSUB InitializeVariables
GOSUB PrintHeadings
GOSUB ProcessDetail
GOSUB CalculatePovertyLevel
GOSUB CalculateAboveAve
END

' ***********************************************************************
' * INITIALIZE VARIABLES *
' ***********************************************************************

InitializeVariables:
LET PageCt = 0
LET Rate = 0
LET Hours = 0
LET T1$ = " Income Survey PAGE ##"
LET H1$ = ""
LET H2$ = " ID Number ANNUAL INCOME NUMBER OF PERSONS "
LET D1$ = " #### $#,###.## #"
LET ST$ = " / \ SUBTOTAL #"
LET TL$ = " TOTAL #"
LET MaxLines = 30
RETURN

' ***************************************************************************
' * LPRINT HEADINGS *
' ***************************************************************************

PrintHeadings:
LET PageCt = PageCt + 1 'Add to page counter
PRINT CHR$(12); 'Advance to tope of page
PRINT USING T1$; PageCt 'Print title line
PRINT
PRINT H1$ 'Print column headings, line 1
PRINT H2$ 'Print column headings, line 2
PRINT
LET LineCt = 0 'Reset line counter
RETURN

' **********************************************************************************
' PROCESS DATA
' ***********************************************************************************
ProcessDetail:

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


' *************************************************************************
' * READ DATA *
' *************************************************************************

ReadData:
READ IDNum, AnIncome, NumPersons
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
RETURN


CalculatePovertyLevel:
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

PrintDetail:
PRINT USING D1$; IDNum; AnIncome; NumPersons 'Print details
RETURN