Getting the sum and average help!

Please support our VB.NET advertiser: $4.95 a Month - ASP.NET Web Hosting – Click Here!
Reply

Join Date: Mar 2008
Posts: 345
Reputation: NinjaLink is an unknown quantity at this point 
Solved Threads: 0
NinjaLink NinjaLink is offline Offline
Posting Whiz

Getting the sum and average help!

 
0
  #1
Nov 22nd, 2008
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?


  1.  
  2. Private Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompute.Click
  3.  
  4.  
  5.  
  6. Try
  7.  
  8. Dim Num As Integer = Convert.ToInt32(txtNumber.Text)
  9. Dim MyArray As Integer()
  10. Dim Sum As Integer
  11. Dim Average As Double
  12.  
  13.  
  14. MyArray = New Integer(200) {}
  15.  
  16. For I = 0 To MyArray.GetUpperBound(0)
  17.  
  18. MyArray(I) = I * 2
  19.  
  20. Next
  21.  
  22.  
  23.  
  24. For I = 0 To MyArray.GetUpperBound(0)
  25.  
  26. Sum = Sum + MyArray(I)
  27.  
  28. Next
  29.  
  30.  
  31. Average = Sum / MyArray.GetUpperBound(0)
  32.  
  33.  
  34. If Num > 199 Then
  35. MessageBox.Show("Please enter a number less than 200")
  36. txtNumber.Clear()
  37. Else
  38.  
  39. MessageBox.Show("The Sum of Array Numbers is" & Sum & "and the Average is" & Average)
  40. End If
  41.  
  42.  
  43.  
  44. Catch formatExceptionParameter As FormatException
  45.  
  46. If txtNumber.Text = String.Empty Then
  47.  
  48. MessageBox.Show("Please enter a number less than 200.")
  49.  
  50. Else
  51.  
  52. MessageBox.Show("Invalid character. Please type in a number less than 200.", "Invalid Number Format", _
  53. MessageBoxButtons.OK, MessageBoxIcon.Error)
  54. End If
  55.  
  56. Catch OtherEx As Exception
  57.  
  58. MessageBox.Show("There was a technical error, please email customer service for further assistance", "Unknown Error", _
  59. MessageBoxButtons.OK, MessageBoxIcon.Error)
  60.  
  61. End Try
  62.  
  63.  
  64. End Sub
Last edited by NinjaLink; Nov 22nd, 2008 at 12:32 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 345
Reputation: NinjaLink is an unknown quantity at this point 
Solved Threads: 0
NinjaLink NinjaLink is offline Offline
Posting Whiz

Re: Getting the sum and average help!

 
0
  #2
Nov 22nd, 2008
I think it is suppose to be

MyArray(I) = I + 10

because I need it to be 10+ more than the integer.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 304
Reputation: timothybard is an unknown quantity at this point 
Solved Threads: 25
timothybard's Avatar
timothybard timothybard is offline Offline
Posting Whiz

Re: Getting the sum and average help!

 
0
  #3
Nov 22nd, 2008
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
  1. Average = Sum / MyArray.GetUpperBound(0)
should be
  1. Average = Sum / (MyArray.GetUpperBound(0) + 1)
The upperBound function returns the largest index, which is one less of the number of items in the array.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC