Program to Find subtraction of two matrices

Problem Statement:- Program to find the subtraction of two matrices or 2D Array.

Sample Input/Output:-


Sample Input First: 2 2

4 5 6 7


1 2 3 4

Sample Output First: 

3 3 3 3

Sample Input Second:3 3 

1 2 3 4 5 6 7 8 9


0 1 1 2 3 6 5 2 1

Sample Output Second: 

1 1 2 2 2 0 2 6 8


Explanation:- 


For 1st output: { matrix[0][0]-matrix1[0][0] } { matrix[0][1]-matrix1[0][1] } { matrix[1][0]-matrix1[1][0] } { matrix[1][1]-matrix1[1][1] }={ 4-1 } {5-2 } {6-3 }.{ 7-4}=3 3 3 3


For 2nd output: { matrix[0][0]-matrix1[0][0] } { matrix[0][1]-matrix1[0][1] }........{ matrix[2][2]-matrix1[2][2] }={ 1-0 } {2-1 }.............{ 9-1 }=1 1...........8


Data requirement:-

  Input Data:- row_size, col_size, matrix[][], matrix1[][]


  Output Data:-sub_matrix[][]


  Additional Data:- i, j


Program in C

Here is the source code of the C Program to find the subtraction of two matrices.


Code:


#include<stdio.h> int main() { int row_size,col_size; //Get size of matrix printf("Enter the row Size Of the Matrix:"); scanf("%d",&row_size); printf("Enter the columns Size Of the Matrix:"); scanf("%d",&col_size); int matrix[row_size][col_size]; //Taking input of the 1st matrix int i,j; printf("Enter the 1st Matrix Element:\n"); for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { scanf("%d",&matrix[i][j]); } } int matrix1[row_size][col_size]; //Taking input of the 2nd matrix printf("Enter the 2nd Matrix Element:\n"); for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { scanf("%d",&matrix1[i][j]); } } int sub_matrix[row_size][col_size]; //Compute Subtraction of two matrices for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { sub_matrix[i][j]=matrix[i][j]-matrix1[i][j]; } } //display the Subtraction of two matrices printf("Subtraction of the two Matrices is:\n"); for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { printf("%d ",sub_matrix[i][j]); } printf("\n"); } }


Input/Output:

Enter the row Size Of the Matrix:2 Enter the columns Size Of the Matrix:2 Enter the 1st Matrix Element: 4 5 6 7 Enter the 2nd Matrix Element: 1 2 3 4 Subtraction of the two Matrices is: 3 3 3 3


Program in C++

Here is the source code of the C++ Program to perform the subtraction of two 2*3 matrices.


Code:


#include<iostream> using namespace std; int main() { int row_size,col_size; //Get size of matrix cout<<"Enter the row Size Of the Matrix:"; cin>>row_size; cout<<"Enter the columns Size Of the Matrix:"; cin>>col_size; int matrix[row_size][col_size]; //Taking input of the 1st matrix int i,j; cout<<"Enter the 1st Matrix Element:\n"; for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { cin>>matrix[i][j]; } } int matrix1[row_size][col_size]; //Taking input of the 2nd matrix cout<<"Enter the 2nd Matrix Element:\n"; for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { cin>>matrix1[i][j]; } } int sub_matrix[row_size][col_size]; //Compute Subtraction of two matrices for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { sub_matrix[i][j]=matrix[i][j]-matrix1[i][j]; } } //display the Subtraction of two matrices cout<<"Subtraction of the two Matrices is:\n"; for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { cout<<sub_matrix[i][j]<<" "; } cout<<"\n"; } }


Input/Output:

Enter the row Size Of the Matrix:3 Enter the columns Size Of the Matrix:3 Enter the 1st Matrix Element: 1 2 3 4 5 6 7 8 9 Enter the 2nd Matrix Element: 0 1 1 2 3 6 5 2 1 Subtraction of the two Matrices is: 1 1 2 2 2 0 2 6 8


Program in Java

Here is the source code of the Java Program to Program to find the subtraction of two matrices.


Code:


import java.util.Scanner; public class SubtractionOfTwoMatrices { public static void main(String[] args) { Scanner cs=new Scanner(System.in); int row_size,col_size; //Get size of matrix System.out.print("Enter the row Size Of the Matrix:"); row_size=cs.nextInt(); System.out.print("Enter the columns Size Of the Matrix:"); col_size=cs.nextInt(); int matrix[][] =new int[row_size][col_size]; //Taking input of the 1st matrix int i,j; System.out.println("Enter the 1st Matrix Element:"); for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { matrix[i][j]=cs.nextInt(); } } int matrix1[][] =new int[row_size][col_size]; //Taking input of the 2nd matrix System.out.println("Enter the 2nd Matrix Element:"); for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { matrix1[i][j]=cs.nextInt(); } } int sub_matrix[][] =new int[row_size][col_size]; //Compute Subtraction of two matrices for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { sub_matrix[i][j]=matrix[i][j]-matrix1[i][j]; } } //Display Subtraction of two matrices System.out.println("Subtraction of the two Matrices is:"); for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { System.out.print(sub_matrix[i][j]+" "); } System.out.println(); } cs.close(); } }


Input/Output:

Enter the row Size Of the Matrix:2 Enter the columns Size Of the Matrix:3 Enter the 1st Matrix Element: 7 11 12 9 13 17 Enter the 2nd Matrix Element: 4 5 6 7 8 9 Subtraction of the two Matrices is: 3 6 6 2 5 8


Program in Python

Here is the source code of the Python Program to find the subtraction of two matrices.


Code:


# Get size of matrix row_size=int(input("Enter the row Size Of the Matrix:")) col_size=int(input("Enter the columns Size Of the Matrix:")) matrix=[] # Taking input of the 1st matrix print("Enter the Matrix Element:") for i in range(row_size): matrix.append([int(j) for j in input().split()]) matrix1=[] # Taking input of the 2nd matrix print("Enter the Matrix Element:") for i in range(row_size): matrix1.append([int(j) for j in input().split()]) # Compute Subtraction of two matrices sub_matrix=[[0 for i in range(col_size)] for i in range(row_size)] for i in range(len(matrix)): for j in range(len(matrix[0])): sub_matrix[i][j]=matrix[i][j]-matrix1[i][j] # display the Subtraction of two matrices print("Subtraction of the two Matrices is:") for m in sub_matrix: print(m)


Input/Output:

Enter the row Size Of the Matrix:2 Enter the columns Size Of the Matrix:2 Enter the Matrix Element: 44 16 33 14 Enter the Matrix Element: 10 6 29 16 Subtraction of the two Matrices is: [34, 10] [4, -2]


Most Recommend Questions:-








More Questions:-




Array Programming Questions and Solutions(50)

C/C++/Java/Python Practice Question

Post a Comment

0 Comments