Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Program to enter number and then display average of them
Index -> Programming, Visual Basic and Other Basics -> Visual Basic Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
peterggmss




PostPosted: Tue Jan 09, 2007 1:49 pm   Post subject: Program to enter number and then display average of them

This is designed for a teacher to enter everyone's mark and then it will show that the average is. I can't figure out how to get the Average function to work properly and any help I can get would be appreciated.

code:
    Dim Marks(1 To 99999), LastMark, CurrentMark, NumberMarks As Double
    Dim Average As Double
   
Private Sub cmdEnter_Click()

    EnterMarks

    NumberMarks = CurrentMark
   
    AverageMark
   
    lblAverage.Caption = Average

End Sub

Function EnterMarks()

    CurrentMark = 0

    LastMark = "1" 'Makes the computer think that LastMark is not nothing

    Do Until LastMark = ""
        CurrentMark = CurrentMark + 1
        Marks(CurrentMark) = InputBox("Please enter mark " + Str(CurrentMark) & ".", "Enter Mark")
        LastMark = Marks(CurrentMark)
    Loop

End Function

Function AverageMark()

    Average = 0

    Do Until CurrentMark = 0
        If Not Marks(CurrentMark) = "" Then
            Average = Average + Marks(CurrentMark)
        End If
        CurrentMark = CurrentMark - 1
    Loop

    Average = Average / NumberMarks - 1

End Function
Sponsor
Sponsor
Sponsor
sponsor
Silent Avenger




PostPosted: Tue Jan 09, 2007 8:19 pm   Post subject: Re: Program to enter number and then display average of them

You could probably do this really easily without functions and make it more simple. Also, if you're using functions try to make them self contained and that way you'll easily be able to copy and paste the function that you may need in another program. Here's a simple example of one (don't ask about the variable names)

code:
Dim partition As Integer

Function we(hey As Integer)
hey = hey + 1
End Function


Private Sub Command1_Click()
Call we(partition)
Command1.Caption = partition
End Sub


Just put a command box on your form and paste in the code and see how it works. Anyways now on to your problem. The problem with your program all goes back to simple math rules. If you remember from math the BEDMAS rule which shows the order of solving an equation by Brackets Exponents Devision Multiplication Addition Subtraction (I think it's right). With this in mind and knowing that VB follows the same rules your equation
code:
Average = Average / NumberMarks - 1
The way this equation is set up it will divide average by number marks first then subtract 1. Try replacing it with this and your program will work perfectly:)

code:
Average = Average / (NumberMarks - 1)
Display posts from previous:   
   Index -> Programming, Visual Basic and Other Basics -> Visual Basic Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: