0% found this document useful (0 votes)
85 views

2.matrix Addition and Subtraction Using 2-D Array PDF

This program performs matrix addition and subtraction of two matrices of any size entered by the user. It prompts the user to enter the number of rows and columns, then declares arrays to store the matrix elements and results. The user enters the elements of each matrix, then nested loops are used to perform element-wise addition and subtraction storing the results in separate arrays which are then printed.

Uploaded by

Arnab Dey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views

2.matrix Addition and Subtraction Using 2-D Array PDF

This program performs matrix addition and subtraction of two matrices of any size entered by the user. It prompts the user to enter the number of rows and columns, then declares arrays to store the matrix elements and results. The user enters the elements of each matrix, then nested loops are used to perform element-wise addition and subtraction storing the results in separate arrays which are then printed.

Uploaded by

Arnab Dey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Lab-Report-2

Matrix addition and subtraction using 2-D array


Objectives:
This program is designed to perform matrix addition and subtraction of two matrices entered by
the user. It first prompts the user to enter the number of rows and columns for the matrices. The
program then declares two 2-D arrays, mat1 and mat2, with the given number of rows and columns.
Additionally, two more arrays, sum and diff, are declared to store the result of matrix addition and
subtraction, respectively. The user is prompted to enter the elements of mat1 and mat2 using nested
loops. Two nested loops are used to perform matrix addition and store the result in the sum array.
Similarly, two more nested loops are used to perform matrix subtraction and store the result in the
diff array. Finally, the sum and difference matrices are printed to the console using nested loops.
The program then ends.

Code:
#include <iostream>

using namespace std;

int main()

int rows, cols;

cout << "Enter number of rows and columns: ";

cin >> rows >> cols;

int mat1[rows][cols], mat2[rows][cols], sum[rows][cols], diff[rows][cols];

// Get input for mat1

cout << "Enter elements of matrix 1: " << endl;

for (int i = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) {

cin >> mat1[i][j];

// Get input for mat2

cout << "Enter elements of matrix 2: " << endl;

for (int i = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) {


cin >> mat2[i][j];

// Perform matrix addition

for (int i = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) {

sum[i][j] = mat1[i][j] + mat2[i][j];

// Perform matrix subtraction

for (int i = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) {

diff[i][j] = mat1[i][j] - mat2[i][j];

// Print the sum matrix

cout << "Sum of the matrices is: " << endl;

for (int i = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) {

cout << sum[i][j] << " ";

cout << endl;

// Print the difference matrix

cout << "Difference of the matrices is: " << endl;

for (int i = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) {

cout << diff[i][j] << " ";

cout << endl;

return 0;
}

Input:
Enter number of rows and columns: 2 2
Enter elements of matrix 1:
12
34
Enter elements of matrix 2:
56
78

Output:
Sum of the matrices is:
68
10 12
Difference of the matrices is:
-4 -4
-4 -4

You might also like