Matrix Addition
Matrix Addition
#include <stdio.h>
#include<conio.h>
void readMatrix(int matrix[10][10], int rows, int cols)
{
int i,j;
printf("Enter elements of the matrix (%d x %d):\n", rows, cols);
for(i =0;i<rows;i++)
{
for(j =0;j<cols;j++)
{
printf("Element [%d][%d]: ", i, j);
scanf("%d", &matrix[i][j]);
}
}
}
void printMatrix(int matrix[10][10], int rows, int cols)
{
int i,j;
printf("Matrix:\n");
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
}
void addMatrices(int A[10][10], int B[10][10], int result[10][10], int rows, int cols)
{
int i,j;
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
result[i][j] = A[i][j] + B[i][j];
}
}
}
void main()
{
int A[10][10], B[10][10], result[10][10];
int rows, cols;
clrscr();
printf("Enter the number of rows and columns of the matrices: ");
scanf("%d %d", &rows, &cols);
printf("Enter elements of first matrix:\n");
readMatrix(A, rows, cols);
printf("Enter elements of second matrix:\n");
readMatrix(B, rows, cols);
addMatrices(A, B, result, rows, cols);
printf("Resultant Matrix after Addition:\n");
printMatrix(result, rows, cols);
getch();
}