Matrix Operations in C | Addition, Multiplication, Transpose

Matrix Operations in C. Previously we had developed multiple C program on matrix like C program to find the Addition of two Matrix, C program to find the Subtraction of two matrices, C Program to Find Multiplication of two Matrix, C program to find the transpose of a matrix, Sum of diagonal elements in C, C program to Find out each row sum and column sum of a matrix. Now, let us develop a program to perform various matrix operations addition, subtraction, multiplication, transpose using switch-case statement and function.

Here we will write C Program to perform matrix addition, subtraction, multiplication, and transpose. We will develop appropriate C functions for the following to perform matrix addition, subtraction, multiplication, and transpose operations.

Matrix is a two-dimensional array. And to represent the two-dimensional array there should be two loops, where outer loops represent rows of the matrix and the inner loop represents the column of the matrix. Now, let us start with the addition of two matrices.

matrix

C Program for Matrix Addition Subtraction and Multiplication Using Functions and switch-case

Program description:- Write a C program to perform various matrix operations addition, subtraction, multiplication, transpose using switch-case statement and function.

In this program, we will perform all operations through the 3×3 matrix, and the input for the matrix is given explicitly but you can ask for the input from the end-user. See:- How to take and display matrix in C.

#include<stdio.h>
#include<stdlib.h>

// function to add two 3x3 matrix
void add(int m[3][3], int n[3][3], int sum[3][3])
{
  for(int i=0;i<3;i++)
    for(int j=0;j<3;j++)
      sum[i][j] = m[i][j] + n[i][j];
}

// function to subtract two 3x3 matrix
void subtract(int m[3][3], int n[3][3], int result[3][3])
{
  for(int i=0;i<3;i++)
    for(int j=0;j<3;j++)
      result[i][j] = m[i][j] - n[i][j];
}

// function to multiply two 3x3 matrix
void multiply(int m[3][3], int n[3][3], int result[3][3])
{
  for(int i=0; i < 3; i++)
  {
    for(int j=0; j < 3; j++)
    {
      result[i][j] = 0; // assign 0
      // find product
      for (int k = 0; k < 3; k++)
      result[i][j] += m[i][k] * n[k][j];
    }
  }
}

// function to find transpose of a 3x3 matrix
void transpose(int matrix[3][3], int trans[3][3])
{
  for (int i = 0; i < 3; i++)
    for (int j = 0; j < 3; j++)
      trans[i][j] = matrix[j][i];
}

// function to display 3x3 matrix
void display(int matrix[3][3])
{
  for(int i=0; i<3; i++)
  {
    for(int j=0; j<3; j++)
      printf("%d\t",matrix[i][j]);

    printf("\n"); // new line
  }
}

// main function
int main()
{
  // matrix
  int a[][3] = { {5,6,7}, {8,9,10}, {3,1,2} };
  int b[][3] = { {1,2,3}, {4,5,6}, {7,8,9} };
  int c[3][3];

  // print both matrix 
  printf("First Matrix:\n");
  display(a);
  printf("Second Matrix:\n");
  display(b);

  // variable to take choice
  int choice;

  // menu-driven
  do
  {
    // menu to choose the operation
    printf("\nChoose the matrix operation,\n");
    printf("----------------------------\n");
    printf("1. Addition\n");
    printf("2. Subtraction\n");
    printf("3. Multiplication\n");
    printf("4. Transpose\n");
    printf("5. Exit\n");
    printf("----------------------------\n");
    printf("Enter your choice: ");
    scanf("%d", &choice);

    switch (choice) {
      case 1:
        add(a, b, c);
        printf("Sum of matrix: \n");
        display(c);
        break;
      case 2:
        subtract(a, b, c);
        printf("Subtraction of matrix: \n");
        display(c);
        break;
      case 3:
        multiply(a, b, c);
        printf("Multiplication of matrix: \n");
        display(c);
        break;
      case 4:
        printf("Transpose of the first matrix: \n");
        transpose(a, c);
        display(c);
        printf("Transpose of the second matrix: \n");
        transpose(b, c);
        display(c);
        break;
      case 5:
        printf("Thank You.\n");
        exit(0);
      default:
        printf("Invalid input.\n");
        printf("Please enter the correct input.\n");
    }
  }while(1);

  return 0;
}

Output:-

First Matrix:
5 6 7
8 9 10
3 1 2
Second Matrix:
1 2 3
4 5 6
7 8 9

Choose the matrix operation,
----------------------
1. Addition
2. Subtraction
3. Multiplication
4. Transpose
5. Exit
----------------------
Enter your choice: 1
Sum of matrix:
6 8 10
12 14 16
10 9 11

Choose the matrix operation,
----------------------
1. Addition
2. Subtraction
3. Multiplication
4. Transpose
5. Exit
----------------------
Enter your choice: 2
Subtraction of matrix:
4 4 4
4 4 4
-4 -7 -7

Choose the matrix operation,
----------------------
1. Addition
2. Subtraction
3. Multiplication
4. Transpose
5. Exit
----------------------
Enter your choice: 3
Multiplication of matrix:
78 96 114
114 141 168
21 27 33

Choose the matrix operation,
----------------------
1. Addition
2. Subtraction
3. Multiplication
4. Transpose
5. Exit
----------------------
Enter your choice: 4
Transpose of the first matrix:
5 8 3
6 9 1
7 10 2
Transpose of the second matrix:
1 4 7
2 5 8
3 6 9

Choose the matrix operation,
----------------------
1. Addition
2. Subtraction
3. Multiplication
4. Transpose
5. Exit
----------------------
Enter your choice: 6
Invalid input.
Please enter the correct input.

Choose the matrix operation,
----------------------
1. Addition
2. Subtraction
3. Multiplication
4. Transpose
5. Exit
----------------------
Enter your choice: 5
Thank You.

While displaying transpose of the matrix we had displayed transpose of both matrix but you can ask user to choose the matrix, and display transpose of only that matrix.

Explanation

Matrix addition is the operation of adding two matrices by adding the corresponding entries together. The matrix can be added only when the number of rows and columns of the first matrix is equal to the number of rows and columns of the second matrix. In this program, we will take two square matrices of size 3×3. Matrix addition is very simple, just add the elements located at the same position with respect to the row and column. We had already discussed in-depth about matrix addition in C.

The matrix subtraction is very similar to the matrix addition operation. Instead of addition operator use subtraction operator and remaining things will be remain same. See more:- Matrix subtraction in C.

We can multiply two matrices if, and only if, the number of columns in the first matrix equals the number of rows in the second matrix. Otherwise, the product of two matrices is undefined. See more:- Matrix multiplication in C.

If A=[aij] be a matrix of order m x n, then the matrix obtained by interchanging the rows and columns of A is known as Transpose of matrix A. Transpose of matrix A is represented by AT. See more:- Transpose of a Matrix in C

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

1 thought on “Matrix Operations in C | Addition, Multiplication, Transpose”

Leave a Reply