BRABU 2nd, 4th and 6th Examination Schedule Out
#include <stdio.h>
#include <conio.h>
#define SIZE 10
void input(int a[][SIZE], int r, int c);
void show(int a[][SIZE], int r, int c);
void add(int a[][SIZE], int b[][SIZE], int c[][SIZE],
int r1, int c1, int r2, int c2);
int main()
{
int a[SIZE][SIZE], b[SIZE][SIZE], c[SIZE][SIZE];
int r1, r2, c1, c2;
printf("\nEnter the row and column of first matrix: ");
scanf("%d%d", &r1, &c1);
if (r1 > SIZE || c1 > SIZE || r1 <= 0 || c1 <= 0)
{
printf("\nInvalid size of first matrix");
return 0;
}
input(a, r1, c1);
printf("\nEnter the row and column of second matrix: ");
scanf("%d%d", &r2, &c2);
if (r2 > SIZE || c2 > SIZE || r2 <= 0 || c2 <= 0)
{
printf("\nInvalid size of second matrix");
return 0;
}
input(b, r2, c2);
printf("\nElements of First Matrix:\n");
show(a, r1, c1);
printf("\nElements of Second Matrix:\n");
show(b, r2, c2);
add(a, b, c, r1, c1, r2, c2);
getch();
return 0;
}
void input(int a[][SIZE], int r, int c)
{
int i, j;
printf("\nEnter the elements of matrix:\n");
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
scanf("%d", &a[i][j]);
}
}
}
void show(int a[][SIZE], int r, int c)
{
int i, j;
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
printf("%5d", a[i][j]);
}
printf("\n");
}
}
void add(int a[][SIZE], int b[][SIZE], int c[][SIZE],
int r1, int c1, int r2, int c2)
{
int i, j;
if (r1 == r2 && c1 == c2)
{
for (i = 0; i < r1; i++)
{
for (j = 0; j < c1; j++)
{
c[i][j] = a[i][j] + b[i][j];
}
}
printf("\nResultant Matrix:\n");
show(c, r1, c1);
}
else
{
printf("\nMatrix Addition is not possible.");
printf("\nBoth matrices must have the same order.");
}
}