// Project arrays.cpp // Show how to use arrays // in C++. #include int main() { // Declare one-dimensional array on stack. int a[5], i, j; // Assign values to array. a[0]=4; a[1]=0; a[2]=5; a[3]=9; a[4]=2; // Print array. for(i = 0; i <= 4; i++) cout << a[i] << " "; cout << endl; // No array bounds checking in C++. cout << a[5] << endl; // Can combine array declaration and // initialization like Java. int b[] = {4, 0, 5, 9, 2}; for(i = 0; i <= 4; i++) cout << b[i] << " "; cout << endl << endl; // Create and print 2-dimensional array. int c[3][3]; c[0][0] = 34; c[0][1] = 96; c[0][2] = 18; c[1][0] = 48; c[1][1] = 30; c[1][2] = 18; c[2][0] = 29; c[2][1] = 31; c[2][2] = 92; for(i = 0; i <= 2; i++) { for(j = 0; j <= 2; j++) cout << c[i][j] << " "; cout << endl; } return 0; } // Output: 4 0 5 9 2 1245120 4 0 5 9 2 34 96 18 48 30 18 29 31 92