-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfaddmultransp.c
157 lines (146 loc) · 4.2 KB
/
faddmultransp.c
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <stdio.h>
void read_matrix(int[][5], int, int);
void sum_matrix(int[][5], int[][5], int, int);
void multi_matrix(int[][5], int[][5], int, int, int);
void transp_matrix(int[][5], int, int);
void display_matrix(int[][5], int, int);
int i, j, k;
int main()
{
int num, A[5][5], B[5][5], m, n, p, q;
do
{
printf("\n1. Matrix addition\n2. Matrix Multiplication\n3. Transpose of a Matrix\n4. Exit\nEnter which operation you want to perform (1,2,3,4) : ");
scanf("%d", &num);
switch (num)
{
case 1:
printf("Enter the no. of rows and columns of Matrix 1:\n");
scanf("%d %d", &m, &n);
printf("Enter the no. of rows and columns of Matrix 2:\n");
scanf("%d %d", &p, &q);
printf("Enter the elements of the Matrix 1 : \n");
read_matrix(A, m, n);
printf("Enter the elements of the Matrix 2 : \n");
read_matrix(B, p, q);
printf("The Matrices are:\n");
printf("Matrix 1:\n");
display_matrix(A, m, n);
printf("\n");
printf("Matrix 2:\n");
display_matrix(B, p, q);
if (m == p && n == q)
{
sum_matrix(A, B, m, n);
}
else
{
printf("The matrices cannot be added\n");
}
break;
case 2:
printf("Enter the no. of rows and columns of Matrix 1:\n");
scanf("%d %d", &m, &n);
printf("Enter the no. of rows and columns of Matrix 2:\n");
scanf("%d %d", &p, &q);
printf("Enter the elements of the Matrix 1 : \n");
read_matrix(A, m, n);
printf("Enter the elements of the Matrix 2 : \n");
read_matrix(B, p, q);
printf("The Matrices are:\n");
printf("Matrix 1:\n");
display_matrix(A, m, n);
printf("\n");
printf("Matrix 2:\n");
display_matrix(B, p, q);
if (n == p)
{
printf("Product matrix is:\n");
multi_matrix(A, B, m, q, p);
}
else
{
printf("Matrix Multiplication is not possible\n");
}
break;
case 3:
printf("Enter the no. of rows and columns of Matrix:\n");
scanf("%d %d", &m, &n);
printf("Enter the elements of the Matrix : \n");
read_matrix(A, m, n);
printf("The Matrix is:\n");
display_matrix(A, m, n);
transp_matrix(A, m, n);
break;
case 4:
break;
default:
printf("Invalid entry!\n");
break;
}
} while (num != 4);
}
void read_matrix(int c[][5], int m1, int n1)
{
for (i = 0; i < m1; i++)
{
for (j = 0; j < n1; j++)
{
scanf("%d", &c[i][j]);
}
}
}
void sum_matrix(int a1[][5], int b1[][5], int m2, int n2)
{
int c[5][5];
for (i = 0; i < m2; i++)
{
for (j = 0; j < n2; j++)
{
c[i][j] = a1[i][j] + b1[i][j];
}
}
printf("Sum Matrix is : \n");
display_matrix(c, m2, n2);
}
void multi_matrix(int a1[][5], int b1[][5], int m2, int q2, int p2)
{
int c[5][5];
for (i = 0; i < m2; i++)
{
for (j = 0; j < q2; j++)
{
c[i][j] = 0;
for (k = 0; k < q2; k++)
{
c[i][j] += a1[i][k] * b1[k][j];
}
}
}
printf("Product Matrix is : \n");
display_matrix(c, m2, q2);
}
void transp_matrix(int c[][5], int m1, int n1)
{
int d[5][5];
for (i = 0; i < m1; i++)
{
for (j = 0; j < n1; j++)
{
d[j][i] = c[i][j];
}
}
printf("Transposed Matrix is: \n");
display_matrix(d, n1, m1);
}
void display_matrix(int c[][5], int m1, int n1)
{
for (i = 0; i < m1; i++)
{
for (j = 0; j < n1; j++)
{
printf("%d ", c[i][j]);
}
printf("\n");
}
}