# How do you find the average?

Page 1 of 1

## 14 Replies - 1563 Views - Last Post: 23 March 2010 - 01:05 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=163640&amp;s=83d669cd668d770faa59edf9bc51aad5&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 mbrother64

Reputation: -9
• Posts: 78
• Joined: 30-August 09

# How do you find the average?

Posted 22 March 2010 - 06:29 PM

Heres my code so far:

```Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Subject As Integer
Subject = InputBox("How many products")
Dim i As Integer
Dim num As Integer
Dim total As Integer
Dim average As Integer

For i = 1 To Subject
num = (Val(InputBox("Enter sales amount:")))

total = total + num
average = average / num

Next

TextBox1.Text = total
TextBox2.Text = average

End Sub
```

Only "0" appears in the TextBox2 and not the average number.

This post has been edited by mbrother64: 22 March 2010 - 06:43 PM

Is This A Good Question/Topic? 0

## Replies To: How do you find the average?

### #2 iHack

Reputation: 1
• Posts: 29
• Joined: 16-October 09

## Re: How do you find the average?

Posted 22 March 2010 - 07:01 PM

mbrother64, on 22 March 2010 - 05:29 PM, said:

Heres my code so far:

```Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Subject As Integer
Subject = InputBox("How many products")
Dim i As Integer
Dim num As Integer
Dim total As Integer
Dim average As Integer

For i = 1 To Subject
num = (Val(InputBox("Enter sales amount:")))

total = total + num
average = average / num

Next

TextBox1.Text = total
TextBox2.Text = average

End Sub
```

Only "0" appears in the TextBox2 and not the average number.

Tell me if this works...

```            For i = 1 To Subject
num = (Val(InputBox("Enter sales amount:")))

total = (CInt(total) + CInt(num))
average = (CInt(average) / CInt(num))

Next

TextBox1.Text = total
TextBox2.Text = average
```

You might want to try reversing "TextBox2.Text = average" to "average = TextBox2.Text"... This is off the top of my head. Reply back if it does or doesn't and I'll test something else out...

This post has been edited by iHack: 22 March 2010 - 07:02 PM

### #3 mbrother64

Reputation: -9
• Posts: 78
• Joined: 30-August 09

## Re: How do you find the average?

Posted 22 March 2010 - 07:45 PM

No It still dosent work. Getting the total works, but All I get for the average is 0.

### #4 ZRonZ

Reputation: 27
• Posts: 198
• Joined: 09-January 09

## Re: How do you find the average?

Posted 22 March 2010 - 07:50 PM

What is the correct formula for average?

You're using: average = average / num

Num is the sales amount from the input box and the variable average is declared as an integer but with no value. Therefore it's 0

The way you're calculating, the average = 0 divided by whatever the user inputs means that the "average" will always be 0 (unless the user enters 0).

Everything you need to know has been explained in previous post replies. Figure out the real formula for average, capture the one piece of data you need that you are not getting using methods you are already using for other data and you've got it.

### #5 iHack

Reputation: 1
• Posts: 29
• Joined: 16-October 09

## Re: How do you find the average?

Posted 22 March 2010 - 08:30 PM

ZRonZ, on 22 March 2010 - 06:50 PM, said:

What is the correct formula for average?

You're using: average = average / num

Num is the sales amount from the input box and the variable average is declared as an integer but with no value. Therefore it's 0

The way you're calculating, the average = 0 divided by whatever the user inputs means that the "average" will always be 0 (unless the user enters 0).

Everything you need to know has been explained in previous post replies. Figure out the real formula for average, capture the one piece of data you need that you are not getting using methods you are already using for other data and you've got it.

Heh, I assumed his vars were right...

### #6 mbrother64

Reputation: -9
• Posts: 78
• Joined: 30-August 09

## Re: How do you find the average?

Posted 22 March 2010 - 09:00 PM

"Everything you need to know has been explained in previous post replies."

Where?

"Figure out the real formula for average"

Thats what I'm trying to figure out! Whats the proper formula for finding the average?

### #7 PsychoCoder

Reputation: 1619
• Posts: 19,853
• Joined: 26-July 07

## Re: How do you find the average?

Posted 22 March 2010 - 09:06 PM

Formula for average

Quote

SumOfAllItems/NumberOfItems

### #8 mbrother64

Reputation: -9
• Posts: 78
• Joined: 30-August 09

## Re: How do you find the average?

Posted 22 March 2010 - 09:19 PM

Oh I see. And how do I get the number of items?

### #9 iHack

Reputation: 1
• Posts: 29
• Joined: 16-October 09

## Re: How do you find the average?

Posted 22 March 2010 - 10:50 PM

mbrother64, on 22 March 2010 - 08:19 PM, said:

Oh I see. And how do I get the number of items?

You need to think about it... I suggest you look at your code... Perhaps *hint* make a counter...

### #10 mbrother64

Reputation: -9
• Posts: 78
• Joined: 30-August 09

## Re: How do you find the average?

Posted 23 March 2010 - 02:24 AM

How do I get the sum of items?

Ive since tried

```Dim Subject As Integer
Subject = InputBox("How many products")
Dim i As Integer
Dim num As Integer
Dim word As String
Dim average As Integer

For i = 1 To Subject
num = (Val(InputBox("Enter sales amount:")))
average = num / Subject '< I tried this line

Next
TextBox2.Text = average
```

But nope, it still dosent work.

"Subject" gets me the num of all items but "num" only calculates the last sum entered and not the total sum. How do I get the total sum of items?

This post has been edited by mbrother64: 23 March 2010 - 03:13 AM

### #11 motcom

• D.I.C Lover

Reputation: 288
• Posts: 1,371
• Joined: 16-June 08

## Re: How do you find the average?

Posted 23 March 2010 - 04:58 AM

Hi,

Try this

```Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Subject As Integer
Subject = InputBox("How many products")
Dim i As Integer
Dim num As Integer
Dim total As Integer

For i = 1 To Subject
num = (Val(InputBox("Enter sales amount:")))
total = total + num
Next

TextBox1.Text = total
TextBox2.Text = total / Subject

End Sub

```

### #12 raziel_

• Like a lollipop

Reputation: 458
• Posts: 4,226
• Joined: 25-March 09

## Re: How do you find the average?

Posted 23 March 2010 - 05:23 AM

but is it not better to use "Dim Subject As String" not "Dim Subject As Integer" after all InputBox return result as string. and inputing "aaa" in the inputbox will result to crash the program. simply check if it is number. just a suggestion

### #13 motcom

• D.I.C Lover

Reputation: 288
• Posts: 1,371
• Joined: 16-June 08

## Re: How do you find the average?

Posted 23 March 2010 - 05:31 AM

So true, error checking is yet to be done..

### #14 ZRonZ

Reputation: 27
• Posts: 198
• Joined: 09-January 09

## Re: How do you find the average?

Posted 23 March 2010 - 06:46 AM

ZRonZ, on 22 March 2010 - 06:50 PM, said:

Everything you need to know has been explained in previous post replies. Figure out the real formula for average, capture the one piece of data you need that you are not getting using methods you are already using for other data and you've got it.

Remember this old post? Here's how you get the number of data points. The only difference is that you add 1 everytime and not the inputbox("How Many").

Quote

Nevermind, I got it. I had to add another variable(which I called "total")

Dim Subject As Integer
Subject = InputBox("How many")
Dim i As Integer
Dim num As Integer
Dim total as Integer

For i = 1 To Subject
num + Val(InputBox("This many"))
total = total + num
Next

TextBox1.Text = total

So you take the total from the above, and divide it by the number of data points from your modified code based on the above and you get the average.

The skill you needed had been handed to you. You just need to be a little creative with it. Think through what you want to do. Are there any similarities to previous code you've written? If there are, pull out that code and modify it to suit the new situation.

This post has been edited by ZRonZ: 23 March 2010 - 06:46 AM

### #15 mbrother64

Reputation: -9
• Posts: 78
• Joined: 30-August 09

## Re: How do you find the average?

Posted 23 March 2010 - 01:05 PM

That sucks. I thought I already posted that I figured it out and decided to use total. I was so proud of myself to for figuring it out to.