-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrix.c
155 lines (155 loc) · 2.72 KB
/
Matrix.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
#include<stdio.h>
#include<conio.h>
void add();
void sub();
void multiply();
void trans();
void main(){
int choice,i;
char ans='y';
clrscr();
printf("Enter 1 to add\n");
printf("Enter 2 to subtract\n");
printf("Enter 3 to multiply\n");
printf("Enter 4 to transpose\n");
do{
printf("Rember both matrix should be 2*2\n");
printf("Enter your choice:-");
scanf("%d",&choice);
switch(choice){
case 1:
add();
break;
case 2:
sub();
break;
case 3:
multiply();
break;
case 4:
trans();
break;
default:
printf("Enter a valid choice ");
}
printf("Do you want to run again if yes than type y otherwise press enter twice to exit:-");
fflush(stdin);
scanf("%c",&ans);
}while(ans=='y');
getch();
}
void add(){
int a[2][2],b[2][2],c[2][2]={{0,0},{0,0}},j,i;
printf("You have choosen addition\n");
printf("Enter elements of first matrix :-\n");
for(i=0;i<2;i++){
for(j=0;j<2;j++){
scanf("%d",&a[i][j]);
}
}
printf("Enter elements of second matrix:-\n");
for(i=0;i<2;i++){
for(j=0;j<2;j++){
scanf("%d",&b[i][j]);
}
}
for(i=0;i<2;i++){
for(j=0;j<2;j++){
c[i][j]=a[i][j]+b[i][j];
}
}
printf("The resultant matrix is:-\n");
for(i=0;i<2;i++){
for(j=0;j<2;j++){
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
void sub(){
int a[2][2],b[2][2],c[2][2]={{0,0},{0,0}},j,i;
printf("You have choosen subtract \n");
printf("Enter elements of first matrix :-\n");
for(i=0;i<2;i++){
for(j=0;j<2;j++){
scanf("%d",&a[i][j]);
}
}
printf("Enter elements of second matrix:-\n");
for(i=0;i<2;i++){
for(j=0;j<2;j++){
scanf("%d",&b[i][j]);
}
}
printf("The resultant matrix is:-\n");
for(i=0;i<2;i++){
for(j=0;j<2;j++){
c[i][j]=a[i][j]-b[i][j];
}
}
for(i=0;i<2;i++){
for(j=0;j<2;j++){
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
void trans(){
int a[2][2],c[2][2]={{0,0},{0,0}},j,i;
printf("You have choosen transpose");
printf("Enter elements of matrix to be transposed:-\n");
for(i=0;i<2;i++){
for(j=0;j<2;j++){
scanf("%d",&a[i][j]);
}
}
for(i=0;i<2;i++){
for(j=0;j<2;j++){
c[i][j]=a[j][i];
}
}
printf("The given matrix is :-\n");
for(i=0;i<2;i++){
for(j=0;j<2;j++){
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("The transpose is:-\n");
for(i=0;i<2;i++){
for(j=0;j<2;j++){
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
void multiply(){
int a[2][2],b[2][2],c[2][2]={{0,0},{0,0}},j,i,k;
printf("You have choosen multiplication\n");
printf("Enter elements of first matrix :-\n");
for(i=0;i<2;i++){
for(j=0;j<2;j++){
scanf("%d",&a[i][j]);
}
}
printf("Enter elements of second matrix:-\n");
for(i=0;i<2;i++){
for(j=0;j<2;j++){
scanf("%d",&b[i][j]);
}
}
for(i=0;i<2;i++){
for(j=0;j<2;j++){
for(k=0;k<2;k++){
c[i][j]+=a[i][k]*b[k][j];
}
}
}
printf("The multiplication of given matrices is:-\n");
for(i=0;i<2;i++){
for(j=0;j<2;j++){
printf("%d\t",c[i][j]);
}
printf("\n");
}
}