Results 1 to 11 of 11

Thread: Average Calculator

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2001
    Posts
    39

    Average Calculator

    Could some do the following:

    Make a program that will allow the user to enter up to ten digits and then average them. The numbers entered can have more than one digit and can have only one decimal point.

    Requirements:

    1.Use a procedure in a standard module to display the digit on the screen.

    2. Use a constant for the upper limit of the array.

    3.The user should not be allowed to store more than 10 numbers.

    4. The users should only be allowed to press cmdAverage if there are numbers in the array.

    5. the user should only be allowed to enter one decimal point per array.

    6.If the display area is blank the user should not be able to store a value. (I think you should use the Len() function)

    Thats it, Anyone want to try?

  2. #2
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Will this do ?
    Attached Files Attached Files
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  3. #3
    Hyperactive Member techman2553's Avatar
    Join Date
    Mar 2001
    Location
    <- To your left.
    Posts
    362
    This sounds like a classroom homework assignment ????
    ----------

  4. #4
    Addicted Member Nice's Avatar
    Join Date
    Oct 2000
    Location
    Germany
    Posts
    144
    exactly..i felt the same too..

  5. #5

    Thread Starter
    Member
    Join Date
    Mar 2001
    Posts
    39

    Assignment

    Yes it is an assignment, so what, Im gonna change it up.

    The program needs to use control array, with numbers 0 -9

    thank you to everyone that tries

  6. #6
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    my example uses a control array ...
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  7. #7
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390
    Do the numbers entered have to be in 1 textbox? or can you use 10?

    code tips...if you can use 10 boxes (easiest)

    in the KeyPress event check for more than 1 "."


    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
    If KeyAscii = Asc(".") And InStr(Text1.Text, ".") Then KeyAscii = 0
    End Sub
    
    VBBrowser v2.2.4

    and...you say "store" the number? what are you looking to do...actually store data in a file?
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  8. #8

    Thread Starter
    Member
    Join Date
    Mar 2001
    Posts
    39

    Buttons

    i know I looked at it , see the problem im having with mine is that I have to use 10 buttons and 1 display and I have to add the numbers one at a time with in a loop I think, and I have no idea how to do that.

  9. #9
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    mine adds them in a loop.
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  10. #10
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390
    Here is code...
    New Form

    add a text box called txtNum
    Copy and paste it 9 more times. Choose YES for create control array(10 total..)
    add a cmnd btn named cmdAverage
    drop this code in the form:

    Code:
    Private Sub cmdAverage_Click()
    For x = 0 To txtNum.Count - 1
     If txtNum(x).Text <> "" Then
     tmp = tmp + Val(txtNum(x).Text)
     cnt = cnt + 1
     End If
    Next
    MsgBox "The Average of the " & cnt & " Numbers entered is : " & tmp / cnt
    
    End Sub
    
    Private Sub txtNum_Change(Index As Integer)
    For x = 0 To txtNum.Count - 1
     If txtNum(x).Text <> "" Then cmdAverage.Enabled = True: Exit Sub
    Next
    cmdAverage.Enabled = False
    End Sub
    
    Private Sub txtNum_GotFocus(Index As Integer)
    txtNum(Index).SelStart = 0
    txtNum(Index).SelLength = Len(txtNum(Index))
    End Sub
    
    Private Sub txtNum_KeyPress(Index As Integer, KeyAscii As Integer)
    If KeyAscii = Asc(".") And InStr(txtNum(Index).Text, ".") Then KeyAscii = 0
    If IsNumeric(Chr(KeyAscii)) = True Or KeyAscii = Asc(".") Or KeyAscii = 8 Then Exit Sub
    KeyAscii = 0
    End Sub
    
    VBBrowser v2.2.4
    This code:
    Disables Button if no nums are entered
    allows only numbers and only 1 "." per numb
    uses a control array
    loops through and counts/ add num to get average..
    also auto selects enter num when focus is given (so user doesnt have to delete)

    I have not looked at plenderj's code but knowing him I am guessing his code is complete as well.
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  11. #11
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Of course

    Code:
    Private Sub Command1_Click()
        Dim i As Long
        Dim numNumbers As Long
        Dim tempX As Double
        For i = 0 To txtNum.UBound
            If (txtNum(i) <> "") Then
                If (IsNumeric(txtNum(i))) Then
                    tempX = tempX + txtNum(i)
                    numNumbers = numNumbers + 1
                End If
            End If
        Next i
        If (numNumbers <> 0) Then MsgBox "Average : " & tempX / numNumbers
    End Sub
    
    Private Sub Form_Load()
        Dim i As Long
        For i = 1 To 9
            Load txtNum(i)
            txtNum(i).Move txtNum(0).Left, i * (130 + txtNum(0).Height)
            txtNum(i).Visible = True
            Load lblNum(i)
            lblNum(i).Move lblNum(0).Left, i * (130 + txtNum(0).Height)
            lblNum(i).Visible = True
            lblNum(i).Caption = "Number " & i & " : "
        Next i
    End Sub
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width