Average

C

Carlo B

I need to store numbers in an array and by using a class I need to
calculate the average of the numbers entered. I cannot get the text box
to return the total of the numbers in the average function - it always
returns a 0.
What is wrong with this coding?
Thanks
Carlob1


Dim statistic(50) As statistics
Dim lastnumber As Integer
Private Sub btnRecord_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnRecord.Click
Dim st As statistics
st = New statistics()
lastnumber += 1
st.num = CDbl(txtNumber.Text)
statistic(lastnumber) = st
txtNumber.Text = 0
txtNumber.Focus()
End Sub

Private Sub btnAverage_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnAverage.Click
txtAverage.Text = statistic(lastnumber).average
End Sub
End Class
Class statistics
Private m_count As Integer
Private m_num As Double
Public total As Double
Public Property count() As Integer
Get
Return m_count
End Get
Set(ByVal Value As Integer)
m_count = Value
End Set
End Property
Public Property num() As Double
Get
Return m_num
End Get
Set(ByVal Value As Double)
m_num = Value
End Set
End Property
Sub addnumber(ByVal m_num)
total = m_num
total += total
End Sub
Function average() As Double
Dim av As Double
av = total
Return av
End Function
End Class
 
C

Cor Ligthert

Hi Carlo,

You want to get return a total, as average, however where did you fill that
total, i see a sub Addnumber however never used as far as I did see?

Cor
 
K

Ken Tucker [MVP]

Hi,


Dim arDbl(50) As Double

For x As Integer = 0 To 49
arDbl(x) = Rnd(200) * 2000
Console.WriteLine(arDbl(x).ToString)
Next

Dim dblTotal As Double = 0

For i As Integer = 0 To 49
dblTotal += arDbl(i)
Next

Dim dblAvg As Double = dblTotal / 50
Console.WriteLine(String.Format("Average {0} ", dblAvg.ToString))


Ken
 
C

Carlo B

Hi
I'm not quite sure what is meant with 'enable 'option strict on' to
solve the problem.
I've now got it to show 'NaN' in the textbox because I have referenced
'addnumber' in the 'average' function. But it is still not giving me the
average.
Any ideas?
Thanks


Dim statistic(50) As statistics
Dim lastnumber As Integer
Private Sub btnRecord_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnRecord.Click
Dim st As statistics
st = New statistics()
lastnumber += 1
st.num = CDbl(txtNumber.Text)
statistic(lastnumber) = st
txtNumber.Text = 0
txtNumber.Focus()
End Sub

Private Sub btnAverage_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnAverage.Click
txtAverage.Text = CStr(statistic(lastnumber).average)
End Sub
End Class
Class statistics
Private m_count As Integer
Private m_num As Double
Public total As Double
Public Property count() As Integer
Get
Return m_count
End Get
Set(ByVal Value As Integer)
m_count = Value
End Set
End Property
Public Property num() As Double
Get
Return m_num
End Get
Set(ByVal Value As Double)
m_num = Value
End Set
End Property
Sub addnumber(ByVal m_num)
total = m_num
total += CDbl(total)
End Sub
Function average() As Double
Dim av As Double
Dim addnumber As Double = total
av = CDbl(addnumber / m_count)
Return av
End Function
End Class
 
A

Armin Zingler

Carlo B said:
Hi
I'm not quite sure what is meant with 'enable 'option strict on'
to solve the problem.

It means that we can no try your code because it is not compilable, so it's
hard to help. In addition, Option strict excludes obvious errors, that's why
it should always be enabled first.

I don't understand the code. You've got several numbers, but you are trying
to return the average from a single instance althought they don't have a
relation to each other. You never call 'addnumber', consequently 'total' is
always 0. You also never increase 'count', consequently average always
returns 0/0 which is NaN (not a number because of the division by zero).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top