// this program reads numbers into a 2D array with 3 rows and 4 // columns, prints a row, prints a column, then prints the whole // array #include <iostream.h> #include <iomanip.h> int main() { int sample[3][4]; int i,j; // read values into the whole array for (i = 0 ; i < 3 ; i++) { cout << "Enter 4 numbers: "; for (j = 0 ; j < 4 ; j++) cin >> sample[i][j]; } // print one row cout << "The first row contains: "; for (j = 0 ; j < 4 ; j++) cout << sample[0][j] << " "; cout << endl; // print one column cout << "The second column contains: "; for (i = 0 ; i < 3 ; i++) cout << sample[i][1] << " "; cout << endl; // print the whole array, row by row cout << "\nThe whole array:\n"; for (i = 0 ; i < 3 ; i++) { cout << "Row " << i << ": "; for (j = 0 ; j < 4 ; j++) cout << setw(4) << sample[i][j] << " "; cout << endl; } }
Enter 4 numbers: 2 4 6 8 Enter 4 numbers: 40 30 20 10 Enter 4 numbers: 12 9 6 3 The first row contains: 2 4 6 8 The second column contains: 4 30 9 The whole array: Row 0: 2 4 6 8 Row 1: 40 30 20 10 Row 2: 12 9 6 3
Email Me |
Office Hours |
My Home Page |
Department Home |
MCC Home Page
© Copyright Emmi Schatz 2002