-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsequential.c
More file actions
171 lines (141 loc) · 2.93 KB
/
sequential.c
File metadata and controls
171 lines (141 loc) · 2.93 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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include <stdlib.h>
struct Timer
{
struct timeval time_start, time_end;
short int running;
long seconds;
};
void Start(struct Timer *t)
{
(*t).seconds = 0.0;
gettimeofday(&((*t).time_start), NULL);
(*t).running = 1;
}
void Stop(struct Timer *t) //returns time in milli-secs
{
if ((*t).running == 0) { (*t).seconds = 0.0; return; }
gettimeofday(&((*t).time_end), NULL);
long secs, uSecs;
secs = (*t).time_end.tv_sec - (*t).time_start.tv_sec;
uSecs = (*t).time_end.tv_usec - (*t).time_start.tv_usec;
(*t).seconds =((secs) * 1000 + uSecs/1000.0) + 0.5;
(*t).running = 0;
}
float Elapsed(struct Timer *t)
{
if ((*t).running == 1) Stop(t);
return (*t).seconds;
}
int main()
{
int row1,col1,row2,col2,i=0,j=0,k=0;
again:
printf("Enter Dimensions for Matrix A:\n");
printf("Enter Row=");
scanf("%d",&row1);
printf("Enter Column=");
scanf("%d",&col1);
printf("Enter Dimensions for Matrix B:\n");
printf("Enter Row=");
scanf("%d",&row2);
printf("Enter Column=");
scanf("%d",&col2);
if(col1==row2)
{
int **a = (int **)malloc(row1 * sizeof(int *));
for (int i=0; i<row1; i++)
a[i] = (int *)malloc(col1 * sizeof(int));
int **b = (int **)malloc(row2 * sizeof(int *));
for (int i=0; i<row2; i++)
b[i] = (int *)malloc(col2 * sizeof(int));
int **mul = (int **)malloc(row1 * sizeof(int *));
for (int i=0; i<row1; i++)
mul[i] = (int *)malloc(col2 * sizeof(int));
srand(time(0));
for(i=0;i<row1;i++)
{
for(j=0;j<col1;j++)
{
a[i][j]=rand()%100;
}
}
for(i=0;i<row2;i++)
{
for(j=0;j<col2;j++)
{
b[i][j]=rand()%100;
}
}
for(i=0;i<row1;i++)
{
for(j=0;j<col2;j++)
{
mul[i][j]=0;
}
}
//starting the timer
struct Timer timer;
Start(&timer);
//multiply of the matrix;
for(i=0;i<row1;i++)
{
for(j=0;j<col2;j++)
{
for(k=0;k<row2;k++)
{
mul[i][j]+=a[i][k]*b[k][j];
}
}
}
//stop timer and print time in miliseconds
Stop(&timer);
printf("\nTime elapsed = %g ms\n", Elapsed(&timer));
//for printing of matrix
if(row1<10)
{
//Matrix a
printf("Matrix A\n");
for(int i=0;i<row1;i++)
{
for(int j=0;j<col1;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
//Matrix b
printf("Matrix B\n");
for(int i=0;i<row2;i++)
{
for(int j=0;j<col2;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
printf("\n");
//for printing result
printf("Resultant Matrix\n");
for(int i=0;i<row1;i++)
{
for(int j=0;j<col2;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
}
}
else
{
system("clear");
printf("Multiplication not Possible Due to Dimensions\n");
printf("Try Again\n");
goto again;
}
return 0;
}