| | |
Getting the sum and average help!
Please support our VB.NET advertiser: $4.95 a Month - ASP.NET Web Hosting – Click Here!
![]() |
•
•
Join Date: Mar 2008
Posts: 345
Reputation:
Solved Threads: 0
Hey,
I am trying to create the sum of my values contained in MyArray variable simultaneously while assigning the values in the same loop. Afterwards, I want to find the average of the numbers after the loop finishes.
Problem: When I compute the numbers, I keep getting the same sum and average for whatever number I put in. How do I fix this problem?
Also, do you see any other errors in my code?
I am trying to create the sum of my values contained in MyArray variable simultaneously while assigning the values in the same loop. Afterwards, I want to find the average of the numbers after the loop finishes.
Problem: When I compute the numbers, I keep getting the same sum and average for whatever number I put in. How do I fix this problem?
Also, do you see any other errors in my code?
VB Syntax (Toggle Plain Text)
Private Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompute.Click Try Dim Num As Integer = Convert.ToInt32(txtNumber.Text) Dim MyArray As Integer() Dim Sum As Integer Dim Average As Double MyArray = New Integer(200) {} For I = 0 To MyArray.GetUpperBound(0) MyArray(I) = I * 2 Next For I = 0 To MyArray.GetUpperBound(0) Sum = Sum + MyArray(I) Next Average = Sum / MyArray.GetUpperBound(0) If Num > 199 Then MessageBox.Show("Please enter a number less than 200") txtNumber.Clear() Else MessageBox.Show("The Sum of Array Numbers is" & Sum & "and the Average is" & Average) End If Catch formatExceptionParameter As FormatException If txtNumber.Text = String.Empty Then MessageBox.Show("Please enter a number less than 200.") Else MessageBox.Show("Invalid character. Please type in a number less than 200.", "Invalid Number Format", _ MessageBoxButtons.OK, MessageBoxIcon.Error) End If Catch OtherEx As Exception MessageBox.Show("There was a technical error, please email customer service for further assistance", "Unknown Error", _ MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End Sub
Last edited by NinjaLink; Nov 22nd, 2008 at 12:32 pm.
You mention you get the same sum and average no matter which number you put in. Which line of code are you referring to when you say "for whatever number I put in".
Secondly, the following line
should be
The upperBound function returns the largest index, which is one less of the number of items in the array.
Secondly, the following line
VB.NET Syntax (Toggle Plain Text)
Average = Sum / MyArray.GetUpperBound(0)
VB.NET Syntax (Toggle Plain Text)
Average = Sum / (MyArray.GetUpperBound(0) + 1)
![]() |
Similar Threads
- need help with getting average using java servlet (Java)
- Determin the average of an array that generates 30 random numbers. (C++)
- Help with counter-controlled loop to find average (C++)
- Finding the average of numbers (C++)
- Errors in counting number of elements of the array greater and lesser than average (C)
- calculating sum, average, highest and lowest grades (Visual Basic 4 / 5 / 6)
- user input random amount display average. little problem i have run into (Java)
Other Threads in the VB.NET Forum
- Previous Thread: auto delete text in textbox
- Next Thread: Using Select Statement to retrieve data from SQL Server 2005 database
| Thread Tools | Search this Thread |
.net .net2005 .net2008 30minutes 2005 2008 access account arithmetic array basic browser button buttons center check code component connectionstring crystalreport cuesent data database databasesearch datagrid datagridview date datetimepicker design dissertation dissertations dissertationthesis dissertationtopic dropdownlist excel fade file-dialog filter folder ftp generatetags hardcopy image images input insert intel monitor navigate net networking opacity output panel passingparameters peertopeervideostreaming picturebox picturebox1 port printing problem problemwithinstallation project reports" savedialog searchvb.net select settings shutdown string survey tcp temperature text textbox timer timespan toolbox transparency trim updown user usercontrol vb vb.net vb.netcode vb.netformclosing()eventpictureboxmessagebox vb.nettoolboxvisualbasic2008sidebar vb2008 vbnet view visual visualbasic visualbasic.net visualstudio visualstudio2008 web winforms wpf year





