Click here to Skip to main content
15,900,511 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I'm writing a program the allows Professor Carver to display a grade based on the number of possible points he enters in the inputbox (strMax). Then the user enters the points he earned in the textbox (dblPoints)

Then I solve the percentage. dblResults = dblPoints/CDbl(strMax)

The first step is to store the minimum percentage points in one dimensional array. I did that.

The second step is to store the grades in one dimensional array. I did that.

The arrays should be parallel arrays. <------ Having trouble with it

It then should display the corresponding grade from the strGrades array <---- Not done.

I'm having trouble to display arrays in that are parallel. The problem is the for loop

I tried this
VB
For intGrade As Integer = 0 To 4
         If dblResults <= dblPer(intGrade) Then
             lblGrade.Text = strGrade(intGrade + 1)
         ElseIf dblResults >= dblPer(0) Then
             lblGrade.Text = strGrade(0)
         Else
             lblGrade.Text = strGrade(4)
         End If
     Next intGrade

I'm getting the error that says "Index was outside the bounds of the array." There's something wrong with the strGrade(intGrade+1), but no idea how to fix this.

I also tried this way
VB
For intGrade As Integer = 0 To 4
    If dblResults >= dblPer(intGrade) Then
        lblGrade.Text = strGrade(intGrade + 1)
    End If
Next intGrade


Still getting the same error?

Any help with the if statements? I don't understand what's wrong.

Thanks.

Here is the code in case you don't understand

Private Sub btnDisplay_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
        ' display the grade the student gets
        Dim dblPoints As Double
        Dim dblResults As Double
        Dim strGrade() As String =
            {"A", "B", "C", "D", "F"}
        Dim dblPer() As Double =
            {0.9, 0.8, 0.7, 0.6, 0.5}

        Double.TryParse(txtPoints.Text, dblPoints)
        ' displays the percentage the student recieves
        dblResults = dblPoints / CDbl(strMax)
        ' determines the grade

        For intGrade As Integer = 0 To 4
            If dblResults >= dblPer(intGrade) Then
                lblGrade.Text = strGrade(intGrade + 1)
            End If
        Next intGrade


    End Sub
   Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' get the total number of possible points
        strMax =
            InputBox("Enter the total possible points.", "Professor Carver")

    End Sub
Posted
Updated 6-Nov-11 7:23am
v4
Comments
LanFanNinja 9-Nov-11 18:49pm    
I have added a solution for your problem below even though it is a little late I wanted to post it anyway so you can know what the problems were just in case you did not figure it out.

In addition to what SAKryukov posted in solution 2 which does indeed need to be done! This is my modifications to your code to fix your problems.

change
VB
Dim dblPer() As Double =
    {0.9, 0.8, 0.7, 0.6, 0.5}


to this
VB
Dim dblPer() As Double =
    {0.9, 0.8, 0.7, 0.6, 0.0}


then change this
VB
For intGrade As Integer = 0 To 4
    If dblResults >= dblPer(intGrade) Then
        lblGrade.Text = strGrade(intGrade + 1)
    End If
Next intGrade


to this
VB
For intGrade As Integer = 0 To dblPer.Length
    If dblResults >= dblPer(intGrade) Then
        lblGrade.Text = strGrade(intGrade)
        Exit For
    End If
Next intGrade


A little late I know but I hope it helps.
 
Share this answer
 
Comments
byron luo 10-Nov-11 0:05am    
Thanks, and wow how come my gmail doesn't notify me if I can't see SA's reply.

But anyway, You're method does work, but the problem is I cannot use 0 in the array due to the instructions, and I have not learned the Exit For yet. the instructions wants to me to write the code in a complicated way. I guess it's too late now since I already turn my assignment in.
I didn't use the parallel array for letter A. Instead I use if-else statement to make decisions on A or else parallel arrays to B,C,D,F. Sorry if this doesn't make sense.
This is a simple bug; instead of lblGrade.Text = strGrade(intGrade + 1) use lblGrade.Text = strGrade(intGrade). The indexes should run from 0 to strGrade.Length - 1.

An important note: using immediate constant of 4 is not reliable. Actually, this is another bug which will not be manifested… just yet, but can easily haunt you later. Always use Length for arrays, Count for collections, etc.

A bonus advice: get rid of you Hungarian notation. For .NET, it makes no sense at all. Just make all names semantic, use proper case; generally, find (good) Microsoft naming conventions and use them. In practice, this is not insignificant detail, no, this is important — in practice.

—SA
 
Share this answer
 
Comments
byron luo 6-Nov-11 13:28pm    
Thanks for your reply, but I already tried your solution, even though I didn't use length. I typed 100 points in the inputbox, but then when I type 95, it displays F. It should display "A".

Any suggestions, I think there's something wrong with the if statement, but I can't see what the problem is.
Sergey Alexandrovich Kryukov 6-Nov-11 18:58pm    
No. You got the exception. If you did not you would not ask this question. So accept it.

As to display of "F" or "A", it's a matter of school arithmetic and simple logic -- follow it.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900