Thread: Adding Values Of An Array

  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 02: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, 06:24 AM
  2. putting values into an array
    By zdream8 in forum C Programming
    Replies: 15
    Last Post: 05-21-2008, 11:18 PM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. eliminating dup values in array
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 04-19-2002, 06:05 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM