Matrix:
"A rectangular array of numbers , symbols or expressions in rows and columns is known as matrix"
Addition of Matrices:
Addition of matrices is done by adding the corresponding Entries together. Let say we have two matrix A and B , the addition of these two matrix is done in this way.
Matrix Addition
Using C Language:
Matrix can be defined in two ways. First is you can predefined matrix in program code. Second you ask the user to enter elements of matrix.I am going for the second option. So let begins with the start of program code given at the end of this article.
After including library files like stdio.h and main function and return statement which is the basic part of every c program, you have to define three arrays for matrices.One for matrix A , second for matrix B and third for the output of addition of these two matrices , the matrix C = matrix A + matrix B. It is better to initialize them to zero in order to avoid garbage values. Otherwise if you don't defined then its OK.
Note: "printf statement is used to display something in the output and scanf statement is used to get something from the user"
How to get values for matrices from User :
In my program, i am dealing with 2*2 matrix addition, therefore i define arrays like a[2][2],b[2][2],c[2][2]. So lets start taking values for matrices a and b.
There are two ways to enter elements in the matrix , First way is you filled first column and then for second column. Second way is filled first row and then second row.I am going for the Second way.
Now i used nested for loops.Outer loop and the inner loop so two for loops in nested way.Outer loop is for the rows and inner loop is for columns. How many times these two loops runs or how many cycles are required for these two loops, as there are 2*2 matrices means there are two rows and two columns. so outer loop and inner loop must runs for two times.Now look at the code below. you can now easily understand this.
for(i=0; i<2;i++)
{
for(j=0;j<2;j++)
{
printf("\t");
scanf("%d",&a[i][j]);
}
}
Note: printf("\t"); ==> (it just to have some spaces before elements otherwise it is not necessary)
Code is same to get the values for Matrix A and Matrix B , but there is b[i][j] instead of a[i][j] for matrix B.
How to Display Matrix Elements in Matrix form for matrix A and matrix B:
Procedure is same, tow loops , outer for loop is for rows and inner loop is for columns. but now you are showing or displaying something so printf statement is used inside the inner loop.
for(i=0; i<2;i++)
{
for(j=0;j<2;j++)
{
printf("\t\t %d", a[i][j]);
}
printf("\n");
}
Note: printf("\n"); ==> this is to go to next row(line) after printing first row elements.
b[i][j] is used instead of a[i][j] for matrix B.
b[i][j] is used instead of a[i][j] for matrix B.
How to write code for Addition of Matrices A and B:
As you can see that , in the getting or displaying elements of matrix , it getting or displaying elements one by one , so all you need is to write this statement in the inner loop
c[i][j] = a[i][j] + b[i][j]
for(i=0; i<2;i++)
{
for(j=0;j<2;j++)
{
c[i][j]=a[i][j] + b[i][j];
}
}
if you don't understand this then simply write in paper or word document the values of i and j for each cycle then you can under stand which element is going to add.
e.g when i = 0(first row) and j = 0(first column)
it means c[0][0] = a[0][0] + b[ 0 ][0 ];
it will add element placed in the first row and first column of matrices A and B and store in the same position first row and first column of matrix C.
How to Display matrix C:
Procedure is same as to display matrix A and matrix B.
printf("%d",c[i][j] ); is used in the inner loop.
Note: \t is for having some spaces and \n is for go to next line.
Program code:
#include<stdio.h>
int main()
{
int a[2][2] , b[2][2], c[2][2];
int i , j;
printf("*****************************************************\n");
printf("\tAddition of Matrix Program\n");
printf("*****************************************************\n");
//code for getting elements from the user
printf("Enter 2*2 matrix A :\n\n");
for(i=0; i<2;i++)
{
for(j=0;j<2;j++)
{
printf("\t");
scanf("%d",&a[i][j]);
}
}
printf("\nEnter 2*2 matrix B\n\n");
for(i=0; i<2;i++)
{
for(j=0;j<2;j++)
{
printf("\t");
scanf("%d",&b[i][j]);
}
}
//code for displaying elements in matrix form
printf("\nMatrix A is:\n");
for(i=0; i<2;i++)
{
for(j=0;j<2;j++)
{
printf("\t\t %d", a[i][j]);
}
printf("\n");
}
printf("Matrix B is:\n");
for(i=0; i<2;i++)
{
for(j=0;j<2;j++)
{
printf("\t\t %d", b[i][j]);
}
printf("\n");
}
//code for addition of matrix
for(i=0; i<2;i++)
{
for(j=0;j<2;j++)
{
c[i][j]=a[i][j] + b[i][j];
}
}
//code for diplaying result after addtion in matrix form.
printf("\nMatrix C is :\n\n");
for(i = 0; i<2;i++)
{
for(j=0;j<2;j++)
{
printf("\t\t %d",c[i][j]);
}
printf("\n");
}
printf("******************************************************");
return 0;
}
Output: After compile and run , the output will be.
Output
No comments:
Post a Comment