Q:

Write a C program to Addition of two Matrices

0

Write a C program to Addition of two Matrices. Here’s simple Program to Add two Matrices in C Programming Language.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

What is Matrix ?


Matrix representation is a method used by a computer language to store matrices of more than one dimension in memory. C uses “Row Major”, which stores all the elements for a given row contiguously in memory.

Two-dimensional Arrays : :

The simplest form of multidimensional array is the two-dimensional array. A two-dimensional array is, in essence, a list of one-dimensional arrays. An m × n (read as m by n) order matrix is a set of numbers arranged in m rows and n columns.

To declare a two-dimensional integer array of size [x][y], you would write something as follows −

  • type arrayName [ x ][ y ];

Where type can be any valid C data type and arrayName will be a valid C identifier.


Below is the source code for C program to Addition of two Matrices which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :

/*  C Program for Addition of two matrices  */

#include <stdio.h>

int main()
{
    int A[10][10],B[10][10],C[10][10],i,j,m,n;
    int row, col;

    printf("Enter no. of rows :: ");
        scanf("%d", &m);
        printf("\nEnter no. of cols :: ");
        scanf("%d",&n);

        printf("\nEnter values to the matrix A :: \n");
        for (i = 0; i < m; i++)
        {
            for (j = 0; j < n; j++)
            {
                 printf("\nEnter a[%d][%d] value :: ",i,j);
                 scanf("%d", &A[i][j]);
        }
    }

    printf("\nThe given matrix A is :: \n\n");

        for (i = 0; i < m; ++i)
        {
            for (j = 0; j < n; ++j)
            {
                printf("\t%d", A[i][j]);
            }
            printf("\n\n");
        }

    printf("\nEnter values to the matrix B :: \n");
        for (i = 0; i < m; i++)
        {
            for (j = 0; j < n; j++)
            {
                 printf("\nEnter a[%d][%d] value :: ",i,j);
                 scanf("%d", &B[i][j]);
        }
    }

    printf("\nThe given matrix B is :: \n\n");

        for (i = 0; i < m; ++i)
        {
            for (j = 0; j < n; ++j)
            {
                printf("\t%d", B[i][j]);
            }
            printf("\n\n");
        }

    /*
     * Adds both matrices A and B entry wise or element wise
     * And stores result in matrix C
     */
    for(row=0; row<m; row++)
    {
        for(col=0; col<n; col++)
        {
            C[row][col] = A[row][col] + B[row][col];
        }
    }

    /*
     * Prints the Addition of two matrices A and B
     */
    printf("\nAddition of matrices A + B = C\n");
    printf("\nThe given matrix C is :: \n\n");

        for (i = 0; i < m; ++i)
        {
            for (j = 0; j < n; ++j)
            {
                printf("\t%d", C[i][j]);
            }
            printf("\n\n");
        }

    return 0;
}

Output : :

/*  C Program for Addition of two matrices  */

Enter no. of rows :: 2

Enter no. of cols :: 2

Enter values to the matrix A ::

Enter a[0][0] value :: 1

Enter a[0][1] value :: 2

Enter a[1][0] value :: 3

Enter a[1][1] value :: 4

The given matrix A is ::

        1       2

        3       4


Enter values to the matrix B ::

Enter a[0][0] value :: 5

Enter a[0][1] value :: 6

Enter a[1][0] value :: 7

Enter a[1][1] value :: 8

The given matrix B is ::

        5       6

        7       8


Addition of matrices A + B = C

The given matrix C is ::

        6       8

        10      12


Process returned 0

Above is the source code for C program to Addition of two Matrices which is successfully compiled and run on Windows System.The Output of the program is shown above .

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C Matrix Solved Programs – C Programming

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a C Program for Subtraction of two matrices... >>