Iterating through arrays
Any loop can be used to iterate through the array elements. Example :
Example
#include <iostream> #include <iomanip> // for setw using namespace std; 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] = 0; // set element at location i to 0 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; getchar(); return 0; }
Output
Element Value 0 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0