1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Summing Array elements

Discussion in 'Scripting & Programming' started by philbenson, Nov 27, 2005.

  1. philbenson

    philbenson Byte Poster

    208
    1
    20
    I have a 9 element array which stores integer values. What I would like to be able to do is to add up the individual elements, and then store the value in another integer variable.

    Can this be done without having to explicitly add up each element?

    e.g instead of saying for eg,

    Dim x(8) as Integer
    Dim xTotal as Integer

    xTotal = x(0) + x(1) + x(2)....+ x(8)

    can I have something like

    xTotal = Sum(x(0):x(8))

    which of course is similar to how you would add up the values in a range of cells in a spreadsheet. I know the former would work, but the latter looks tidier!

    Edit (SG) Removed Smilies!
     
    Certifications: MCP, MCP+I, MCSE, MCSA, MCTS
    WIP: CCNA(?)
  2. philbenson

    philbenson Byte Poster

    208
    1
    20
    Oh dear.. Poxy smilies! :x
     
    Certifications: MCP, MCP+I, MCSE, MCSA, MCTS
    WIP: CCNA(?)
  3. simongrahamuk
    Honorary Member

    simongrahamuk Hmmmmmmm?

    6,199
    125
    199
    Fixed the smilies for you Phil! :biggrin
     
  4. hbroomhall

    hbroomhall Petabyte Poster Gold Member

    6,623
    115
    224
    Which language are you intending to use here? It makes a difference!

    Harry.
     
    Certifications: ECDL A+ Network+ i-Net+
    WIP: Server+
  5. zimbo
    Honorary Member

    zimbo Petabyte Poster

    5,215
    98
    181
    hmmm lets see if i understand what you want.. you have an array with 8 elements and you want to add them all up without say:

    x=array[1]+array[2].....

    well if this is it.. why not use a loop? aka conditional statement..
     
    Certifications: B.Sc, MCDST & MCSA
    WIP: M.Sc - Computer Forensics
  6. SimonV

    SimonV Petabyte Poster Administrator

    6,616
    149
    228
    Use the code tags and you wont get the smiles in your text. Hight light your code and hit the code tags buttton [​IMG] . HTH
     
    Certifications: MOS Master 2003, CompTIA A+, MCSA:M, MCSE
    WIP: Keeping CF Alive...

Share This Page

Loading...