C Program for performing matrix addition using structure
// This code is written by Souvik Ghosh
#include <stdio.h>
typedef struct
{
int M[10][10];
int row, column;
} matrix;
void input(matrix *p)
{
int row, column;
printf("Enter the number of rows\n");
scanf("%d", &row);
printf("Enter the number of column\n");
scanf("%d", &column);
printf("Enter Elements\n");
p->row = row;
p->column = column;
for (int i = 0; i < p->row; i++)
{
for (int j = 0; j < p->column; j++)
{
scanf("%d", &p->M[i][j]);
}
}
}
void output(matrix m)
{
for (int i = 0; i < m.row; i++)
{
printf("\n");
for (int j = 0; j < m.column; j++)
{
printf("%d ", m.M[i][j]);
}
}
}
void addition(matrix *m1, matrix *m2)
{
for (int i = 0; i < m1->row; i++)
{
for (int j = 0; j < m2->column; j++)
{
m1->M[i][j] += m2->M[i][j];
}
}
}
void main()
{
matrix m1, m2;
printf("Enter First Matrix\n");
input(&m1);
printf("Enter Second Matrix\n");
input(&m2);
if (m1.row != m2.row || m1.column != m2.column)
{
printf("Matrix Adition Not Possible\n");
}
else
{
addition(&m1, &m2);
output(m1);
}
}