Question Adding a range of numbers efficiently

Old Marcus

Member
Joined
Sep 22, 2009
Messages
6
Programming Experience
Beginner
Hi, I'm making a program as part of a college assignment that requires my to add up the contents of an array. Now the array has 20 places, and the only method I know of atm is:

a + b + c + d + e etc...

They are all different numbers so I can't just do a * 20, I need a more code efficient way of adding all these numbers up, is this possible?

I apologise if this has been asked before, I had a quick search but couldn't find much. If the thread already exists, I would be happy to read that without having to get you long suffering people to tell someone yet again how to do it.

And google didn't turn up anything useful either, unless I was using the wrong search terms... :(
 
Are you talking Integers?
VB.NET:
Dim int As Integer = 0
Dim myIntegerArray() As Integer = New Integer() {1, 2, 3, 4, 5, 6}
For Each i As Integer In myIntegerArray
   int += i
Next
MessageBox.Show(int.ToString)
 
Programming doesn't exist in a vacuum. Look at what newguy posted and then think about how you would add up a list of 20 numbers manually. You would add up the first 2 and get a result, right? Then you would add the third number to that result to get a new result, right? You keep going like that until you got to the end of the list, right? Isn't that exactly what newguy's code does?

This is probably too advanced for your class but, just so that you're aware, the easiest way to do this is with LINQ, e.g.
VB.NET:
Dim myIntegerArray() As Integer = New Integer() {1, 2, 3, 4, 5, 6}
Dim sum As Integer = myIntegerArray.Sum()

MessageBox.Show(sum)
:D "Dim sum" :D
 
You need to use an accumulator inside a loop. Example in pseudocode:

total = 0
For x = 0 to UpperboundValue
'Get the new value from the x element of the array
'for example: newvalue = myarray(x)
total = total + new value 'this is the accumulator
Next x
 
@Solitaire: wasn't that what newguy posted? Or are you not familiar with the += operator?
 
Yes, I am familiar with the += operator.
And no, it's not what newguy posted.

This is for a beginning programming student who may not have learned more than the basics in his class. I used the word, "accumulator" to describe the special kind of variable. I also simplified the code to use the more basic For loop rather than the For Each loop that newguy used, and that the OP may or may not be familiar with.
 
{Be hAppy}

'Say your array name is A or any type

Dim Sum As Object = 0
For Each c As Object In A
if not c is nothing then
Sum += Val(c.ToString)
end if
Next
MsgBox(Sum)
 
@Solitaire: Sorry, but I don't see the difference.
@send2te: Now that is surely the same as what newguy posted. Or do you claim to use different variable names? Perhaps you can post Solitaires code with different variable names also?
 
Dear JohnH

Yes my new code is approximately same but it is not limited the variable types - I mean it can be Integer, Long, Single, Double etc.

If you are interested for some other sort of coding then please see this one:

Private Sub GetSum()
Dim A(5)
A(0) = 5
A(1) = 5
A(2) = 10
A(3) = 10
A(4) = 30
A(5) = 40
MsgBox(GetSum(A, 0))
End Sub


Private Function GetSum(ByRef A As Object, ByVal ForIndex As Integer) As Single
If ForIndex > UBound(A) Then
Return 0
Else
Return A(ForIndex) + GetSum(A, ForIndex + 1)
End If
End Function
 
Back
Top