Answered by:
Performing mathematical operations on array elements
Question

Hi I'm new to programming and have found Small Basic to be very fun and intuitive, but I'm having problems with arrays. I have written a program to calculate my GPA. It compiles fine and runs but will not do the math properly. I'm basically asking how many classes the user has, then using that number to make an array called numClasses, so mI can calculate the grade points, etc. Anyways, here is the entire program. The part I'm sure I'm messing up on is where I 'Calculate total grade points. Any pointers or help anyone could give would be great!
[code]
'GPA calculator
'Get number of classes to calculate GPA for
TextWindow.Write("How many classes do you have grades for?: ")
numClasses = TextWindow.Read()'Initialize the loop control variable and get class hours and letter grades
For i = 1 To numClasses
TextWindow.Write("Enter class hours: ")
hours[numClasses] = TextWindow.Read()
TextWindow.Write("Enter letter grade for class: ")
letterGrade[numClasses] = TextWindow.Read()
EndFor
'Convert letter grades to grade points
For i = 1 To numClasses
If letterGrade = "A" Then
gradePoints = "4"
ElseIf letterGrade = "B" Then
gradePoints = "3"
ElseIf letterGrade = "C" Then
gradePoints = "2"
ElseIf letterGrade = "D" Then
gradePoints = "1"
Else
gradePoints = "0"
EndIf
EndFor
'Calculate total attempted hours
For i = 1 To numClasses
totalHours = "0"
totalHours = totalHours + hours[numClasses]
EndFor
'Calculate total grade points
For i = 1 To numClasses
totGradePoints[numClasses] = "0"
totGradePoints[numClasses] = hours[numClasses] * gradePoints[numClasses]
GradePointTotal = "0"
GradePointTotal = totGradePoints[numClasses] + totGradePoints[numClasses]
EndFor
'Calculate GPA
gpa = totalHours / GradePointTotal
'Display GPA
TextWindow.WriteLine("Your GPA is " + gpa)[/code]
Answers

Joshua,
You've made a good start there, but you're going to kick yourself when I tell you the problem :). It's all correct apart from some minor errors where you use the wrong variable...
In 'Initialise the loop control' you are doing hours[numClasses]= and letterGrade[numClasses]=. Have a think a moment about what you're doing and you'll realise you should be using the loop variable 'i' instead of 'numClasses'. The reason being that 'i' loops through each of the array items, while 'numClasses' always points to the last array item which leaves all the other items blank. (eg. hours[i]=)
The same issue mentioned above occurs in 'Calculate total attempted hours, but you are also always setting totalHours = 0, which you can safely delete altogether as this stops your total from being calculated.
In 'Calculate total grade points you simply want to just calculate the total grade points inside the loop, such as: GradePointTotal = GradePointTotal + hours[i] * gradePoints[i]
Finally, in 'Convert letter grades to grade points, you'll see that you forgot to use the array, and need to add '[i]' after the variable letterGrade. (eg. If letterGrade[i]="A")
 Marked as answer by Joshua01100110 Thursday, May 27, 2010 4:10 AM
All replies

Joshua,
You've made a good start there, but you're going to kick yourself when I tell you the problem :). It's all correct apart from some minor errors where you use the wrong variable...
In 'Initialise the loop control' you are doing hours[numClasses]= and letterGrade[numClasses]=. Have a think a moment about what you're doing and you'll realise you should be using the loop variable 'i' instead of 'numClasses'. The reason being that 'i' loops through each of the array items, while 'numClasses' always points to the last array item which leaves all the other items blank. (eg. hours[i]=)
The same issue mentioned above occurs in 'Calculate total attempted hours, but you are also always setting totalHours = 0, which you can safely delete altogether as this stops your total from being calculated.
In 'Calculate total grade points you simply want to just calculate the total grade points inside the loop, such as: GradePointTotal = GradePointTotal + hours[i] * gradePoints[i]
Finally, in 'Convert letter grades to grade points, you'll see that you forgot to use the array, and need to add '[i]' after the variable letterGrade. (eg. If letterGrade[i]="A")
 Marked as answer by Joshua01100110 Thursday, May 27, 2010 4:10 AM

Joshua,
Using your same layout here's a visual of a working model. This can be downloaded via KPM189.
Jricestk
'GPA calculator
'Get number of classes to calculate GPA for
TextWindow.Write("How many classes do you have grades for?: ")
numClasses = TextWindow.Read()'Initialize the loop control variable and get class hours and letter grades
For i = 1 To numClasses
TextWindow.Write("Enter class hours: ")
hours[i] = TextWindow.Read()
TextWindow.Write("Enter letter grade for class: ")
letterGrade[i] = TextWindow.Read()
EndFor'Convert letter grades to grade points
For i = 1 To numClasses
If letterGrade[i] = "A" or letterGrade[i] ="a" Then
gradePoints[i] = 4
ElseIf letterGrade[i] = "B" or letterGrade[i] = "b" Then
gradePoints[i] = 3
ElseIf letterGrade[i] = "C" or letterGrade[i] = "c" Then
gradePoints[i] = 2
ElseIf letterGrade[i] = "D" or letterGrade[i] = "d" Then
gradePoints[i] = 1
Else
gradePoints[i] = 0
EndIf
EndFor'Calculate total attempted hours
totalHours = "0"
For i = 1 To numClasses
totalHours = totalHours + hours[i]
EndFor'Calculate total grade points
GradePointTotal=0
For i = 1 To numClasses
GradePointTotal = GradePointTotal + (GradePoints[i]*Hours[i])
EndFor'Calculate GPA
gpa = GradePointTotal / TotalHours
'Display GPA
TextWindow.WriteLine("Your GPA is " + gpa)

