# Getting the average of an array using FOR NEXT

Page 1 of 1

## 7 Replies - 5806 Views - Last Post: 26 June 2012 - 08:57 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=283445&amp;s=b03e7cba4af04331e9b8deaf3f6ff26c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Quiza12

Reputation: 0
• Posts: 9
• Joined: 21-June 12

# Getting the average of an array using FOR NEXT

Posted 21 June 2012 - 12:17 AM

Hi everyone, I'm quite new to VB, and I am completely stuck on a problem of trying to get the average of an array of numbers. There could be up to 999 indexes (I picked a large array because of the the numbers of entries the array could have). As you can see I have managed to get the program to calculate the Sum of the in the array, but I am struggling with getting the average.

Say, for example, I enter three sets of 90's, I get 3.

How do I get an average?

(BTW I have programmed it so the numbers go into an List Box, so this is why you see no code to make up the array or to where the array is printing the values).

Thanks

```

Dim Sum As Integer
Dim SumCount As Integer
Dim AverageCount As Integer
Dim StudentMarks(999) As Integer

For Me.SumCount = 1 To StudentMarks(StudentMark)

Sum = StudentMark + Sum

AverageCount = AverageCount + 1

Next

lblSum.Text = Sum

lblAverage.Text = Sum / AverageCount

```

Is This A Good Question/Topic? 0

## Replies To: Getting the average of an array using FOR NEXT

### #2 GodOfCode

Reputation: 0
• Posts: 4
• Joined: 15-June 12

## Re: Getting the average of an array using FOR NEXT

Posted 21 June 2012 - 01:34 AM

Dim lCounter As Long
Dim dTotal As Double
Dim dAverage As Double

dTotal = 0
For lCounter = 0 To UBound(yourArray)
dTotal = dTotal + yourArray(lCounter)
Next
dAverage = dTotal / (UBound(yourArray) + 1)

### #3 Quiza12

Reputation: 0
• Posts: 9
• Joined: 21-June 12

## Re: Getting the average of an array using FOR NEXT

Posted 21 June 2012 - 04:30 AM

GodOfCode, on 21 June 2012 - 01:34 AM, said:

Dim lCounter As Long
Dim dTotal As Double
Dim dAverage As Double

dTotal = 0
For lCounter = 0 To UBound(yourArray)
dTotal = dTotal + yourArray(lCounter)
Next
dAverage = dTotal / (UBound(yourArray) + 1)

### #4 Quiza12

Reputation: 0
• Posts: 9
• Joined: 21-June 12

## Re: Getting the average of an array using FOR NEXT

Posted 21 June 2012 - 04:57 AM

Quiza12, on 21 June 2012 - 04:30 AM, said:

GodOfCode, on 21 June 2012 - 01:34 AM, said:

Dim lCounter As Long
Dim dTotal As Double
Dim dAverage As Double

dTotal = 0
For lCounter = 0 To UBound(yourArray)
dTotal = dTotal + yourArray(lCounter)
Next
dAverage = dTotal / (UBound(yourArray) + 1)

Ok so I've inserted this code,

```Total = 0
For Counter = 0 To UBound(StudentMarks)
Total = Total + StudentMarks(Counter)
Next

Average = Total / (UBound(StudentMarks) + 1)

lblAverage.Text = Average

```

but if for example, I enter four 60's, the answer comes up as 0.24.

Sorry to be such a pain but if there are any other hints you can give me? I have scoured the Internet for so long trying to find a solution.

Thanks.

### #5 BobRodes

• Lovable Curmudgeon

Reputation: 600
• Posts: 3,071
• Joined: 19-May 09

## Re: Getting the average of an array using FOR NEXT

Posted 21 June 2012 - 06:46 PM

The problem is that you've "hard-wired" your array to have more values than it needs. Most people use dynamic arrays, and the solution that you've been given works well for those.

As I think you know, to get an average of the numbers in a group, sum the numbers and divide the result by the number of numbers. Ubound returns the number of elements in the array, so the solution you've been given assumes that the array is full. You're entering four 60's, coming up with 240, and then dividing the result by 999.
The computer happily gives you the result: .24.

Lesson 1. If someone writes code for you instead of showing you how to do it yourself, you're stuck with his mistakes instead of your own. Others' mistakes are not as solvable as your own.

Lesson 2. Dynamic arrays. Learn to use them when the size of your array isn't static. Here's an example:
```'Declare the array with no number
Dim StudentMarks() as Integer
'Now, to change the size of the array:
ReDim Preserve StudentMarks(1)

```
You use the Preserve keyword to keep the values that you already have in the array. If you don't use it, the array will be emptied each time you re-dimension it.

So, if you use a dynamic array, it will always be full and unbound will be a number you can use.

### #6 Quiza12

Reputation: 0
• Posts: 9
• Joined: 21-June 12

## Re: Getting the average of an array using FOR NEXT

Posted 21 June 2012 - 07:24 PM

BobRodes, on 21 June 2012 - 06:46 PM, said:

The problem is that you've "hard-wired" your array to have more values than it needs. Most people use dynamic arrays, and the solution that you've been given works well for those.

As I think you know, to get an average of the numbers in a group, sum the numbers and divide the result by the number of numbers. Ubound returns the number of elements in the array, so the solution you've been given assumes that the array is full. You're entering four 60's, coming up with 240, and then dividing the result by 999.
The computer happily gives you the result: .24.

Lesson 1. If someone writes code for you instead of showing you how to do it yourself, you're stuck with his mistakes instead of your own. Others' mistakes are not as solvable as your own.

Lesson 2. Dynamic arrays. Learn to use them when the size of your array isn't static. Here's an example:
```'Declare the array with no number
Dim StudentMarks() as Integer
'Now, to change the size of the array:
ReDim Preserve StudentMarks(1)

```
You use the Preserve keyword to keep the values that you already have in the array. If you don't use it, the array will be emptied each time you re-dimension it.

So, if you use a dynamic array, it will always be full and unbound will be a number you can use.

Thank you very much! I get it now absolute legends

### #7 _HAWK_

• Master(Of Foo)

Reputation: 1162
• Posts: 4,444
• Joined: 02-July 08

## Re: Getting the average of an array using FOR NEXT

Posted 21 June 2012 - 09:04 PM

Or you can use a List(Of T) and use it's extension method .Ave .

```Dim myNumbers As New List(Of Integer)
myNumbers.AddRange(New Integer() {2, 4, 6, 8})
MessageBox.Show(myNumbers.Ave.ToString)
```

### #8 BobRodes

• Lovable Curmudgeon

Reputation: 600
• Posts: 3,071
• Joined: 19-May 09

## Re: Getting the average of an array using FOR NEXT

Posted 26 June 2012 - 08:57 AM

Also a good idea, maybe a better one.