Jump to content
357mag

How do I use a range-based for loop to sum all elements

Recommended Posts

I've never used range-based for loops so I don't know how to write one that is keeping track of the sum of the elements in an array:

 

int main()
{
    int myArray[] = {2, 4, 7, 8, 9};

    cout << "The values in the array are: ";

    for(int n : myArray)
        cout << n << " ";

    int sum = 0;

    for(int x : myArray)
    {
        x += myArray[x]; // don't know what to write here
    }
    cout << "The sum is: " << x << endl; // use of undeclared identifier

    system("pause");
    return 0;
}

 

Share this post


Link to post

It was a couple a decade ago when I wrote my previous C code. At that moment x is out of scope You should use sum to hold the result:

 

sum +=myArray[x]

 

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


 

Share this post


Link to post

Something is still off. My console window is saying that sum is equal to 1985788.

Share this post


Link to post

I find it interesting that you clearly knew how to access the element values in the 1st range-for loop, but didn't know how to do the exact same thing in the 2nd range-for loop. 

  • Confused 2

Share this post


Link to post

I wonder why there is no AV or exception in the case of "sum +=myArray[x]" when the index is out of range? Because it's C++?

Edited by Rolf Fankhauser

Share this post


Link to post
3 hours ago, Rolf Fankhauser said:

I wonder why there is no AV or exception in the case of "sum +=myArray[x]" when the index is out of range? Because it's C++?

Accessing an array out of bounds is Undefined Behavior.  Anything can happen.  An AV/exception is not guaranteed.  C++ does not have bounds checking on vanilla C-style arrays.  If you want that, switch to std::array or std::vector and use their at() method.

 

In this particular example, the first few array values are valid indexes into the array, and the remaining values are small enough that the code would exceed the bounds of the array into surrounding memory but stay within the bounds of the calling thread's stack,  It's valid memory access as far as the OS is concerned, thus no AV is thrown, but it's still Undefined Behavior as far as C++ is concerned.

Edited by Remy Lebeau

Share this post


Link to post

Thanks Remy for the explanation!

I run it several times and got different results for the  invalid indexes. Index 7 always returned 14880996, the other results seem to be random :

7 (because myArray[2] = 7)
9 (because myArray[4] = 9)
14880996
16789504
16448156

 

7
9
14880996
3264512
2095744

 

7
9
14880996
4907008
7338908

Edited by Rolf Fankhauser

Share this post


Link to post
1 hour ago, Rolf Fankhauser said:

I run it several times and got different results for the  invalid indexes. Index 7 always returned 14880996, the other results seem to be random :

That's like the very definition of Undefined Behavior.  Accessing the array out of bounds is reaching into surrounding memory, and the content of that memory is random/indeterminate on each new run.  It could be random bytes left over from a previous run/process.  It could be bytes belonging to other local variables on the stack.  You just don't know.

 

myArray[2] = 7
myArray[4] = 9
myArray[7] = out of bounds, UB!

myArray[8] = out of bounds, UB!
myArray[9] = out of bounds, UB!

Edited by Remy Lebeau

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×