✨✨ hello everyone ✨✨
⚡⚡ only for computer science student ⚡⚡
✴ questions
❄❄program to add two matrices?
............................................................................
please don't answer when u don't know.
only computer science student touch ?
Answers
Answered by
37
answer :
first remember that sum and different operations are possible on two identical matrices.for instance , A [n] [m] and B [p] [Q] are the two given matrices , then, in order to program sum and difference operations following must be true :
n = p and m = q
the program to add two matrices is as follows :
#include <iostream.h>
#include <process .h > // for exist ( ) function
int main ( )
{
int a [ 10 ] [ 10 ] , b [ 10 ] [10] , c [ 10 ] [10] ;
int i , j , m, n, p , q ;
// Input rows & columns of matrices A [ m]
[n] and B [ p ] [ q] .
cout << " \ n Input row & column of matrix - A \ n " ;
cin >> m >> n;
cout <<" \ nInput row & column of matrix - B \n " ;
cin >> p >> q;
if (( m = = p ) & & ( n = = q) ) // check if matrix can be added .
cout <<" matrix can be added \ n" ;
else
{ cout << " matrix cannot be added \ n " ;
exist ( 0 ) ;
}
cout << " \ nInput matrix - A \n " ;
for ( i = 0 ; i <m ; i ++ )
{
for ( j = 0 ; j < n ; j + + )
cin >> a [i ] [ j] ;
}
cout << " \nMATRIX -A :" ;
for ( i = 0 ; i < m ; i + + ) // Print matrix A in matrix form.
{ cout << "/ n" ;
for ( j = 0 ; j < n ; j + + )
cout << " " << a [ i ] [ j ] ;
}
cout << " \ nInput matrix- B \ n ; . // Loop to accept matrix B.
for ( i = 0 ; i < p ; i + + )
{
for ( j = 0 ; j < q ; j + + )
cin>> b [ i ] [ j ] ;
}
cout < < " \ n MATRIX - B : " ;
for ( i = 0 ; i < p ; i + + ) // print matrix B in matrix form .
{ cout << " \ n " ;
for ( j = 0 ; j < q ; j ++)
cout << " " << b [ i ] [ j ] ;
}
for ( i = 0 ; i < m ; i ++ ) addiction of two matrix .
{
for ( j = 0 ; j < n ; j + + )
c [ i ] [ j ] = a [ i ] [ j ] ;
}
cout << " \ n The sum of two matrix is : \ n " ;
for. ( i = 0 ; i < m ; i + + )
{
cout << " \ n " ;
for ( j = 0 ; j < n ; j ++ )
cout << " " << c [ i ] [ j ] ;
}
return 0;
}
first remember that sum and different operations are possible on two identical matrices.for instance , A [n] [m] and B [p] [Q] are the two given matrices , then, in order to program sum and difference operations following must be true :
n = p and m = q
the program to add two matrices is as follows :
#include <iostream.h>
#include <process .h > // for exist ( ) function
int main ( )
{
int a [ 10 ] [ 10 ] , b [ 10 ] [10] , c [ 10 ] [10] ;
int i , j , m, n, p , q ;
// Input rows & columns of matrices A [ m]
[n] and B [ p ] [ q] .
cout << " \ n Input row & column of matrix - A \ n " ;
cin >> m >> n;
cout <<" \ nInput row & column of matrix - B \n " ;
cin >> p >> q;
if (( m = = p ) & & ( n = = q) ) // check if matrix can be added .
cout <<" matrix can be added \ n" ;
else
{ cout << " matrix cannot be added \ n " ;
exist ( 0 ) ;
}
cout << " \ nInput matrix - A \n " ;
for ( i = 0 ; i <m ; i ++ )
{
for ( j = 0 ; j < n ; j + + )
cin >> a [i ] [ j] ;
}
cout << " \nMATRIX -A :" ;
for ( i = 0 ; i < m ; i + + ) // Print matrix A in matrix form.
{ cout << "/ n" ;
for ( j = 0 ; j < n ; j + + )
cout << " " << a [ i ] [ j ] ;
}
cout << " \ nInput matrix- B \ n ; . // Loop to accept matrix B.
for ( i = 0 ; i < p ; i + + )
{
for ( j = 0 ; j < q ; j + + )
cin>> b [ i ] [ j ] ;
}
cout < < " \ n MATRIX - B : " ;
for ( i = 0 ; i < p ; i + + ) // print matrix B in matrix form .
{ cout << " \ n " ;
for ( j = 0 ; j < q ; j ++)
cout << " " << b [ i ] [ j ] ;
}
for ( i = 0 ; i < m ; i ++ ) addiction of two matrix .
{
for ( j = 0 ; j < n ; j + + )
c [ i ] [ j ] = a [ i ] [ j ] ;
}
cout << " \ n The sum of two matrix is : \ n " ;
for. ( i = 0 ; i < m ; i + + )
{
cout << " \ n " ;
for ( j = 0 ; j < n ; j ++ )
cout << " " << c [ i ] [ j ] ;
}
return 0;
}
Anonymous:
nice answer bro !!
Similar questions