/* File: fig07_04.cpp */ #include #include int main() { // Use initializer list to initialize array n int n[10] = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 }; // If the array size is omitted from a declaration with an initializer list, // the compiler determines the number of elements in an array, by counting the number // of elements in the initializer list. Therefore, // int n[] = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 }; // means the same thing as // int n[10] = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 }; // If the array size and initializer are specified in an array declaration, // the number of initializers MUST be less than or equal to the array size // otherwise the array declaration will cause a compilation error. // If there are fewer initializers than elements in an array, the remaining elements // will be initialized to zero. This means, that // int n[10] = {0}; // can be used to initialize elements of an array n to 0 instead of using // for(int i = 0; i < 10; i++){n[i] = 0;}; // output each array element's value std::cout << "Element" << std::setw(13) << "Value" << std::endl; for(int j = 0; j < 10; j++) { std::cout << std::setw(7) << j << std::setw(13) << n[j] << std::endl; }; return 0; };