Republic of the Philippines
CENTRAL BICOL STATE UNIVERSITY OF AGRICULTURE
Sipocot, Camarines Sur
COLLEGE OF INFORMATION TECHNOLOGY
CC 104 – Data Structures and Algorithms
1st semester, AY 2019 – 2020
Addition and Subtraction of Matrices
by: Jerico C. Villanueva
Description:
This program is an array which the user needs to input 3 rows and 3 columns. This
array has a two sets of rows and columns which is matrix A and B. And user will choose
of the operations to solve the matrix. Addition and subtraction will be the operations of
this program. The process of solving will be like this the both of the first number
of matrices A and B will be added or subtract first, until the last number of matrices A and
B. The answer will be displayed the sum or difference of matrix A and B.
Source Code:
#include <iostream>
using namespace std;
int main ()
{
int a[3][3];
int b[3][3];
char operation;
for ( int i = 0; i <3; i++ )
for ( int j= 0; j < 3; j++ )
{
cout << "1st Matrix. Index:a[" << i << "] [" << j
<< "]: ";
cin >> a[i] [j];
}
cout<<"\n";
for ( int i = 0; i <3; i++ )
for ( int j= 0; j < 3; j++ )
{
cout << "2nd Matrix. Index:b[" << i << "] [" << j
<< "]: ";
cin >> b[i] [j];
}
cout <<"\nSelect an Operation: (+) or (-): " ;
cin >> operation;
cout <<"\nMATRIX A: \n";
for ( int i= 0; i < 3; i++ ) {
for ( int j= 0; j < 3; j++ )
{
cout << a[i] [j]<<" ";
}
cout <<endl;
}
cout <<"\nMATRIX B: \n";
for ( int i= 0; i < 3; i++ ) {
for ( int j= 0; j < 3; j++ )
{
cout << b[i] [j]<<" ";
}
cout <<endl;
}
if (operation== '+') {
cout <<"\nThe Sum of A and B is:\n";
for ( int i= 0; i < 3; i++ ) {
for ( int j= 0; j < 3; j++ )
{
int result = a[i][j]+b[i][j];
cout << result<<" ";
}
cout <<endl;
}
}
else if (operation== '-') {
cout <<"\nThe Difference of A and B is:\n";
for ( int i= 0; i < 3; i++ ) {
for ( int j= 0; j < 3; j++ )
{
int result = a[i][j]-b[i][j];
cout << result<<" ";
}
cout <<endl;
}
}
return 0;
}
Output:
Addition Subtraction