14 Replies - 1563 Views - Last Post: 23 March 2010 - 01:05 PM Rate Topic: -----

#1 mbrother64  Icon User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • 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

                ListBox1.Items.Add(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  Icon User is offline

  • New D.I.C Head

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

Re: How do you find the average?

Posted 22 March 2010 - 07:01 PM

View Postmbrother64, 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

                ListBox1.Items.Add(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))

                ListBox1.Items.Add(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

Was This Post Helpful? 0
  • +
  • -

#3 mbrother64  Icon User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#4 ZRonZ  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • 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.
Was This Post Helpful? 1
  • +
  • -

#5 iHack  Icon User is offline

  • New D.I.C Head

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

Re: How do you find the average?

Posted 22 March 2010 - 08:30 PM

View PostZRonZ, 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...

Read this post mbrother64!
Was This Post Helpful? 0
  • +
  • -

#6 mbrother64  Icon User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#7 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1619
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#8 mbrother64  Icon User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#9 iHack  Icon User is offline

  • New D.I.C Head

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

Re: How do you find the average?

Posted 22 March 2010 - 10:50 PM

View Postmbrother64, 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...
Was This Post Helpful? -1
  • +
  • -

#10 mbrother64  Icon User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#11 motcom  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 288
  • View blog
  • 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
                ListBox1.Items.Add(num)
            Next

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

    End Sub


Was This Post Helpful? 0
  • +
  • -

#12 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 458
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#13 motcom  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 288
  • View blog
  • 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..
Was This Post Helpful? 0
  • +
  • -

#14 ZRonZ  Icon User is offline

  • D.I.C Head

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

Re: How do you find the average?

Posted 23 March 2010 - 06:46 AM

View PostZRonZ, 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

Was This Post Helpful? 0
  • +
  • -

#15 mbrother64  Icon User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1