Thread: Adding an array using a For loop

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    8

    Adding an array using a For loop

    I need to add the elements of an array together using a for loop and then print the result. I know it is a simple code but I am struggling with it. Can anyone offer any advice on this?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What have you tried?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    For Loop Example:
    Code:
    for(int i = 0; i < ARRAY_SIZE; i++){
    }
    Woop?

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    8
    I'm kinda stuck at the starting point (I'm just learning this stuff). I know the basic code structure but I am struggling with the body of it. The basic structure of the for loop does help. For instance, say you had to add together 1,2,3 and 4 using a for loop. What would that look like?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, suppose you have two variables, sum and x. To add x to sum, you would write:
    Code:
    sum += x;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    8
    So for multiple variables, say x, y, z, it would be like this?: x+=y+=z;

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Nope, unless you intend to add z to y as well. In this case you are summing the elements of an array, one at a time.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    8
    Oh ok. So it would be: sum += x ; Where x is just the name of the array?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No, where x is the current element of the array, i.e., you would replace x with say, y[i], where y is the array name and i is the array index.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10

  11. #11
    Registered User
    Join Date
    Mar 2009
    Posts
    8
    Elkvis- I appreciate it but Googling it is what led me this forum in the first place

  12. #12
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    You could need to declare a variable to hold the sum , ie. int sum=0;

    Then use a for loop to go through the elements of the array, and add it to sum.

    As such,

    Code:
    for(int i=0; i < array_size; i++)
         sum=sum + myarray[i]; //or sum += myarray[i]
    
    cout << sum;
    All this is doing is accessing the element of the array, and adding it to the value in sum, so you get the total of the array.
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Modify an single passed array element
    By swgh in forum C Programming
    Replies: 3
    Last Post: 08-04-2007, 08:58 AM
  3. Adding elements to array?
    By INFERNO2K in forum C++ Programming
    Replies: 4
    Last Post: 12-15-2005, 01:56 PM
  4. Adding Scores from a string array
    By MB1 in forum C++ Programming
    Replies: 6
    Last Post: 11-11-2005, 07:27 AM
  5. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 11:44 AM

Tags for this Thread