-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram2
More file actions
83 lines (78 loc) · 1.69 KB
/
program2
File metadata and controls
83 lines (78 loc) · 1.69 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
#include<stdio.h>
#include<stdlib.h>
void main()
{
int i,j,k,m,n,p,q,sum,choice;
int *A[3],*B[3],*C[3];
printf("enter the order of matrix 1\n");
scanf("%d%d",&m,&n);
printf("enter the order of matrix 2\n");
scanf("%d %d",&p,&q);
for(i=0;i<m;i++)
A[i]=(int*)malloc(n*sizeof(int));
for(i=0;i<p;i++)
B[i]=(int*)malloc(q*sizeof(int));
printf("enter elements of matrix 1\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",A[i]+j);
printf("enter elements of matrix 2\n");
for(i=0;i<p;i++)
for(j=0;j<q;j++)
scanf("%d",B[i]+j);
while(1)
{
printf("\n 1->Addition of matrix 2->Multiplication of matrix 3->Exit");
scanf("%d",&choice);
switch(choice)
{
case 1:
if(m!=p || n!=q)
printf("Matrix order mismatch - Addition not possible\n");
else{
for(i=0;i<m;i++)
C[i]=(int*)malloc(n*sizeof(int));
for(i=0;i<m;i++)
for(j=0;j<n;j++)
*(C[i]+j)= *(A[i]+j)+ *(B[i]+j);
printf("\n Result of matrix addition:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("\t%d",*C[i]+j);
}
printf("\n");
}
}
break;
case 2:
if(n!=p)
printf("\nmatrices cannot be multiplied\n");
else{
for(i=0;i<p;i++)
{
C[i]=(int*)malloc(q*sizeof(int));
}
for(i=0;i<m;i++)
for(j=0;j<p;j++)
{
*(C[i]+j)=0;
for(k=0;k<n;k++)
*(C[i]+j)= *(C[i]+j)+ *(A[i]+k)* *(B[k]+j);
}
printf("\n Result of matrix Multiplicationn:\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf("\t%d",*C[i]+j);
}
printf("\n");
}
}
break;
case 3: exit(1);
}
}
}