// arr1.cpp By: Aiman Hanna - ©1993-2006 Aiman Hanna // This program introduces arrays, and illustrates how they can be manipulated. // Key Points: 1) Arrays always start with index "0", and go up to the array size - 1. // For example, the indices of an array of 6 elements go from index 0 // to index 5. #include void ArrConstruct() { // this function illustrates how arrays are created and manipulated. // The function creates an array of 6 integers, initializes it, then // manipulates it. int firstarr[ 6 ]; // "firstarr" is the name of the array. "6" here is the // number of indices. // Initialize the array. Notice that the array starts always with a "0" index // and it ends with the number of indices- 1, which is 5 in our example. firstarr[0] = 2; firstarr[1] = 4; firstarr[2] = 6; firstarr[3] = 12; firstarr[4] = 15; firstarr[5] = 17; // Show out all the values in the array cout << "\n Here are the array values after initialization" << endl; cout << " ===============================================" << endl; for (int i = 0; i < 6; i++) cout << firstarr[i] << "\t"; // Change the values of the 3rd & 5th elements. Notice that since the array starts // always with a 0 index, the 3rd element is the one with index # 2, and the fifth // element is the one with index # 4 firstarr[2] = 90; // 3rd element firstarr[4] = 200; // 5th element cout << "\n Here are the array values after 1st modification" << endl; cout << " =================================================" << endl; for ( i = 0; i < 6; i++) cout << firstarr[i] << "\t"; // Allow the user to change the values of the 1st and 4th elements cout << "\n Enter two integers: "; cin >> firstarr[0] >> firstarr[3]; cout << "\n Here are the array values after 2nd modification" << endl; cout << " =================================================" << endl; for ( i = 0; i < 6; i++) cout << firstarr[i] << "\t"; // Find the total of the values in the array int totalval = 0; for ( i = 0; i < 6; i++) totalval += firstarr[i]; cout << "\n The total of the values inside the array is: " << totalval << endl; } int main() { ArrConstruct(); return 0; } // The output of the program /* Here are the array values after initialization =============================================== 2 4 6 12 15 17 Here are the array values after 1st modification ================================================= 2 4 90 12 200 17 Enter two integers: 20 89 Here are the array values after 2nd modification ================================================= 20 4 90 89 200 17 The total of the values inside the array is: 420 */