| | |
Getting the sum and average help!
Please support our VB.NET advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2008
Posts: 365
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 .net2008 2005 2008 access account arithmetic array basic beginner bing browser button buttons center check code crystalreport cuesent data database datagrid datagridview date datetimepicker designer dissertation dissertations dissertationtopic dropdownlist excel fade file-dialog filter forms ftp generatetags google hardcopy html images input insert intel internet mobile monitor net networking objects open output panel passingparameters pdf picturebox picturebox1 port position print printing problem save searchbox searchvb.net select serial settings shutdown soap sqlserver survey table tcp temperature text textbox timer timespan toolbox transparency trim update user vb vb.net vb.netformclosing()eventpictureboxmessagebox vb2008 vba vbnet visual visualbasic visualbasic.net visualstudio.net visualstudio2008 web winforms wpf wrapingcode year





