Confused about example in Arrays Tutorial

Hi all!

Can someone walk me through the example below and how it comes to 12206? I'm having trouble following.

Thank you,

// arrays example
#include <iostream>
using namespace std;
int billy [] = {16, 2, 77, 40, 12071};
int n, result=0;

int main ()
  for ( n=0 ; n<5 ; n++ )
    result += billy[n];
  cout << result;
  return 0;
Last edited on
Which part don't you understand? Is it the for-loop, or the array itself, or some particular line?
The for-loop... Is n++ just adding 1 over and over until N=5?

And then for the result... I just get all confused. I'm not sure I understand the math going on in this loop.
I may have just figured it out... first time through the loop n=0, so billy =16... result becomes 16...

Second time through n=1, so result becomes 16+2=18... and so on... +77... +40... +12071... Yup, there's the 12206. Haha, thanks for making me look at it closer. :)
Last edited on
That's good. Sorry I didn't help directly, but if you got there in the end, great.
Topic archived. No new replies allowed.