help with coding program

dave1816

Member
Joined
Dec 11, 2007
Messages
8
Programming Experience
Beginner
hey im new here and newish to vb.net which im studying at uni. Ive created a code which calculates students grade points and displays the No of cat points they have got alltogether. An example of this would be if they got 13 -15 then they get a first for that module and they would get 1 displayed in a textbox , if they get another then they get two....and so on....

anyway geting back to the point, i have to display the highest, lowest and average grade point and not sure which is the easyest way to do this based on the code ive pasted below. I was thinking an array but i dont understand the feature, although knowing what it can do. i also have a secound form which i will not paste on here as it only display the results of the variables ect "firstclass" in a text box. Anyway any ideas what approach i should take to carryout the calculations? p.s havn't named the controlls yet as im lazy,lol yet there aint tat many.

VB.NET:
 Public Class Form1
    Public firstclass As Double
    Public Uppersecound As Double
    Public Lowersecound As Double
    Public Thirdclass As Double
    Public fail As Double
    Public Totalresults As Double

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim results As String

      
        results = TextBox2.Text

        results = check(results)

        Totalresults = Totalresults + results


        If RadioButton1.Checked Then
            calculate15(results)
        End If

        If RadioButton2.Checked Then
            calculate30(results)

        Else
            MsgBox("Please select a total score your grade is marked out of from the point grade option box e.g. 15 or 30")
        End If



    End Sub

    Private Function check(ByVal checkresults) As String


        If IsNumeric(checkresults) = False Then

            MsgBox("Please insert a numeric value e.g. 1 not ONE")
            checkresults = -1
            check = CDbl(checkresults)
        Else
            check = CDbl(checkresults)
        End If


    End Function

    Private Sub calculate15(ByVal results)


        If results > 12 And results < 16 Then
            firstclass = firstclass + 1
        End If

        If results > 9 And results < 13 Then
            Uppersecound = Uppersecound + 1
        End If

        If results > 6 And results < 10 Then
            Lowersecound = Lowersecound + 1
        End If

        If results > 3 And results < 7 Then
            Thirdclass = Thirdclass + 1
        End If

        If results >= 0 And results < 4 Then
            fail = fail + 1
        Else
            MsgBox("The number you inserted is an invalid number! Note: if you wish to caculate your score out of 30 grade points, please remember to select 30 from the selection box, otherwise enter an invalid number from 0-15")
        End If


       
        If results = -1 Then
            TextBox2.Text = ""
            TextBox2.Focus()
        End If


    End Sub
    Private Sub calculate30(ByVal results)

        If results > 25 And results < 31 Then
            firstclass = firstclass + 1
        End If

        If results > 20 And results < 26 Then
            Uppersecound = Uppersecound + 1
        End If

        If results > 16 And results < 21 Then
            Lowersecound = Lowersecound + 1
        End If

        If results > 13 And results < 17 Then
            Thirdclass = Thirdclass + 1
        End If

        If results >= 0 And results < 14 Then
            fail = fail + 1


        End If

        If results = -1 Then
            TextBox2.Text = ""
            TextBox2.Focus()

        Else
            MsgBox("The number you inserted is an invalid number! Note: if you wish to caculate your score out of 30 grade points, please remember to select 30 from the selection box, otherwise enter an invalid number from 0-15")
        End If




    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Form2.Show()
        Me.Close()
    End Sub
End Class

hope u can help

dave
 
Last edited by a moderator:
I'm not completely sure what you're trying to do, but I think I might be able to help. Basically, if I'm understanding this right, every time you add (or calculate) another grade point you'll want to compare them to the previous ones.

I think you'd have class level variables called intHighestGrade, intLowestGrade, and intAverageGrade.

Every time you add one you have an if statement similar to
VB.NET:
If GradeInQuestion > intHighestGrade
intHighestGrade = GradeInQuestion
End If

VB.NET:
If GradeInQuestion < intLowestGrade
intLowestGrade = GradeInQuestion
End If

For the average, you'd need to have a variable that accumulated once every time a grade was added and one variable that held the total of all the grades. At the point where you need to calculate the average you'd simply go:

VB.NET:
intAverage = intTotalOfGrades / intNumberOfGrades.

I hope that made sense and apologize if it didn't.
 
Yes thats it i want to compair the number inserted to the other numbers inserted to find out the highest number...wish i wrote it this easy b4,lol so if i enter 15 ,13, 9 , 8 it would compair all numbers and tell me the highest number.

I did come up with something simillar but not as clear as your explination, however when i tryed the code based on what you said the only issue i had is that when calculating the next total the "intHighestGrade" restores it default value "0", how do i approach this problem?

The only way i can think of is storing it in a textbox and obtaining the value form the textbox..any other possibilitys?

My code statement for finding the highets grade is:

If results > highestgrade Then
highestgrade = results
End If

Thanks for the reply though m8 much appreciated
 
ah simple error i just had to set the highestgrade ="0" an input it where my form loads weird how tat effected the code ey....

Thanks anyway

ps u sure get alot of reply in here:eek:lol
 
I cleaned up your code a little bit so I could understand what you're currently doing and how it's being done. If my code below doesn't do exactly what yours does, sorry about that
VB.NET:
Public Class Form1
    Public firstclass As Integer
    Public Uppersecound As Integer
    Public Lowersecound As Integer
    Public Thirdclass As Integer
    Public fail As Integer
    Public Totalresults As Double

    Private Grades As New List(Of Double)

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim results As Double = 0
      
        Double.TryParse(TextBox2.Text, results)
        
        Totalresults += results
        Grades.Add(results)

        If RadioButton1.Checked Then
            calculate15(results)
        ElseIf RadioButton2.Checked Then
            calculate30(results)
        Else
            MsgBox("Please select a total score your grade is marked out of from the point grade option box e.g. 15 or 30")
        End If
    End Sub

    Private Sub calculate15(ByVal results As Double)

        If results > 12 AndAlso results < 16 Then firstclass += 1
        If results > 9 AndAlso results < 13 Then Uppersecound += 1
        If results > 6 AndAlso results < 10 Then Lowersecound += 1
        If results > 3 AndAlso results < 7 Then Thirdclass += 1
        If results >= 0 AndAlso results < 4 Then fail += 1
    End Sub

    Private Sub calculate30(ByVal results)
        If results > 25 AndAlso results < 31 Then firstclass += 1
        If results > 20 AndAlso results < 26 Then Uppersecound += 1
        If results > 16 AndAlso results < 21 Then Lowersecound += 1
        If results > 13 AndAlso results < 17 Then Thirdclass += 1
        If results >= 0 AndAlso results < 14 Then fail += 1
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Form2.Show()
        Me.Close()
    End Sub
End Class

What I have done was added a Grades list (of type double) then when you grab the input from the user (which I would recommend using a NumericUpDown control instead of a TextBox) it stores the value in the list via it's add() method. This neat list stores each grade, and because the whole thing is of type 'Double' you know all the values are Doubles, math is easier since there's no conversion. Once you're done grabbing the data, simply Sort the list (using the List's Sort method()) and the first element (index 0) is the lowest grade, and the highest element (index(list.count - 1)) is the highest grade. To get the average simply loop the list and divide the total by the .Count() - 1 and there you go
 
right thanks for that i changed my code now like yours although i stuck on geting the data:mad: im not sure 100% what im doing on that part i have input

grades.sort(index 0) is this how you obtain the lowest number?

i have been trying to find how to obtain sorted data on the internet and browsing through my visualbaic 2005 book and cant find anything!:(

can u or anyone explain how to obtain the data or pass me a sutiable link which explains how to as data in a list

cheers Dave and again thanks..
 
Back
Top