Adding Values Of An Array

This is a discussion on Adding Values Of An Array within the C++ Programming forums, part of the General Programming Boards category; how can I add the values of an array using a for loop? this is my code --------------------------------------------------------------------------- #include <iostream> ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    9

    Question Adding Values Of An Array

    how can I add the values of an array using a for loop?
    this is my code

    ---------------------------------------------------------------------------
    #include <iostream>



    int main()
    {
    int sum=0;
    int a[100];

    for (int count=1; count<100; count++)
    cout<<"element #: "<<count<<endl;



    sum += a[count++];

    cout<<"sum is: "<<sum<<endl;



    return 0;
    }
    --------------------------------------------------------------------------
    Thank you all for your help
    Last edited by moenia; 05-12-2003 at 03:49 PM.

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    ... using a for loop ...
    Code:
    for (int i = 0; i < 5; i ++)
       sum += arr[i];

  3. #3
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    Code:
    for (i = 0; i < len(a); ++i)
        sum += a[i];
    Or you could consult any book on either C or C++, they all have something similar to the above.
    p.s. What the alphabet would look like without q and r.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Storing values from Edit Box into an array
    By E_I_S in forum C++ Programming
    Replies: 10
    Last Post: 06-05-2008, 07:24 AM
  2. putting values into an array
    By zdream8 in forum C Programming
    Replies: 15
    Last Post: 05-22-2008, 12:18 AM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM
  4. eliminating dup values in array
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 04-19-2002, 07:05 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 10:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21