adding values of an array

using a For structure

Page 1 of 1

10 Replies - 39103 Views - Last Post: 03 April 2006 - 03:07 PM Rate Topic: -----

#1 C0deGirl19  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-March 06

adding values of an array

Posted 01 April 2006 - 02:30 PM

I want to add all the values of this array and display the sum in a label using the "For version meant to process each element in an array." I dont really understand how thats done. :blink: Its the only part of this project left that i need.

Here is the array:

Dim inventory(,) As Integer = {{34, 56}, _
{75, 67}, _
{5, 6}}

I just really have no idea how to do this and i have a feeling it should be easy...uggh!

This post has been edited by C0deGirl19: 01 April 2006 - 08:33 PM


Is This A Good Question/Topic? 0
  • +

Replies To: adding values of an array

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: adding values of an array

Posted 01 April 2006 - 10:42 PM

Your talking about a For Each...Next Loop. Looks like this.

Dim months() As String = {"Jan","Feb","Mar","Apr","May",...etc}
Dim element As String

For Each element In months
     'Your code here
Next element


Was This Post Helpful? 0
  • +
  • -

#3 C0deGirl19  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-March 06

Re: adding values of an array

Posted 02 April 2006 - 08:54 AM

Yeah i guess that makes sense but I need to add the values to get the sum...?
Was This Post Helpful? 0
  • +
  • -

#4 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: adding values of an array

Posted 02 April 2006 - 08:59 AM

Do you need to add the two values of each element together for a sum, add all values of all elements for a sum, or add corresponding values (like the 1st value from each one) from each element for a sum? If you could provide an example using the initial numbers you specified, we could better provide some guidance.
Was This Post Helpful? 0
  • +
  • -

#5 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: adding values of an array

Posted 02 April 2006 - 11:35 AM

Ok, well to add them up. The variable element becomes temporary storage for the current item in the array while used in this type of For...Loop, it will contain the value of each item in the array as the loop is processing.
So all you need to do is create a third variable to hold the running total. Then add each new element to the running total. Then after the loop has completed you could display the sum of all the numbers or whatever other code you need to implement.

Like this:


Dim numbers() As Integer = {1,2,3,4,5}
Dim element As Integer
Dim total As Integer

For Each element In numbers
     total = total + element
Next element


Was This Post Helpful? 0
  • +
  • -

#6 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: adding values of an array

Posted 02 April 2006 - 12:45 PM

I'm afraid the questions I posed will still have to be answered...the user appears to have a series of two dimensional arrays, and no indications as to what is being added to what.
Was This Post Helpful? 0
  • +
  • -

#7 sontek  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 283
  • Joined: 13-September 01

Re: adding values of an array

Posted 02 April 2006 - 02:48 PM

I also responded/gave a solution to this problem and asked her the same question about the multi-dimensional arrays, dunno what happened to my post :P
Was This Post Helpful? 0
  • +
  • -

#8 C0deGirl19  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-March 06

Re: adding values of an array

Posted 02 April 2006 - 07:48 PM

I needed to get the sum of all the numbers in the array to display in a label. I think i should have it figured out with jayman's post. Thanks everyone! ;)
Was This Post Helpful? 0
  • +
  • -

#9 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: adding values of an array

Posted 02 April 2006 - 08:50 PM

Quote

  I want to add all the values of this array and display the sum in a label...

Actually she did say how she wanted to add them up.

And using a For...Each loop will work for single or multidimensional arrays. Although as you noted, the process would be different if you needed to add only certain numbers and not all of them.
Was This Post Helpful? 0
  • +
  • -

#10 sontek  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 283
  • Joined: 13-September 01

Re: adding values of an array

Posted 03 April 2006 - 02:46 AM

She said she wanted to add them all up but if thats all she wanted she had no reason for using a multi-dimensional array which is what lead amadeus and I to believe that there was more to the problem than she was leading to
Was This Post Helpful? 0
  • +
  • -

#11 C0deGirl19  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-March 06

Re: adding values of an array

Posted 03 April 2006 - 03:07 PM

I got it to work the way I wanted it to. The For..Next Loop worked just fine B)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1