473,387 Members | 1,771 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Average


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
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #1
5 1791
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
Nov 20 '05 #2
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
----------------------------
"Carlo B" <ca****@global.co.za> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...

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
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 20 '05 #3
* Carlo B <ca****@global.co.za> scripsit:
Sub addnumber(ByVal m_num)


.... enable 'Option Strict On'.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #4
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


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #5
"Carlo B" <ca****@global.co.za> schrieb
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).
--
Armin

Nov 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Sara | last post by:
I am trying to find the average number of products of the set that is on rows...dynamically. For example, if I had states on rows and was looking at the average number of products, I would see...
2
by: Karen | last post by:
I inserted this statement and it worked great for the customers that have no payment history, but for the customers that have a history and an open invoice, it still used today's date in the mix...
6
by: J | last post by:
Kind of new at programming/vb.net. I'm doing this junky die roller program. Heres's what is supposed to happen: Roll 2 6-sided dies. Add rolls together put total in rolls(d6total). Display...
4
by: Gary | last post by:
Hi, I have a temperature conversion program down pat, but I was told to add an average, meaning, i need to get the average temperature for as many times as it was entered. i do not know where to...
3
by: C++Geek | last post by:
I need to get this program to average the salaries. What am I doing wrong? //Program to read in employee data and calculate the average salaries of the emplyees.
3
by: simpleeshelbee | last post by:
Hey guys, This is my second post and is URGENT!!!! My final assignment is due tonite for class and I have no idea how to write this program right! I am supposed to write a program that uses a...
3
by: mochatrpl | last post by:
I am looking for a way to make a query / report display the running average for total dollars. I have already set up a query to provide totals dollars per day from which a report graphly shows...
3
by: Salad | last post by:
http://www.mathwords.com/w/weighted_average.htm At the above link gives an example of a weighted average. It uses the following example: Grades are often computed using a weighted average....
3
by: vileoxidation | last post by:
Hello, and thanks for any help in advance! Basically, as the title says, I am looking for a way to print the number of times the user gave the program an input, and then also print the average of...
5
by: p3rk3le | last post by:
So, I'm about to do a sequential search on a table (n contents) of random numbers. I have to print the average between the number of comparisons and the contents of the table (n) and the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.