Display Grade based on Points

Creating and using parallel array

Page 1 of 1

6 Replies - 8472 Views - Last Post: 15 November 2011 - 07:32 PM Rate Topic: -----

#1 Perplexedwon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 01-March 10

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.

here is my code:
' Name:         Carver Project
' Purpose:      Displays a grade based on the number of points the user enters
' Programmer:   <Paul J. Williams> on <February 27, 2010>

Option Explicit On
Option Strict On
Option Infer Off

Public Class frmMain

    Private Sub btnExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

    Private Sub txtPoints_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtPoints.Enter
        txtPoints.SelectAll()
    End Sub

    Private Sub txtPoints_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtPoints.KeyPress
        ' accepts only numbers and the Backspace key

        If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso e.KeyChar <> ControlChars.Back Then
            e.Handled = True
        End If
    End Sub

    Private Sub txtPoints_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtPoints.TextChanged
        lblGrade.Text = String.Empty
    End Sub

    Private Sub btnDisplay_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
        ' Display the letter grade associated with a students total points earned.

        Dim intGrade As Integer
        Dim searchForPoints As Integer
        Dim intPoints() As Integer = {450, 400, 350, 300, 0}
        Dim strGrades() As String = {"A", "B", "C", "D", "F"}

        ' Assign a letter grade to a variable. 

        searchForPoints = Integer.Parse(Me.txtPoints.Text)

        ' Search the array for the grades

        If searchForPoints >= 0 Or searchForPoints <= 500 Then

            'End If

            For intGrade = 0 To 4
                If searchForPoints < intPoints(intGrade) Then

                    lblGrade.Text = strGrades(intGrade + 1)


                End If
            Next intGrade
        End If

    End Sub
End Class




Is This A Good Question/Topic? 0
  • +

Replies To: Display Grade based on Points

#2 Merddin  Icon User is offline

  • New D.I.C Head

Reputation: 12
  • View blog
  • Posts: 37
  • Joined: 19-February 10

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



Hope that helps and good luck with the coding.
Was This Post Helpful? 0
  • +
  • -

#3 myork  Icon User is offline

  • D.I.C Head

Reputation: 41
  • View blog
  • Posts: 129
  • Joined: 04-March 09

Re: Display Grade based on Points

Posted 01 March 2010 - 11:04 PM

View PostMerddin, 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.
searchForPoints = 401

For intGrade = 0 To 4
intGrade = 0:
searchForPoints < intPoints(0) == 401 < 450 == True
  lblGrade.Text = strGrades(intGrade + 1) == "B"  'What we want'

intGrade = 1:
searchForPoints < intPoints(1) == 401 < 400 == False
  lblGrade.Text = "A"  'Not what we want'


This post has been edited by myork: 01 March 2010 - 11:05 PM

Was This Post Helpful? 0
  • +
  • -

#4 xoptimizedrsx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 03-October 11

Re: Display Grade based on Points

Posted 15 November 2011 - 07:02 PM

How about just doing it like this?
If you add in two Else IF you can sync all numbers in the input box as well as go above the 450 count.

' Name:         Carver Project
' Purpose:      Displays a grade based on the number of points the user enters
' Programmer:   <Michael Cobb> on <11-15-2011>

Option Explicit On
Option Strict On
Option Infer Off

Public Class frmMain

    Private Sub btnExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

    Private Sub txtPoints_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtPoints.Enter
        txtPoints.SelectAll()
    End Sub

    Private Sub txtPoints_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtPoints.KeyPress
        ' accepts only numbers and the Backspace key

        If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso e.KeyChar <> ControlChars.Back Then
            e.Handled = True
        End If
    End Sub

    Private Sub txtPoints_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtPoints.TextChanged
        lblGrade.Text = String.Empty
    End Sub

    Private Sub btnDisplay_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
        ' Display the letter grade associated with a students total points earned.

        Dim intGrade As Integer
        Dim searchForPoints As Integer
        Dim intPoints() As Integer = {450, 400, 350, 300, 0}
        Dim strGrades() As String = {"A", "B", "C", "D", "F"}

        ' Assign a letter grade to a variable. 

        searchForPoints = Integer.Parse(txtPoints.Text)

        ' Search the array for the grades

        For intGrade = 0 To 4

            If searchForPoints < intPoints(intGrade) Then
                lblGrade.Text = strGrades(intGrade + 1)

            ElseIf searchForPoints = intPoints(intGrade) Or searchForPoints < intPoints(intGrade) Then
                lblGrade.Text = strGrades(intGrade)

            ElseIf searchForPoints > intPoints(intGrade) Then
                lblGrade.Text = strGrades(0)

            End If
        Next

    End Sub
End Class



Was This Post Helpful? 0
  • +
  • -

#5 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Display Grade based on Points

Posted 15 November 2011 - 07:08 PM

You could retrieve the values, then do this:
If grade >=450 AndAlso =< 500 Then
lblGrade.Text = "A"
ElseIf 
'Conditions follow
End If


So basically what that does is just test the conditions, if the number is less than 500 and greater than 450.
Was This Post Helpful? 0
  • +
  • -

#6 xoptimizedrsx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 03-October 11

Re: Display Grade based on Points

Posted 15 November 2011 - 07:25 PM

View Postxoptimizedrsx, on 15 November 2011 - 07:02 PM, said:

How about just doing it like this?
If you add in two Else IF you can sync all numbers in the input box as well as go above the 450 count.

' Name:         Carver Project
' Purpose:      Displays a grade based on the number of points the user enters
' Programmer:   <Michael Cobb> on <11-15-2011>

Option Explicit On
Option Strict On
Option Infer Off

Public Class frmMain

    Private Sub btnExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

    Private Sub txtPoints_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtPoints.Enter
        txtPoints.SelectAll()
    End Sub

    Private Sub txtPoints_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtPoints.KeyPress
        ' accepts only numbers and the Backspace key

        If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso e.KeyChar <> ControlChars.Back Then
            e.Handled = True
        End If
    End Sub

    Private Sub txtPoints_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtPoints.TextChanged
        lblGrade.Text = String.Empty
    End Sub

    Private Sub btnDisplay_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
        ' Display the letter grade associated with a students total points earned.

        Dim intGrade As Integer
        Dim searchForPoints As Integer
        Dim intPoints() As Integer = {450, 400, 350, 300, 0}
        Dim strGrades() As String = {"A", "B", "C", "D", "F"}

        ' Assign a letter grade to a variable. 

        searchForPoints = Integer.Parse(txtPoints.Text)

        ' Search the array for the grades

        For intGrade = 0 To 4

            If searchForPoints < intPoints(intGrade) Then
                lblGrade.Text = strGrades(intGrade + 1)

            ElseIf searchForPoints = intPoints(intGrade) Or searchForPoints < intPoints(intGrade) Then
                lblGrade.Text = strGrades(intGrade)

            ElseIf searchForPoints > intPoints(intGrade) Then
                lblGrade.Text = strGrades(0)

            End If
        Next

    End Sub
End Class




correction... sorry
the lower section is as follows to get things correctly reading as parallels. you can possibly remove one of the Else if's and it will still function as written in my book.

This Book is terrible and out dated..IMO

For intGrade = 0 To 4

            If searchForPoints < intPoints(intGrade) Then
                lblGrade.Text = strGrades(intGrade + 1)

            ElseIf searchForPoints = intPoints(intGrade) Or searchForPoints < intPoints(intGrade) Then
                lblGrade.Text = strGrades(intGrade)

            ElseIf searchForPoints > 450 Then
                searchForPoints = intPoints(intGrade)
                lblGrade.Text = strGrades(intGrade)
            End If
        Next


Was This Post Helpful? 0
  • +
  • -

#7 xoptimizedrsx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 03-October 11

Re: Display Grade based on Points

Posted 15 November 2011 - 07:32 PM

View PostjimmyBo, on 15 November 2011 - 07:08 PM, said:

You could retrieve the values, then do this:
If grade >=450 AndAlso =< 500 Then
lblGrade.Text = "A"
ElseIf 
'Conditions follow
End If


So basically what that does is just test the conditions, if the number is less than 500 and greater than 450.

That would work also But the book and Professor will want the lblgrade.text= strpoint(????)

They the Teachers) are so picky sometimes...

as for my personal project I still cant figure out my darn COM port use selected... I know its doable, but I'm to new programming and get lost on that area.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1