-
Notifications
You must be signed in to change notification settings - Fork 6
/
array_using_pointers.c
155 lines (152 loc) · 2.98 KB
/
array_using_pointers.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<malloc.h>
int row,col;
void create_first(int *,int,int);
void create_second(int *,int,int);
void addition(int *,int *,int *,int,int);
void subtraction(int *,int *,int *,int,int);
void transpose_first(int *,int *,int,int);
void transpose_second(int *,int *,int,int);
int main()
{
int ch;
printf("enter number of rows:\n");
scanf("%d",&row);
printf("enter number of coloumns:\n");
scanf("%d",&col);
int *a=(int *)malloc(row*col*sizeof(int));
int *b=(int *)malloc(row*col*sizeof(int));
int *c=(int *)malloc(row*col*sizeof(int));
int *d=(int *)malloc(row*col*sizeof(int));
int *ta=(int *)malloc(row*col*sizeof(int));
int *tb=(int *)malloc(row*col*sizeof(int));
printf("enter number of rows:\n");
scanf("%d",&row);
printf("enter number of coloumns:\n");
scanf("%d",&col);
create_first(a,row,col);
create_second(b,row,col);
while(1)
{
printf("enter your choice:\n1.additon\n2.subtrcation\n3.transpose first matrix\n4.transpose 2nd matrix:\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
addition(a,b,c,row,col);
break;
case 2:
subtraction(a,b,d,row,col);
break;
case 3:
transpose_first(a,ta,row,col);
break;
case 4:
transpose_second(b,tb,row,col);
break;
}
}
return 0;
}
void create_first(int *a,int row,int col)
{
int i,j,n;
printf("enter elements of first matrix:\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
scanf("%d",&n);
*(a+i*row+j)=n;
}
}
}
void create_second(int *b,int row,int col)
{
int i,j,num;
printf("enter elements of second matrix:\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
scanf("%d",&num);
*(b+i*row+j)=num;
}
}
}
void addition(int *a,int *b,int *c,int row,int col)
{
int i,j;
printf("addition of both matrices:\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
*(c+i*row+j)=*(a+i*row+j)+*(b+i*row+j);
}
}
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("element c[%d][%d]-%d\n",i,j,*(c+i*col+j));
}
}
}
void subtraction(int *a,int *b,int *d,int row,int col)
{
int i,j;
printf("subtraction:\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
*(d+i*col+j)=*(a+i*col+j)-*(b+i*col+j);
}
}
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("element d[%d][%d]-%d\n",i,j,*(d+i*col+j));
}
}
}
void transpose_first(int *a,int *ta,int row,int col)
{
int i,j;
for(i=0;i<col;i++)
{
for(j=0;j<row;j++)
{
*(ta+i*row+j)=*(a+j*row+i);
}
}
printf("Transpose of first matrix is:\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("element ta[%d][%d]-%d\n",i,j,*(ta+i*col+j));
}
}
}
void transpose_second(int *b,int *tb,int row,int col)
{
int i,j;
for(i=0;i<col;i++)
{
for(j=0;j<row;j++)
{
*(tb+i*row+j)=*(b+j*col+i);
}
}
printf("Transpose of second matrix:\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("element tb[%d][%d]-%d\n",i,j,*(tb+i*col+j));
}
}
}