Home
Answers
Viewqa
Hibernate
C++ question - add the two matrices and display the elements of resultant matrix.

QuestionAsk Questions?

Posted in : Hibernate

- Write a program to accept from user elements of two 2x3 matrices. Then add the two matrices and display the elements of resultant matrix.

View Answers

May 8, 2012 at 3:31 PM

**C sum of two matrices**

**The given code allow the user to enter the number of rows and columns of matrix. Accordingly, the code accepts the row and column values from the user and determine their sum.**

#include <stdio.h> #include <conio.h> void main() { int m, n, c, d, first[10][10], second[10][10], sum[10][10]; printf("Enter the number of rows and columns of matrix "); scanf("%d%d", &m, &n); printf("Enter the elements of first matrix\n"); for ( c = 0 ; c < m ; c++ ) for ( d = 0 ; d < n ; d++ ) scanf("%d", &first[c][d]); printf("Enter the elements of second matrix\n"); for ( c = 0 ; c < m ; c++ ) for ( d = 0 ; d < n ; d++ ) scanf("%d", &second[c][d]); for ( c = 0 ; c < m ; c++ ) for ( d = 0 ; d < n ; d++ ) sum[c][d] = first[c][d] + second[c][d]; printf("Sum of entered matrices:-\n"); for ( c = 0 ; c < m ; c++ ) { for ( d = 0 ; d < n ; d++ ) printf("%d\t", sum[c][d]); printf("\n"); } getch(); }

May 8, 2012 at 3:32 PM

If you another example then please go through the following link:

May 8, 2012 at 3:35 PM

**C++ Sum of matrix**

**The given code allow the user to enter the number of rows and columns of matrix. Accordingly, the code accepts the row and column values from the user and determine their sum.**

#include<iostream> #include<stdio.h> #include<conio.h> using namespace std; void main() { int m, n, c, d, first[10][10], second[10][10], sum[10][10]; cout << "Enter the number of rows and columns of matrix "; cin >> m >> n; cout << "Enter the elements of first matrix\n"; for ( c = 0 ; c < m ; c++ ) for ( d = 0 ; d < n ; d++ ) cin >> first[c][d]; cout << "Enter the elements of second matrix\n"; for ( c = 0 ; c < m ;c++ ) for ( d = 0 ; d < n ; d++ ) cin >> second[c][d]; for ( c = 0 ; c < m ; c++ ) for ( d = 0 ; d < n ; d++ ) sum[c][d] = first[c][d] + second[c][d]; cout << "Sum of entered matrices:-\n"; for ( c = 0 ; c < m ; c++ ) { for ( d = 0 ; d < n ; d++ ) cout << sum[c][d] << "\t"; cout << endl; } getch(); }

Related Tutorials/Questions & Answers:

Advertisements