0% found this document useful (0 votes)
126 views3 pages

Addition and Subtraction of Matrices

This program allows a user to input values for two 3x3 matrices, A and B. The user then selects either addition or subtraction. The program then performs the selected operation on the corresponding elements of the two matrices and displays the resulting sum or difference matrix. It uses nested for loops to iterate through each element, perform the calculation, and output the results to the console.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
126 views3 pages

Addition and Subtraction of Matrices

This program allows a user to input values for two 3x3 matrices, A and B. The user then selects either addition or subtraction. The program then performs the selected operation on the corresponding elements of the two matrices and displays the resulting sum or difference matrix. It uses nested for loops to iterate through each element, perform the calculation, and output the results to the console.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

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

You might also like