/* This program shows you how to use arrays in C++. These arrays are low level contructs. For more serious work that needs linear algebra concepts use a librarym e.g. uBlas from the boost library. */ #include // for cout, cin, etc #include // include manipulator definitions using namespace std; int main() { int a[2][3] = {{2, 2, 2}, {2, 2, 2}}, b[2][3] = {{3, 3, 3}, {3, 3, 3}}, c[2][3], i, j; // c = a + b for (i=0; i<2; ++i) for (j=0; j<3; ++j) c[i][j] = a[i][j] + b[i][j]; // print c for (i=0; i<2; ++i) for (j=0; j<3; ++j) cout << " " << c[i][j]; cout << endl; // Do i=1,2; Print *, c(i,:); End Do for (i=0; i<2; ++i) { for (j=0; j<3; ++j) cout << " " << c[i][j]; cout << endl; } // c = a*b for (i=0; i<2; ++i) for (j=0; j<3; ++j) c[i][j] = a[i][j] * b[i][j]; // Print "(/,(3i5))", (c(i,:), i=1,2) for (i=0; i<2; ++i) { for (j=0; j<3; ++j) cout << setw(5) << c[i][j]; cout << endl; } // c(1,:) = a(1,:) + b(1,:) i = 0; for (j=0; j<3; ++j) c[i][j] = a[i][j] + b[i][j]; // c(2,:) = a(2,:) * b(2,:) i = 1; for (j=0; j<3; ++j) c[i][j] = a[i][j] * b[i][j]; // Print "(/,(3i5))", (c(i,:), i=1,2) for (i=0; i<2; ++i) { for (j=0; j<3; j++) cout << setw(5) << c[i][j]; cout << endl; } // Program outputs: // 5 5 5 5 5 5 // 5 5 5 // 5 5 5 // 6 6 6 // 6 6 6 // 5 5 5 // 6 6 6 }