Thread: Unexpected output in an example function

  1. #1
    Registered User
    Join Date
    Jan 2015

    Question Unexpected output in an example function

    I am currently trying to understand why this example for using an array as an argument in a function has a different result than what the lecture notes say it should be.

    So supposedly sum should return with the value 28, but I get 27 no matter what. I also am not very good at reading and understanding what exactly the order of operations for this function are, if someone would be willing to help me I would greatly appreciate it

    #include <iostream>
    using namespace std;
    int sum(const int array[], const int length) {
        long sum = 0;
        for (int i = 0; i < length; sum += array[++i]);
            return sum;
    int main() {
        int arr[] = {1, 2, 3, 4, 5, 6, 7};
        cout << "Sum: " << sum(arr, 7) << endl;
        return 0;

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    You may have made a typo error, i.e., array[++i] probably should have been array[i++]. The reason is that with ++i, you never add array[0] to the sum, hence the final result excludes the 1. Furthermore, although it does not show in your tests, you are actually accessing the array out of bounds, i.e., you access array[7], which does not exist. It is probably the case that a zero happens to be in memory at that location, hence it looks like your code "almost works".
    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
    Registered User
    Join Date
    Jan 2015
    Thank you very much it was indeed the array[++i] that was giving me trouble, however passing 7 to length causes the for loop to stop at array[6] not 7 due to the
    i < length. Thank you very much for your help. Much appreciated

  4. #4
    Registered User
    Join Date
    Jun 2005
    "sum += array[i++]" is an example of an expression with multiple side effects (i.e. changing more than one value): it evaluates i, accesses array[i], it changes i, it changes sum.

    As a general rule - as you have demonstrated - code which relies on expressions with multiple side effects is easy to misunderstand and therefore error prone (e.g. a small change can produce the wrong result).

    You would be better off rewriting the code to eliminate reliance on multiple side effects.
        for (int i = 0; i < length; ++i)
            sum += array[i];
    While it is obviously possible to change that code to do something you don't intend, it is also much clearer that it actually does what you intend, because each expression changes - at most - one thing.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unexpected output
    By CrimsonMagi in forum C++ Programming
    Replies: 12
    Last Post: 11-09-2013, 09:26 PM
  2. Unexpected output
    By juice in forum Windows Programming
    Replies: 6
    Last Post: 03-10-2012, 11:13 AM
  3. Unexpected output
    By juice in forum C Programming
    Replies: 24
    Last Post: 11-18-2011, 11:18 PM
  4. Unexpected output
    By GolDRoger in forum C Programming
    Replies: 9
    Last Post: 11-18-2011, 02:49 PM
  5. unexpected output
    By crash88 in forum C Programming
    Replies: 2
    Last Post: 05-16-2006, 09:03 PM