Tuesday, 27 August 2013

Chapter 8 solutions Problem [L][e]- Part 1

Question-Those readers who are from an Engineering/Science 
background may try writing programs for following problems. 
(1)  Write a program to add two 6 x 6 matrices.
Solution-
I would post the solution for Question [L][e] in the end of this chapter .  

This question is a good one for practising 2-D Array.
Let's concentrate on the solution.

#include <stdio.h>
#include <stdlib.h>
#define m 3
#define n 3
int main()
{
int array1[m][n];
int array2[m][n];
int arr[m][n];
int i,j;
printf("Enter the elements of the 1st Matrix\n");
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
scanf("%d",&array1[i][j]);
printf("\n");
}
printf("Enter the elements of the 2nd Matrix\n");
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
scanf("%d",&array2[i][j]);
printf("\n");
}
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
arr[i][j]=array1[i][j]+array2[i][j]; /*Adding array elements */
}
printf("Result of the addition\n");
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
printf("%d ",arr[i][j]);
printf("\n");
}
getch();

}


No comments:

Post a Comment