Header Ads

Header ADS

Program for Addition of Two Matrices

Introduction

Hello friends, In this post, we will learn how to write a simple program in C for addition of two matrices.
This tutorial is for those people who want to learn programming in C and do not necessarily have any previous knowledge of other programming languages. Of course any knowledge of other programming languages or any general computer skill can be useful to better understand this tutorial, although it is not essential.
Here we are calling two standard library functions. Whenever we call the library functions we must write their prototype before making the call.This helps the compiler in checking whether the values being passed and returned are as per the prototype declaration.But since we don’t define the library functions (we merely call them) we may not know the prototypes of library functions. Hence when the library of functions is provided a set of ‘.h’ files is also provided.These files contain the prototypes of library functions. But why multiple files? Because the library functions are divided into different groups and one file is provided for each group. For example, prototypes of all input/output functions are provided in the file ‘stdio.h’, prototypes of all mathematical functions are provided in the file ‘math.h’, etc.
On compilation of the above code the compiler reports all errors due to the mismatch between parameters in function call and their corresponding prototypes declared in the file ‘conio.h’. You can even open this file and look at the prototypes.

Program Briefing

First, we call the standard library function 'stdio.h' because prototypes of all input/output functions are provided in the file ‘stdio.h’. Then we call the standard library function 'conio.h' because On compilation of the above code the compiler reports all errors due to the mismatch between parameters in function call and their corresponding prototypes declared in the file ‘conio.h’. Then comes the program's main body. We declare three two dimensional arrays of 10x10 i.e; a,b and c. Also eight integers viz. i,j,m1,m2,n1,n2,m3,n3. Then we clear the screen. Then we ask for the number of rows of first matrix and store it in m1. Then ask for the number of columns of first matrix and store it in n1. Then We ask for the elements of first matrix and store it in array 'a'. Similarly, we take the values for the other matrix and store them in array 'b'. Then we add the corresponding elements of the matrix and store them in array 'c'. and print array 'c' as the result of addition. and if the number of rows or number of columns of both the matrices are not equal we will print 'Addition is not possible'.

Program

//Program for addition of two matrices :

 #include<stdio.h>
 #include<conio.h>

 void main()
 {
   int a[10][10],b[10][10],c[10][10];
   int i,j,m1,m2,n1,n2,m3,n3;
   clrscr();
   printf("Enter the no. of rows of first matrix\n");
   scanf("%d",&m1);
   printf("Enter the no. of columns of first matrix\n");
   scanf("%d",&n1);
   clrscr();
   printf("Enter the elements of first matrix\n");

   for(i=0;i<m1;i++)
   {
     for(j=0;j<n1;j++)
     {
       scanf("%d",&a[i][j]);
     }
   }

   printf("\n\n\nEnter the no. of rows of second matrix\n");
   scanf("%d",&m2);
   printf("Enter the no. of columns of second matrix\n");
   scanf("%d",&n2);
   clrscr();
   printf("Enter the elements of second matrix\n");

   for(i=0;i<m2;i++)
   {
     for(j=0;j<n2;j++)
     {
       scanf("%d",&b[i][j]);
     }
   }

   clrscr();
   printf("First matrix is : \n\n");
   for(i=0;i<m1;i++)
   {
     for(j=0;j<n1;j++)
     {
       printf("%d\t",a[i][j]);
     }
     printf("\n");
   }

   printf("\n\n\Second matrix is : \n\n");
   for(i=0;i<m2;i++)
   {
     for(j=0;j<n2;j++)
     {
       printf("%d\t",b[i][j]);
     }
     printf("\n");
   }

   if(m1==m2&&n1==n2)
   {
     printf("\n\n\nAddition = \n\n\n");
     m3=m1;
     n3=n1;
     for(i=0;i<m3;i++)
     {
       for(j=0;j<n3;j++)
       {
         c[i][j]=(a[i][j]+b[i][j]);
         printf("%d\t",c[i][j]);
       }
       printf("\n");
     }
   }

   else
   {
     printf("\n\n\n\nAddition is not possible.");
   }

   getch();
 }



No comments

Powered by Blogger.