Array example

Hi, I have written some comments in this source code which I dont understand much. Please correct me.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// 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++ ) // the initial value of n is 0 and its function is to add up to 4.(that is less than 5)
  {
    result += billy[n]; //I cant understand this line.
  }
  cout << result;
  return 0;
}


I cant understand how these lines work:
1
2
3
  for ( n=0 ; n<5 ; n++ )
  {
    result += billy[n];


The process seems just to add 16, 2, 77, 40, 12071. What is the purpose of those lines of codes?
I just experimented the code and fully understand:
for ( n=0 ; n<5 ; n++ )
My only problem remaining is
result += billy[n]
This: result += billy[n]; is a shorthand way of writing this result = result + billy[n];

Also billy[n] is simply saying to take the nth value of the array called billy. That is the nth number in the input sequence on line 5 of your original example.
Topic archived. No new replies allowed.