Home

A program to add two dimensional matrices using functions

C Programming Language 081

Program

#include <stdio.h>

void readmat(int x[10][10], int m, int n);
void printmat(int x[10][10], int m, int n);
void addmat(int x[10][10], int y[10][10], int z[10][10], int m, int n);
void main()
{
    int a[10][10], b[10][10], c[10][10], m, n, i, j;
    printf("Enter order m,n [m x n]: ");
    scanf("%d %d", &m, &n);

    printf("Enter values for 1st matrix [%d x %d]:\n", m, n);
    readmat(a, m, n);

    printf("Enter values for 2nd matrix [%d x %d]:\n", m, n);
    readmat(b, m, n);

    addmat(a, b, c, m, n);

    printf("After addition...\n");
    printmat(c, m, n);

    printf("\n");
}

void readmat(int x[10][10], int m, int n)
{
    int i, j;
    for (i = 0; i < m; i++)
    {
        for (j = 0; j < n; j++)
        {
            scanf("%d", &x[i][j]);
        }
    }
}

void printmat(int x[10][10], int m, int n)
{
    int i, j;
    for (i = 0; i < m; i++)
    {
        for (j = 0; j < n; j++)
        {
            printf("%d\t", x[i][j]);
        }
        printf("\n");
    }
}

void addmat(int x[10][10], int y[10][10], int z[10][10], int m, int n)
{
    int i, j;
    for (i = 0; i < m; i++)
    {
        for (j = 0; j < n; j++)
        {
            z[i][j] = x[i][j] + y[i][j];
        }
    }
}

Result

Enter order m,n [m x n]: 1 2
Enter values for 1st matrix [1 x 2]:
1 2
Enter values for 2nd matrix [1 x 2]:
3 4
After addition...
4       6


Last Updated on

Next Post: Rules vs Guidelines →

Comments