4 Replies - 614 Views - Last Post: 09 November 2008 - 07:59 PM Rate Topic: -----

#1 <jabakaba>  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 07-October 08

Why wont this average

Posted 09 November 2008 - 04:37 PM

its suppose keep track of values entered arrays and keep averaging the new numbers entered and put that to another textbox

Public Class Form1
	'class constants
	Const intNumberOfEmployees As Integer = 10
	Const intMaxNumberofEmployees As Integer = intNumberOfEmployees - 1

	' class level variables
	Private Names(intMaxNumberofEmployees) As String
	Private quarter1(10) As Integer
	Private quarter2(10) As Integer
	Private quarter3(10) As Integer
	Private quarter4(10) As Integer

	Private Sub txtQuarter1_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtQuarter1.Validating, txtQuarter4.Validating, txtQuarter3.Validating, txtQuarter2.Validating
		Dim input As String = CType(sender, TextBox).Text
		If IsNumeric(input) Then
			Dim number As Integer = CInt(input)
		Else
			MessageBox.Show("Please enter an integer.")
			e.Cancel = True
		End If

	End Sub

	

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

		quarter1(0) = CInt(txtQuarter1.Text)
		quarter2(0) = CInt(txtQuarter2.Text)
		quarter3(0) = CInt(txtQuarter3.Text)
		quarter4(0) = CInt(txtQuarter4.Text)
		'  
		quarter1(1) = CInt(txtQuarter1.Text)
		quarter2(1) = CInt(txtQuarter2.Text)
		quarter3(1) = CInt(txtQuarter3.Text)
		quarter4(1) = CInt(txtQuarter4.Text)
		'
		quarter1(2) = CInt(txtQuarter1.Text)
		quarter2(2) = CInt(txtQuarter2.Text)
		quarter3(2) = CInt(txtQuarter3.Text)
		quarter4(2) = CInt(txtQuarter4.Text)
		'
		quarter1(3) = CInt(txtQuarter1.Text)
		quarter2(3) = CInt(txtQuarter2.Text)
		quarter3(3) = CInt(txtQuarter3.Text)
		quarter4(3) = CInt(txtQuarter4.Text)
		'
		quarter1(4) = CInt(txtQuarter1.Text)
		quarter2(4) = CInt(txtQuarter2.Text)
		quarter3(4) = CInt(txtQuarter3.Text)
		quarter4(4) = CInt(txtQuarter4.Text)
		'
		quarter1(5) = CInt(txtQuarter1.Text)
		quarter2(5) = CInt(txtQuarter2.Text)
		quarter3(5) = CInt(txtQuarter3.Text)
		quarter4(5) = CInt(txtQuarter4.Text)
		'
		quarter1(6) = CInt(txtQuarter1.Text)
		quarter2(6) = CInt(txtQuarter2.Text)
		quarter3(6) = CInt(txtQuarter3.Text)
		quarter4(6) = CInt(txtQuarter4.Text)
		'
		quarter1(7) = CInt(txtQuarter1.Text)
		quarter2(7) = CInt(txtQuarter2.Text)
		quarter3(7) = CInt(txtQuarter3.Text)
		quarter4(7) = CInt(txtQuarter4.Text)
		'
		quarter1(8) = CInt(txtQuarter1.Text)
		quarter2(8) = CInt(txtQuarter2.Text)
		quarter3(8) = CInt(txtQuarter3.Text)
		quarter4(8) = CInt(txtQuarter4.Text)
		'
		quarter1(9) = CInt(txtQuarter1.Text)
		quarter2(9) = CInt(txtQuarter2.Text)
		quarter3(9) = CInt(txtQuarter3.Text)
		quarter4(9) = CInt(txtQuarter4.Text)

		Dim intTotal As Integer = 0
		Dim dblAverage As Double
		Dim intCount As Integer
		For intCount = 0 To (quarter1.Length - 1)
			intTotal += quarter1(intCount)

		Next intCount
		dblAverage = intTotal / quarter1.Length
		txtResult1.Text = dblAverage.ToString





Is This A Good Question/Topic? 0
  • +

Replies To: Why wont this average

#2 dbasnett  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 109
  • View blog
  • Posts: 603
  • Joined: 01-October 08

Re: Why wont this average

Posted 09 November 2008 - 04:42 PM

what happens here?

Quote

For intCount = 0 To (quarter1.Length - 1)
intTotal += quarter1(intCount)

Next intCount
dblAverage = intTotal / quarter1.Length
txtResult1.Text = dblAverage.ToString

Was This Post Helpful? 0
  • +
  • -

#3 <jabakaba>  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 07-October 08

Re: Why wont this average

Posted 09 November 2008 - 04:49 PM

skip

This post has been edited by <jabakaba>: 09 November 2008 - 05:01 PM

Was This Post Helpful? 0
  • +
  • -

#4 <jabakaba>  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 07-October 08

Re: Why wont this average

Posted 09 November 2008 - 04:58 PM

View Post<jabakaba>, on 9 Nov, 2008 - 03:49 PM, said:

View Postdbasnett, on 9 Nov, 2008 - 03:42 PM, said:

what happens here?

Quote

For intCount = 0 To (quarter1.Length - 1)
intTotal += quarter1(intCount)

Next intCount
dblAverage = intTotal / quarter1.Length
txtResult1.Text = dblAverage.ToString


umm Im not positve.... I want to t average out my arrays of ten employees and output that statment to a related textbox , now Im getting confused looking at it again


I figure this is how you would add a numeric array

dim inttotal as integer = 0 '  initialize it at zero
dim intcount as integer ' ' used to loop
for intcount = 0 To (quarter1.length -1 ) because arrays start storing at 0 

inttotal +=intUnits(incount) ' this will add as many numbers as are entered

next intcount





then i would just have to divide and show output somehow
Was This Post Helpful? 0
  • +
  • -

#5 magicmonkey  Icon User is offline

  • D.I.C Regular

Reputation: 106
  • View blog
  • Posts: 484
  • Joined: 12-September 08

Re: Why wont this average

Posted 09 November 2008 - 07:59 PM

Well your loop logic seems right for an average, however I think what is wrong is how you are populating your arrays with the data. When you click button1 you are placing the same value, what ever is currently in each text box, into all the positions in the array, so the average is going to be what ever the value of each textbox is for each quarter.

I think what you want to do is place the values of each text box into the array each time the user press the add button, then average them. Instead of using an array use a generic collection List(Of Interger). Then you do not have to worry about size and can just keep adding data to the arrays. Another advantage in .NET 3.5 is LINQ and the fact that it can average the values of the collection for you as shown below.

Public Class Form1
    Private quarter1 As New List(Of Integer)
    Private quarter2 As New List(Of Integer)
    Private quarter3 As New List(Of Integer)
    Private quarter4 As New List(Of Integer)
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        quarter1.Add(Convert.ToInt32(txtQuarter1.Text))
        quarter2.Add(Convert.ToInt32(txtQuarter2.Text))
        quarter3.Add(Convert.ToInt32(txtQuarter3.Text))
        quarter4.Add(Convert.ToInt32(txtQuarter4.Text))

        txtResult1.Text = quarter1.Average.ToString
        txtResult2.Text = quarter2.Average.ToString
        txtResult3.Text = quarter3.Average.ToString
        txtResult4.Text = quarter4.Average.ToString
    End Sub

    Private Sub Clear() 'Put this code in a clear button to reset everything 
        quarter1.Clear()
        quarter2.Clear()
        quarter3.Clear()
        quarter4.Clear()
        txtResult1.Text = String.Empty
        txtResult2.Text = String.Empty
        txtResult3.Text = String.Empty
        txtResult4.Text = String.Empty
    End Sub
End Class


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1