VB Console Program Help

Discussion in 'C#' started by novashun, Mar 14, 2010.

  1. #1
    Can anyone help me with this ?


    'Declare Array called Grades with 5 Elements
    Dim Grades(5) As String

    'Write array elements to screen using For loop
    Dim Average As Integer

    'Loop to have user input array values dynamically
    For index = 0 To 4 Step 1
    Console.Write("Enter a Grade " & index + 1 & ": ")
    Region(index) = Console.ReadLine()
    Next

    'Loop to print out array values
    Console.WriteLine(vbCrLf & "Your Average is:.")
    For index = 0 To 4 Step 1
    Console.Write(Average(grades) & vbCrLf)
    Next


    Console.Write(vbCrLf & "Press Enter Key to exit")
    Console.ReadLine()


    All I need to do is figure out how to take input of 5 grades then average them at the end. I dont know how or where to place the code for the actual averaging. It should be like count , /5 Anyone?
     
    novashun, Mar 14, 2010 IP
  2. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #2
    
        Sub Main()
            'First of all you were defining an array with 6 elements, so you only need five.
            'Grades(0), Grades(1), Grades(2), Grades(3), Grades(4) = Five elements
            Dim Grades(4) As String
            'We do not declare an Average variable as we will declare it as function later.
    
            'Loop to have user input array values dynamically
            'Step 1 is not necessary as it is the default for a for...next loop
            For index = 0 To 4
                Console.Write("Enter a Grade " & index + 1 & ": ")
                'Im not sure why you had Region, but it should be Grades
                Grades(index) = Console.ReadLine()
            Next
    
            'A For..loop is not necessary as we only need to display one value... the average. 
            Console.WriteLine(vbCrLf & "Your Average is:.")
    
            'This will pass the Grades array to the average function (that we will add further down)
            'and display the result
            Console.Write(Average(Grades) & vbCrLf)
    
            Console.Write(vbCrLf & "Press Enter Key to exit")
            Console.ReadLine()
        End Sub
    
        'This is where we declare the Average function
        'We declare it as Double because the average will not always be a whole number
        Function Average(ByVal arrGrades As Array) As Double
            Dim total As Integer
    
            'Add all the values of the array together.
            For i = 0 To arrGrades.Length - 1
                'the following line is the equivalent of:
                'total = total + Int(arrGrades(i))
                'We use the Int function to convert the values from a String to an Integer.
                total += Int(arrGrades(i))
            Next
    
            'Divide by the number of values and return the result
            Average = total / arrGrades.Length
        End Function
    
    Code (markup):
    The code has been commented to assist you in understanding it, If you have any further questions, let me know.
     
    camjohnson95, Mar 14, 2010 IP