Program to Check Multiplicability of Two Matrices in C

Spread the love
Check-Multiplicability-of-Two-Matrices-in-C-language

Program Description:

Write a Program to Check Multiplicability of Two Matrices in C programming language. The program should take the Two matrices’ row sizes and column sizes and check if the matrices can be multiplied.

Before going to look at the program to check Multiplicability of Two Matrices, Let’s understand what is meant by the Multiplicability of matrices.

What is meant by Multiplicability of Two Matrices?

Let’s say we have two matrices X and Y. The dimensions of the first matrix X is m x n. where m is equal to the number of rows in the X and n is equal to the number of columns in the X.

The dimensions of the second matrix Y is n x q.

The two matrices can be multiplicable only if the number of columns in the first Matrix(X) is equal to the number of rows in the second matrix(Y). This is called the Multiplicability of two matrices.

In the above example, The X matrix has dimensions as m x n and Y Matrix has dimensions as n x q. So the Matrices X and Y satisfy the condition and we can multiply the Matrix X and Matrix Y. The multiplication of the Matrix X and Matrix Y generates a new matrix called Matrix Z ( Z = X * Y). The matrix Z will have dimensions as m x q.

📢 Please look at the Matrix Multiplication Program Where we explained matrix multiplication in detail.

Example Input and Output:

Input:

Output:

Program Prerequisites:

Check Multiplicability of Two Matrices in C Explanation/Algorithm:

  1. As we are not going to create any Matrices in the program, We no need to define any arrays.
  2. Declare four variables named rows1, columns1, rows2, and columns2.
  3. Prompt the user to provide the Row size and Column size of the First Matrix. And store the provided values in the rows1 and columns1 variables respectively.
  4. Similarly, Take the Second Matrix row size and column size from the user and update the rows2 and column2 variables.
  5. To check the Multiplicability of above two matrices, Compare the columns1 of the first Matrix and rows2 of the second matrix
    • If the (columns1 != rows2) is true, which means, first matrix column and second matrix rows are not equal, So we can’t Multiply the matrices. Display the result on the console ERROR: Can't Multiply Matrices, Please try again\n
    • If the above condition is false, Then the First Matrix columns and second matrix rows are equal, So we can multiply the two matrices. Display the success message on the console – SUCCESS: We can Mutliply the Matrices X and Y
  6. Stop the program.

Program to Check Multiplicability of Two Matrices in C Language using Loops:

Here is the C Program to check if we can multiply two matrices.

Program Output:

Compile the program using GCC (Any compiler)

$ gcc check-multiplicability.c

Run the program

Test 1: Positive Cases:

Check-Multiplicability-of-Two-Matrices-in-c-program-output

As we can see from the above output, In the first example, The First matrix column size ( 3) is equal to the second matrix row size( 3). So the program displayed we can multiply the matrices

Test 2: Negative Cases:

In the above examples, The column size of the first matrix is not equal to the row size of the second matrix, So we can’t multiply these matrices.

Program to Check Multiplicability of Two Matrices using Functions in C:

Let’s rewrite the above program to use a function ( canMutliply) to check the Multiplicability of two matrices.

Here is the program with canMutliply function

📢As we are using the bool, We need to include the stdbool.h header file.

We have defined a user-defined function called canMultiply. The canMulitply() function will check the Multiplicability of two matrices and return the results.

  • Prototype of the function – bool canMutliply(int rows1, int columns1, int rows2, int columns2)
  • This function takes four Formal Arguments, They are rows1, columns1, rows2, and columns2
  • canMultiply() function returns a Boolean Variable.
  • If the matrices are multiplicable, Then canMultiply() function returns One(1), Otherwise, It returns Zero(0)

We called the canMultiply() function from the main() function and based on the return value, We displayed the result on the console.

Program Output:

Let’s compile and run the program and observe the results.

Check-Multiplicability-of-Two-Matrices-using-functions

As we can see from the above output, The program is generating the desired results

Related Array Programs:

Venkat

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

Leave a Reply