Below is the program from today’s lesson. It is not completed, I would like you to Change the program to display the average with two decimal places. Please add your solution with a comment below.
Private Sub CmdStart_Click()
'load an Array with data from a list box
'list box is hidden on the form
'List Boxes count from 0 so add a heading to the fist item
Dim IntNumber(20) As Integer
Dim StrName(20) As String
Dim intavg As Integer
Call Load_Array(IntNumber(), StrName())
Call Calc_Average(IntNumber(), intavg)
Call Display_Array(IntNumber(), StrName(), intavg)
End Sub
Private Sub Load_Array(ByRef Number() As Integer, ByRef StrName() As String)
Dim Counter As Integer 'local Variable
For Counter = 1 To 20
Number(Counter) = LstNumbers.List(Counter)
StrName(Counter) = LstNames.List(Counter)
Next
End Sub
Private Sub Calc_Average(ByRef IntNumber() As Integer, ByRef intavg As Integer)
Dim IntSum As Integer
For Counter = 1 To 20
IntSum = IntSum + IntNumber(Counter)
Next
intavg = IntSum / 20
End Sub
Private Sub Display_Array(ByRef Number() As Integer, ByRef StrName() As String, ByVal intavg)
'Standard procedure as used in previous programs
Dim Counter As Integer 'local Variable
For Counter = 1 To 20
Form1.Print StrName(Counter) & " scored " & Number(Counter) & "%"
Next
Form1.Print "Average = " & intavg & "%"
End Sub
For1.print “Average = ” & intavg & “.00”
intavg = Format(intavg, “0.00”)
Dim intavg As Integer
Change this line to read
Dim intavg As Single
intavg = format.”0.00″
intavg = IntSum / 20 * 100
Form1.Print “Average = ” & intavg * 100 & “%”
Would you type in at a point “0.00” in order to achieve the 2 decimal places?
Or when saying “intavg = IntSum / 20” instead say you are dividing by “20.00”, and also change the variable array answers to single instead of integer?
Private Sub cmdCalculate_Click()
Dim strDecimal(2) as String
decimal = intSum + intNumber(counter)
Round (intSum + intNumber(counter),2 )
random stab at this but i thought if you could declare the variable and add the two together and round them it would come up !
Remember that Integers do not have decimals!
So we need to change the average to a single
DIM SngAvg as Single
Then we can use the format command – VB6 (check your notes)
Form1.print “Average = ” & Format(sngAvg,”0.00%”)
NOTICE – 0.00 is in quotes!
You may find that this increase your answer by a factor of 100, how would you fix this?