Array inside a loop. Easy exercise.
- a01337106
- 10 feb 2015
- 1 Min. de lectura
We now know how to store different data inside one single variable (an array).
First, remember the parts of the array. The size, index, indeces and the values inside an index. All of the indeces is the total length of an array. Each of those indeces is identified by one number from 0 to the length of the array minus une. Inside of each index a value is stored, this ones are elements inside the array.
Now we have to know how to apply them on exercises.
First let’s see an easy example:
This program will sum all of the values inside a established array A[] of size N
This are the values inside A[]:
[0] [1] [2] [3] [4] [5] [6]
2 6 9 12 6 4 8
with this, we already now that the size of the array is N=7
//The program
loop I from 0 to N-1
int sum=sum+A[I];
end loop
Each time the loop is executed the index of the searched part of the array will change. With this, every time a new index is selected, the value inside that index will be sum in the variable “sum”.
Comments