clarification on this tutorial

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

int answer(int asd[], int num);

int main (){
    int numbers[5]={1,5,42,68,85};
    int total=answer(numbers,5);
    cout << "sum of all elements in the array " << total << endl;
    system("pause");
    return 0;
}

int answer(int asd[], int num){
    int sum=0;
    for(int x=0;x<num;x++){
        sum+=asd[x];
        }
    return sum;
}


If the x value is 5 how can asd[x] at line17 be the result of the sum of all the numbers?.
The asd array has 5 elements. They're indexed from 0 to 4.

The for loop at line 16 execute 5 times with x varying from 0 to 4 adding each of the 5 elements of the array to sum.


Thanks, that helped. Just to be sure, the value asd is basically (1,5,42,68,85), the value num is 5, when i for loop i execute 5 times x (0,1,2,3,4) and then by putting x to asd i make 1,5,42,68,85 (5 numbers) executing, having the result.
Last edited on
Topic archived. No new replies allowed.