+ 2
Can anyone please explain ARRAYS IN LOOPS ..... Im a beginner .. please
3 Answers
+ 3
Arrays in loop usually used for automatically define a new variable for a data type (eg. int).
here is example :
int main () {
int n[ 10 ]; // n is an array of 10 integers
// initialize elements of array n to 0
for ( int i = 0; i < 10; i++ ) {
n[ i ] = i + 100; // set element at location i to i + 100
}
cout << "Element" << setw( 13 ) << "Value" << endl;
// output each array element's value
for ( int j = 0; j < 10; j++ ) {
cout << setw( 7 )<< j << setw( 13 ) << n[ j ] << endl;
}
return 0;
}
+ 1
Array is a set of variables of same data type.
Loops are used to iterate a piece of code.
+ 1
Think of any array as a list of variables. A loop can allows you to move through that list 1 by 1, accessing each member variable.