# 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?: ")

'Initialize the loop control variable and get class hours and letter grades

For i = 1 To numClasses
TextWindow.Write("Enter class hours: ")
TextWindow.Write("Enter letter grade for class: ")
EndFor

For i = 1 To numClasses
Else
EndIf
EndFor

'Calculate total attempted hours

For i = 1 To numClasses
totalHours = "0"
totalHours = totalHours + hours[numClasses]
EndFor

For i = 1 To numClasses
EndFor

'Calculate GPA

'Display GPA

TextWindow.WriteLine("Your GPA is " + gpa)

[/code]

Friday, May 21, 2010 10:51 PM

• 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.

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")

Saturday, May 22, 2010 9:41 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.

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")

Saturday, May 22, 2010 9:41 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?: ")

'Initialize the loop control variable and get class hours and letter grades

For i = 1 To numClasses
TextWindow.Write("Enter class hours: ")
TextWindow.Write("Enter letter grade for class: ")
EndFor

For i = 1 To numClasses
Else
EndIf
EndFor

'Calculate total attempted hours
totalHours = "0"
For i = 1 To numClasses
totalHours = totalHours + hours[i]
EndFor

For i = 1 To numClasses
EndFor

'Calculate GPA

• • 