-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemp.c
More file actions
109 lines (82 loc) · 2.17 KB
/
temp.c
File metadata and controls
109 lines (82 loc) · 2.17 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//place to test out logic for code
/**
* C program to add two matrix using pointers.
*/
#include <stdio.h>
#define ROWS 3
#define COLS 3
/* Function declaration to input, add and print matrix */
void matrixInput(int mat[][COLS]);
void matrixPrint(int mat[][COLS]);
void matrixAdd(int mat1[][COLS], int mat2[][COLS], int res[][COLS]);
int main()
{
int mat1[ROWS][COLS], mat2[ROWS][COLS], res[ROWS][COLS];
// Input elements in first matrix
printf("Enter elements in first matrix of size %dx%d: \n", ROWS, COLS);
matrixInput(mat1);
// Input element in second matrix
printf("\nEnter elements in second matrix of size %dx%d: \n", ROWS, COLS);
matrixInput(mat2);
// Find sum of both matrices and print result
matrixAdd(mat1, mat2, res);
printf("\nSum of first and second matrix: \n");
matrixPrint(res);
return 0;
}
/**
* Function to read input from user and store in matrix.
*
* @mat Two dimensional integer array to store input.
*/
void matrixInput(int mat[][COLS])
{
int i, j;
for (i = 0; i < ROWS; i++)
{
for (j = 0; j < COLS; j++)
{
// (*(mat + i) + j) is equal to &mat[i][j]
scanf("%d", (*(mat + i) + j));
}
}
}
/**
* Function to print elements of matrix on console.
*
* @mat Two dimensional integer array to print.
*/
void matrixPrint(int mat[][COLS])
{
int i, j;
for (i = 0; i < ROWS; i++)
{
for (j = 0; j < COLS; j++)
{
// *(*(mat + i) + j) is equal to mat[i][j]
printf("%d ", *(*(mat + i) + j));
}
printf("\n");
}
}
/**
* Function to add two matrices and store their result in given res
* matrix.
*
* @mat1 First matrix to add.
* @mat2 Second matrix to add.
* @res Resultant matrix to store sum of mat1 and mat2.
*/
void matrixAdd(int mat1[][COLS], int mat2[][COLS], int res[][COLS])
{
int i, j;
// Iterate over each matrix elements
for (i = 0; i < ROWS; i++)
{
for (j = 0; j < COLS; j++)
{
// res[i][j] = mat1[i][j] + mat2[i][j]
*(*(res + i) + j) = *(*(mat1 + i) + j) + *(*(mat2 + i) + j);
}
}
}