forked from swayam-agrahari/C-Program
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmatrix_arithmetic.c
More file actions
65 lines (60 loc) · 1.37 KB
/
matrix_arithmetic.c
File metadata and controls
65 lines (60 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdio.h>
// function to add two matrix
void addMatrix(int a[10][10], int b[10][10],
int c[10][10], int row, int column)
{
for(int i=0; i< row; ++i)
{
for(int j=0; j< column; ++j)
{
// add & store to matrix C
c[i][j] = a[i][j] + b[i][j];
}
}
}
// function to read matrix
void readMatrix(int matrix[10][10], int row, int column)
{
for (int i = 0; i < row; ++i)
{
for (int j = 0; j < column; ++j)
{
scanf("%d", &matrix[i][j]);
}
}
}
// function to display matrix
void displayMatrix(int matrix[10][10], int row, int column)
{
for (int i = 0; i < row; ++i)
{
for (int j = 0; j < column; ++j)
{
printf("%d ", matrix[i][j]);
}
printf("\n"); // new line
}
}
// main function
int main()
{
// declare matrix matrix A, B, & C
int a[10][10]; // first matrix
int b[10][10]; // second matrix
int c[10][10]; // resultant matrix
// read the size of matrices
int row, column;
printf("Enter Row and Column Sizes: ");
scanf("%d %d", &row, &column);
// read matrix A and B
printf("Enter Matrix-1 Elements: \n");
readMatrix(a, row, column);
printf("Enter Matrix-2 Elements: \n");
readMatrix(b, row, column);
// add both matrix A and B
addMatrix(a, b, c, row, column);
// display resultant matrix
printf("Resultant Matrix: \n");
displayMatrix(c, row, column);
return 0;
}