Matrix Addition and Subtraction in C

CCBeginner
Practice Now

Introduction

In this lab, you will learn to perform matrix addition and subtraction in C programming language. This program will ask the user to input two matrices of the same size and then perform the addition and subtraction operation, printing the resulting matrices.

Note: You need to create the file ~/project/main.c yourself to practice coding and learn how to compile and run it using gcc.

cd ~/project
## create main.c
touch main.c
## compile main.c
gcc main.c -o main
## run main
./main

Set up the program

First, set up the program by including the necessary libraries and defining the main function.

#include<stdio.h>

int main()
{
    // Add code here
}

Declare variables

Declare the required variables in the main function. We need 2 matrices, one for each input, and 2 matrices to store the result of the addition and subtraction operations.

int n, m, c, d, first[10][10], second[10][10], sum[10][10], diff[10][10];

n and m are the number of rows and columns of the matrices.

first and second are the input matrices.

sum and diff are the matrices that will store the result of the addition and subtraction operations, respectively.

Take user input

Use scanf() function to ask the user the number of rows and columns of the matrices and then ask them to input the elements of the matrices.

printf("\nEnter the number of rows and columns of the first matrix \n\n");
scanf("%d%d", &m, &n);

printf("\nEnter the %d elements of the first matrix \n\n", m*n);
for(c = 0; c < m; c++)   // to iterate the rows
    for(d = 0; d < n; d++)   // to iterate the columns
        scanf("%d", &first[c][d]);

printf("\nEnter the %d elements of the second matrix \n\n", m*n);
for(c = 0; c < m; c++)   // to iterate the rows
    for(d = 0; d < n; d++)   // to iterate the columns
        scanf("%d", &second[c][d]);

Display the input matrices

Use printf() function to display the input matrices.

/*
    printing the first matrix
*/
printf("\n\nThe first matrix is: \n\n");
for(c = 0; c < m; c++)   // to iterate the rows
{
    for(d = 0; d < n; d++)   // to iterate the columns
    {
        printf("%d\t", first[c][d]);
    }
printf("\n");
}

/*
    printing the second matrix
*/
printf("\n\nThe second matrix is: \n\n");
for(c = 0; c < m; c++)   // to iterate the rows
{
    for(d = 0; d < n; d++)   // to iterate the columns
    {
        printf("%d\t", second[c][d]);
    }
printf("\n");
}

Matrix addition

Add the corresponding elements of the two matrices and store the result in the sum matrix.

for(c = 0; c < m; c++)
    for(d = 0; d < n; d++)
        sum[c][d] = first[c][d] + second[c][d];

Display the result of addition

Use printf() function to display the sum matrix.

// printing the elements of the sum matrix
printf("\n\nThe sum of the two entered matrices is: \n\n");
for(c = 0; c < m; c++)
{
    for(d = 0; d < n; d++)
    {
        printf("%d\t", sum[c][d]);
    }
    printf("\n");
}

Matrix Subtraction

Subtract the corresponding elements of the two matrices and store the result in the diff matrix.

for(c = 0; c < m; c++)
    for(d = 0; d < n; d++)
        diff[c][d] = first[c][d] - second[c][d];

Display the result of subtraction

Use printf() function to display the diff matrix.

// printing the elements of the diff matrix
printf("\n\nThe difference(subtraction) of the two entered matrices is: \n\n");
for(c = 0; c < m; c++)
{
    for(d = 0; d < n; d++)
    {
        printf("%d\t", diff[c][d]);
    }
    printf("\n");
}

Final code

Here is the final code for the program:

#include<stdio.h>

int main()
{
    int n, m, c, d, first[10][10], second[10][10], sum[10][10], diff[10][10];
    printf("\nEnter the number of rows and columns of the first matrix \n\n");
    scanf("%d%d", &m, &n);

    printf("\nEnter the %d elements of the first matrix \n\n", m*n);
    for(c = 0; c < m; c++)   // to iterate the rows
        for(d = 0; d < n; d++)   // to iterate the columns
            scanf("%d", &first[c][d]);

    printf("\nEnter the %d elements of the second matrix \n\n", m*n);
    for(c = 0; c < m; c++)   // to iterate the rows
        for(d = 0; d < n; d++)   // to iterate the columns
            scanf("%d", &second[c][d]);

    /*
        printing the first matrix
    */
    printf("\n\nThe first matrix is: \n\n");
    for(c = 0; c < m; c++)   // to iterate the rows
    {
        for(d = 0; d < n; d++)   // to iterate the columns
        {
            printf("%d\t", first[c][d]);
        }
    printf("\n");
    }

    /*
        printing the second matrix
    */
    printf("\n\nThe second matrix is: \n\n");
    for(c = 0; c < m; c++)   // to iterate the rows
    {
        for(d = 0; d < n; d++)   // to iterate the columns
        {
            printf("%d\t", second[c][d]);
        }
    printf("\n");
    }

    /*
        finding the SUM of the two matrices
        and storing in another matrix sum of the same size
    */
    for(c = 0; c < m; c++)
        for(d = 0; d < n; d++)
            sum[c][d] = first[c][d] + second[c][d];

    // printing the elements of the sum matrix
    printf("\n\nThe sum of the two entered matrices is: \n\n");
    for(c = 0; c < m; c++)
    {
        for(d = 0; d < n; d++)
        {
            printf("%d\t", sum[c][d]);
        }
        printf("\n");
    }

    /*
        finding the DIFFERENCE of the two matrices
        and storing in another matrix difference of the same size
    */
    for(c = 0; c < m; c++)
        for(d = 0; d < n; d++)
            diff[c][d] = first[c][d] - second[c][d];

    // printing the elements of the diff matrix
    printf("\n\nThe difference(subtraction) of the two entered matrices is: \n\n");
    for(c = 0; c < m; c++)
    {
        for(d = 0; d < n; d++)
        {
            printf("%d\t", diff[c][d]);
        }
        printf("\n");
    }

    return 0;
}

Summary

In this lab, you have learned the process of matrix addition and subtraction in C programming language. You have created a program that asks the user to input two matrices of the same size and then performs addition and subtraction operations, displaying the results on the screen.

Other C Tutorials you may like