during run time : Multiple assignment statements are required to insert values to the cells of the array during runtime. The for loop is ideally suited for iterating through the array elements. #include <iostream> using namespace std; int main() int num[ ]; for(int i= ; i< ; i++) cout<< "\n Enter value " << i+ << "= "; cin>>num[i]; // Input values while execution In the above program, a for loop has been constructed to execute the statements within the loop for times. During each iteration of the loop, cout statement prompts you to “Enter value …….” and cin gets the value and stores it in num[i]; The following table shows the execution of the above code block.
Chapter Page - - Iteration i < cout << "\n Enter value " << i+ << "= "; cin>>num [i]; Received value stored in memory i++ (i=i+ ) < (T) Enter value = num[ ] = num[ ] < (T) Enter value = num[ ] = num[ ] < (T) Enter value = num[ ] = num[ ] < (T) Enter value = num[ ] = num[ ] < (T) Enter value = num[ = ] num[ ] < (F) Exit from Loop Note In for loop, the index i is declared with an initial value (zero). Since in most of the cases, the initial value of the loop index will be used as the array subscript representation. Accessing array elements Array elements can be used anywhere in a program as we do in case of a normal variable. The elements of an array are accessed with the array name followed by the subscript index within the square bracket.
Example: cout<<num[ ]; In the above statement, num[ ] refers to the 4th element of the array and cout statement displays the value of num[ ]. Note The subscript in bracket can be a variable, a constant or an expression that evaluates to an integer. #include <iostream> using