LAB 7.docx - CSL-113 Computer Programming Lab Semester BS CS – 01 Exercise 1 Write a C Program that computes the sum of two matrices Each matrix is of

# LAB 7.docx - CSL-113 Computer Programming Lab Semester BS...

• 6

This preview shows page 1 - 3 out of 6 pages.

CSL-113: Computer Programming Lab Semester BS CS – 01 Exercise: 1 Write a C++ Program that computes the sum of two matrices. Each matrix is of 2 rows and 2 columns and will be created from user input. Program #include <iostream> using namespace std; int main() { int A[2][2], B[2][2]; //declaration //input for ( int i = 0; i < 2; i++) { for ( int j = 0; j < 2; j++) { cout << "Enter [" << i << "][" << j << "] of Matrix A: " ; cin >> A[i][j]; } } for ( int i = 0; i < 2; i++) { for ( int j = 0; j < 2; j++) { cout << "Enter [" << i << "][" << j << "] of Matrix B: " ; cin >> B[i][j]; } } //output for ( int i = 0; i < 1; i++) { for ( int j = 0; j < 2; j++) { if (j == 0) { cout << "A = " ; } cout << A[i][j] << "\t" ; } for ( int j = 0; j < 2; j++) { if (j == 0) { cout << "+\tB = " ; } cout << B[i][j] << "\t" ; } for ( int j = 0; j < 2; j++) { if (j == 0) { Department of Computer Sciences Page: 1/6 Semester BSCS 01 CSL-113: Computer Programming Lab Lab 07: Multi-Dimensional Arrays
CSL-113: Computer Programming Lab Semester BS CS – 01 cout << "=\tC = " ; } cout << A[i][j] + B[i][j] << "\t" ; } } cout << endl << " " ; for ( int i = 1; i < 2; i++) { for ( int j = 0; j < 2; j++) { cout << A[i][j] << "\t" ; } cout << " " ; for ( int j = 0; j < 2; j++) { if (j == 0) { cout << " \t " ; } cout << B[i][j] << "\t" ; } cout << " " ; for ( int j = 0; j < 2; j++) { if (j == 0) { cout << " \t " ; } cout << A[i][j] + B[i][j] << "\t" ; } } cout << endl; system( "pause" ); return 0; } Output

#### You've reached the end of your free preview.

Want to read all 6 pages?

• Fall '16
• Sumaira Kausar

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern