Problem

7/12

Iterating over the elements of an array

Theory Click to read/hide

Iterating over array elements
When working with arrays, you usually have to work with all the elements of the array at once.
 
Iterating over elements consists of going through all elements of an array and performing the same operation on each of them.

To do this, most often a loop with a variable is used, which changes from 0 to N-1 (N the number of array elements). ... const int N = 10; int A[N]; for (i = 0; i < N; i++) { // action on element A[i] } ... In the specified loop, the variable i will take on the values ​​0, 1, 2, ..., N-1.  That is, at each step of the loop, we access a specific array element with index i.
Thus, it is enough to write down what needs to be done with one element of the A[i] array and place these actions inside such a loop.
 
Task
Fill the array with the first N natural numbers. Those. at the end of the program, the elements of the array should become equal: A[0] = 1;  A[1] = 2; A[2] = 3; ... A[N-1] = N;
It is easy to see the pattern: the value of an array element must be greater by 1 than the index of the element.
The cycle will look like this: for (i=0; i<N; i++) { A[i] = i+1; }

Problem

1) Study the comments to the program.
2) In block 1, arrange a loop that fills all the elements of the array with the values ​​of natural numbers from 1 to N.