-
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?
-
1 Attachment(s)
-
This sounds like a classroom homework assignment ????
:confused: :rolleyes: :rolleyes: :eek: :rolleyes: :rolleyes: :confused:
-
exactly..i felt the same too..
-
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
-
my example uses a control array ...
-
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?
-
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.
-
mine adds them in a loop.
-
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. ;)
-
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