If you choose to plug 0 into the variable n, the final result will be 80. The array[0] is equal to the value of 16; hence, in each loop iteration, it adds 16 to the variable result.
For example,
For the first iteration of the loop, the result is 16.
For the second iteration of the loop, the result is 32 (16 + 16)
For the third iteration of the loop, the result is 48 (16 + 32).
For the fourth iteration of the loop, the result is 64 (16 + 48).
For the fifth iteration of the loop, the result is 80 (16 + 64).
Below is a code example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
#include <iostream>
using namespace std;
int array [] = {16, 2, 77, 40, 12071};
int n, result=0;
int main ()
{
for (n=0 ; n<5; n++)
{
result += array[0]; // Result is 16, 32, 48, 64, 80
cout << result << endl; // Added this to visualize the running result.
}
cout << result << endl;
return 0;
}
|
If you choose to plug 1 into the variable n, the final result will be 10. The array[1] is equal to the value of 2; hence, in each loop iteration, it adds 2 to the variable result.
For example,
For the first iteration of the loop, the result is 2.
For the second iteration of the loop, the result is 4 (2 + 2)
For the third iteration of the loop, the result is 6 (2 + 4).
For the fourth iteration of the loop, the result is 8 (16 + 6).
For the fifth iteration of the loop, the result is 10 (2 + 8).
Below is a code example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
#include <iostream>
using namespace std;
int array [] = {16, 2, 77, 40, 12071};
int n, result=0;
int main ()
{
for (n=0 ; n<5; n++)
{
result += array[1]; // Result is 2, 4, 6, 8, 10
cout << result << endl; // Added this to visualize the running result.
}
cout << result << endl;
return 0;
}
|
I hope it helps.