none
I need HELP programming my Average's and Grade's

    Question

  • I spent hours trying to get my program to work for a school assignment and I can't get my Average Function to work or display and same with Grades. I have to be able to open it in command prompt and able to read data from a note file here's what I got so far:

     Const AUTHOR As String = "name   Assignment 8"
        Const HEAD1 As String = _
          " Student_NAME       Test1   TEST2   TEST3   TEST4   TEST5   AVERAGE   GRADE"
        Const LINE = "-----------------------------------------------------------------------------"
        Const FMT = "0"
        '------------------------ Subroutine: Main()----------------------------------
        Sub Main()
            Dim Tokens As StringTokenizer
            Dim Delimiter As Char() = {" ", ",", ";", ":"}
            Dim Diskfile As String = "P08.DAT"

            Dim Name As String
            Dim FirstTestScore, SecondTestScore, ThirdTestScore, FourthTestScore, FifthTestScore, Average, Avg, Grade, grade1 As Decimal
            Dim gap As Integer

            console.clear()

            Console.WriteLine(AUTHOR & vbNewLine)
            gap = (80 - HEAD1.Length) \ 2
            Console.WriteLine(Space(gap) & HEAD1 & vbNewLine & Space(gap) & LINE)

            If Not File.Exists(Diskfile) Then
                Console.WriteLine("File: " & Diskfile & " does not exist")
                Console.WriteLine(vbNewLine & vbNewLine) : Exit Sub : End If

            FileOpen(1, Diskfile, OpenMode.Input)
            While Not EOF(1)
                Tokens = New StringTokenizer(LineInput(1), Delimiter)
                Name = Tokens.NextToken() & " " & Tokens.NextToken()
                Name = Name.PadRight(18)
                FirstTestScore = Tokens.NextToken()
                SecondTestScore = Tokens.NextToken()
                ThirdTestScore = Tokens.NextToken()
                FourthTestScore = Tokens.NextToken()
                FifthTestScore = Tokens.NextToken()
                Average = Avg
                Grade = grade1



                Console.WriteLine(Space(gap + 1) & Name _
                       & Format(FirstTestScore, FMT).PadLeft(5) _
                       & Format(SecondTestScore, FMT).PadLeft(8) _
                       & Format(ThirdTestScore, FMT).PadLeft(8) _
                       & Format(FourthTestScore, FMT).PadLeft(8) _
                       & Format(FifthTestScore, FMT).PadLeft(8) _
                       & Format(Average, FMT).PadLeft(7) _
                       & Format(Grade).PadLeft(10))

            End While


        End Sub 'Main()
        '-------------------- Subroutine: PrintScore() -----------------------------
        Sub PrintScore(ByVal P() As Integer)
            For k As Integer = 0 To P.GetUpperBound(0)
                Console.Write(Space(2) & P(k))
            Next
            Console.WriteLine(vbCrLf)
        End Sub 'PrintScore()
        '-------------------- Function: Sum() ------------------------------------
        Function Sum(ByVal P As Integer()) As Integer
            Dim arraySum = 0, k As Integer
            For k = 0 To P.GetUpperBound(0) : arraySum += P(k) : Next
            Return Sum
        End Function 'Sum()
        '-------------------- Function: Average() ------------------------------------
        Function Average(ByVal P As Integer()) As Integer
            Dim Avg = 0, k As Integer
            For k = 0 To P.GetUpperBound(0) : Avg += P(k) / 5 : Next
            Return Avg

        End Function 'Average()
        '------------------------ Function: Grade:() -------------------------
        Function Grade(ByVal Average As Integer) As Char
            Dim grade1 As Char

            If (Average > 89) Then : grade1 = "A"
            ElseIf (Average > 79) Then : grade1 = "B"
            ElseIf (Average > 69) Then : grade1 = "C"
            ElseIf (Average > 59) Then : grade1 = "D"
            Else : grade1 = "F"
            End If

            Return grade1
        End Function 'Grade()

    I see a few things wrong, but I can't figure out how to correct them)

    Saturday, April 15, 2017 3:21 AM

Answers

  • nevermind I just found what I was doing wrong. I had to type each one as a ByValAsDecimal separately

    example:  

    Function CalculateSum(ByVal FirstTestScore As Decimal, ByVal SecondTestScore As Decimal, ByVal ThirdTestScore As Decimal, ByVal FourthTestScore As Decimal, ByVal FifthTestScore As Decimal) As Decimal

    (Sorry for wasting your time and probably aggravating you, I was just going crazy on how difficult of a time I was having with this program because I spent about 8 hours trying to figure this crazy thing out)




    Saturday, April 15, 2017 6:30 AM

All replies

  • I spent hours trying to get my program to work for a school assignment and I can't get my Average Function to work or display and same with Grades.

    Firstly, you should not be doing this as a console app - it should be a Windows Forms app.  Secondly, you seem to be using some very old coding - you should find a much newer reference source for your code.

    The code you have posted calculates the average of an array of integers using the Average function.  You need to get your scores into an array, rather than the separate variables FirstTestScore, SecondTestScore etc.  Once the scores are in an array you can use the function.  So the bits that you need to review are 'Arrays and how to use them' and 'Functions and how to use them' or whatever the equivalent is in your text.

    If you are aware of a specific error, show the code that is wrong and describe the problem. They can be tackled one at a time.

    Saturday, April 15, 2017 3:36 AM
  • Thanks for the Reply.....well Yes it's Old Coding, I'm in visual basics class in college and in my class we use old coding and the thing is I can't figure out how to solve these issues, I can't get grades or average to show, that's the main 2 issues, but I'll try to get those number into an array to function. I'm in the middle of learning arrays and function so I'm kinda jumbled between the 2 at the moment. (any other suggesting will be greatly appreciated ) 

    Saturday, April 15, 2017 4:39 AM
  • I can't get grades or average to show, that's the main 2 issues.

    That's not sufficient detail.  According to my reading of the code, you will get either an error or something displayed.  If it's an error, provide the exact message and the line where it occurs.  If something is displayed, show what it is and what you reckon it should be.  The average will show zero because you aren't calculating it yet.

    You will have difficulty getting assistance with coding that is this old - most people do not use things like StringTokenizer or functions like Space().

    Saturday, April 15, 2017 5:12 AM
  • well, that sticks... but I wonder why do they teach old coding in college if it's no longer being used (kinda feels to be a waste of money on my part but oh well)... I was getting 0's When the code was displaying, but now I'm getting an Error BC30529: All parameters must be explicitly typed if any of them are explicitly typed. in the function section for Sum. Here is what it is now

     '-------------------- Function: CalculateSum() ------------------------------------
        Function CalculateSum(ByVal FirstTestScore, SecondTestScore, ThirdTestScore, FourthTestScore, FifthTestScore As Decimal)
            Dim sum As Decimal
            sum = FirstTestScore + SecondTestScore + ThirdTestScore + FourthTestScore + FifthTestScore

            Return Sum
        End Function 'CalculateSum()

    '------------------------------------------------------------------------------------------

    (any idea how I can fix this?) 

    Saturday, April 15, 2017 6:20 AM
  • nevermind I just found what I was doing wrong. I had to type each one as a ByValAsDecimal separately

    example:  

    Function CalculateSum(ByVal FirstTestScore As Decimal, ByVal SecondTestScore As Decimal, ByVal ThirdTestScore As Decimal, ByVal FourthTestScore As Decimal, ByVal FifthTestScore As Decimal) As Decimal

    (Sorry for wasting your time and probably aggravating you, I was just going crazy on how difficult of a time I was having with this program because I spent about 8 hours trying to figure this crazy thing out)




    Saturday, April 15, 2017 6:30 AM
  • nevermind I just found what I was doing wrong. I had to type each one as a ByValAsDecimal separately

    If the exercise is related to arrays, then that is not the appropriate solution.  The function as originally coded was set up to expect an array, not individual arguments.

    Saturday, April 15, 2017 6:49 AM