Creating and using parallel array

Display Grade based on Points

Posted 01 March 2010 - 04:16 PM

I have a homework assignment to code an application that allows the Prof to display a grade based on the numnber of points he enter. The grading scale is : 0-299 -F; 300-349- D; 350-399- C; 400-449- B; 450-500- A. We are to store the minimum points in a 5 element one dimensional Integer array name intPonts and store the grades in a 5 element one dimensional String array name strGrades. The arrays should be parallel arrays. The procedure should search the intPoints array for the number of points entered by the user and then display the corresponding grade from the strGrades array.

I have stored the minimum points and grades in a one dimensional integer and string array respectively. I have coded the program, and when I put in random points, it gives me the letter grade. For example, if I put in 376 it will display a "C"; if I put in 200, it will display an "F". However, if I put in anything over 449, it will NOT display an "A", it just leaves the lbl blank. I am stumped on how to get the code to read pass 449 points.

Replies To: Display Grade based on Points

Re: Display Grade based on Points

Posted 01 March 2010 - 09:57 PM

It is because of this line in your code:

If searchForPoints < intPoints(intGrade) Then

Since 450 is not greater than 450, this if is never entered and no value is assigned to the text box. Now, based on your code, we already know that the user has a value between 500 and 450 (inclusive). This is the only case where this is false. So, add in an Else If that process the false case to place an A in the text box.

For intGrade = 0 To 4
If searchForPoints < intPoints(intGrade) Then
lblGrade.Text = strGrades(intGrade + 1)
Else If 'insert condition and code here
End If
Next intGrade

Re: Display Grade based on Points

Posted 01 March 2010 - 11:04 PM

Merddin, on 01 March 2010 - 07:57 PM, said:

It is because of this line in your code:

If searchForPoints < intPoints(intGrade) Then

Since 450 is not greater than 450, this if is never entered and no value is assigned to the text box. Now, based on your code, we already know that the user has a value between 500 and 450 (inclusive). This is the only case where this is false. So, add in an Else If that process the false case to place an A in the text box.

For intGrade = 0 To 4
If searchForPoints < intPoints(intGrade) Then
lblGrade.Text = strGrades(intGrade + 1)
Else If 'insert condition and code here
End If
Next intGrade

Hope that helps and good luck with the coding.

The label should be set to 'A' before that loop starts. If the mark is not supposed to be an 'A' then the loop will replace it. Adding an else if in the loop means that lblGrade.Text will always be 'A' unless searchForPoints < intPoints(4) is true, because whether or not it gets set to 'A' in previous iterations, it comes down to the final loop.