0

Hi everyone.
I got tuday an exercise:
Write a program to calculate the average of three exam marks. The input and output should be done by subprograms GetMarks and DisplayAverage respectively. the calculation should be done by a function, CalcAverage.
This is my code:

Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click

Dim student As String, exam1 As Integer, exam2 As Integer, exam3 As Integer
Dim average As Double
lstResult.Items.Clear()

        GetMarks(student, exam1, exam2, exam3)
        average = calcAverage(exam1, exam2, exam3)
        DisplayAverage(student, average)
    End Sub
    Private Sub GetMarks(ByRef student As String, ByVal exam1 As Integer, ByVal exam2 As Integer, ByVal exam3 As Integer)
        student = txtName.Text
        Integer.TryParse(txtExam1.Text, exam1)
        Integer.TryParse(txtExam2.Text, exam2)
        Integer.TryParse(txtExam3.Text, exam3)
    End Sub
    Private Function CalcAverage(ByVal exam1 As Integer, ByVal exam2 As Integer, ByVal exam3 As Integer) As Double
        Return (exam1 + exam2 + exam3) / 3
    End Function
    Private Sub DisplayAverage(ByVal student As String, ByVal average As Double)
        lstResult.Items.Add("Average for " & student & " is " & average)

I think that I messes up smthg and it's showing me average = 0.
Can any correct it for me

Edited by peter_budo: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)

3
Contributors
2
Replies
6
Views
7 Years
Discussion Span
Last Post by __avd
0

Hi.

when you call this out, student, exam1, exam2 and exam3 doesn't have any value.

GetMarks(student, exam1, exam2, exam3)
0

>think that I messes up smthg and it's showing me average = 0.
Can any correct it for me

ByRef arguments/parameters for marks are needed.

Private Sub GetMarks(ByRef student As String, ByRef exam1 As Integer, ByRef exam2 As Integer, ByRef exam3 As Integer)
 student = txtName.Text
 Integer.TryParse(txtExam1.Text, exam1)
 Integer.TryParse(txtExam2.Text, exam2)
 Integer.TryParse(txtExam3.Text, exam3)
End Sub
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.