-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDAA.c
331 lines (289 loc) · 8.83 KB
/
DAA.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
DAA Assignment (Year 02)
IT 14042720
Buwaneka Boralessa
Batch 02
*/
#include <stdio.h>
#include <stdbool.h>
// <stdbool.h> is used for declare boolean variables
void declare_of_regions(int *i);
void declare_of_programs(int *i);
void sort_array(int *arr,int n);
int main()
{
int num_regions=0;//holds number of regions
int num_pro=0;//holds number of programs
declare_of_regions(&num_regions);
declare_of_programs(&num_pro);
int main_mem_arr[4][(num_pro*10)];//use to store mem size with their mem region no
int mem_buffer[4][num_regions];//use to store regions data used in main coding
int output_arr[4][num_pro];//final output data store here
int turnaround_time=0;
int max_region_size=0;
//*********** Take Region Sizes ************
int arr_size[num_regions];// declaration of all regions
int ss;
int y=1;
while(y<=num_regions)
{
printf("Enter Region %d Size = ",y);
scanf("%d",&ss);
if(ss<=0)
{
printf("Enter Valid Size\n");
}
else
{
arr_size[(y-1)]=ss;
if(max_region_size < ss)
{
max_region_size = ss;
}
y++;
}
}
//***************** End *****************
//********** Take Programs Info ***********
int arr_data[num_pro][20];//program data store here
int p=1;
while(p<=num_pro)
{
int pairs_c=0;
printf("Program %d :-",p);
printf("No Pairs = ");
scanf("%d",&pairs_c);
if(pairs_c > 0)
{
arr_data[(p-1)][0] = pairs_c;
int u=1;
int z,mem_s,mem_t;
for(z=1;z<=pairs_c;)
{
scanf("%d %d",&mem_s,&mem_t);
if(mem_s>max_region_size)
{
printf("Invalid Memory Size(Max = %d)",max_region_size);
}
else
{
arr_data[(p-1)][u]=mem_s;
arr_data[(p-1)][(u+1)]=mem_t;
z++;
u=u+2;
}
}
p++;
}
else
{
printf("Enter Valid Pair");
}
}
//***************** End *****************
printf("\n************ | DATA | ***************\n");
printf("%d %d\n",num_regions,num_pro);
int l,xx;
for(l=0;l<num_regions;l++)
{
mem_buffer[0][l] = 0;//set in-use
mem_buffer[1][l] = l;//store region no
mem_buffer[2][l] = arr_size[l];//store region size
mem_buffer[3][l] = 0;//store used time
printf("%d ",arr_size[l]);
}
printf("\n");
p=0;//main_mem x-index count
for(l=0;l<num_pro;l++)
{
output_arr[0][l]=-1;//initialize output arr program no
int pp=arr_data[l][0];
printf("%d ",pp);
for(xx=1;xx<(pp*2);)
{
main_mem_arr[0][p] = 0;//set in-use
main_mem_arr[1][p] = l;//store program no
main_mem_arr[2][p] = arr_data[l][xx];//store program size
main_mem_arr[3][p] = arr_data[l][(xx+1)];//store program time
printf("%d %d ",arr_data[l][xx],arr_data[l][(xx+1)]);
xx=xx+2;
p++;
}
printf("\n");
}
printf("************ | DATA | ***************\n\n");
//----[-- Main Coding --]-------------------------
for(y=1;y<p;y++)//Sort Memory Regions
{
int i=0;
while(main_mem_arr[3][y] > main_mem_arr[3][i])
{
i++;
}
int key_a=main_mem_arr[0][y];//store key availability
int key_r=main_mem_arr[1][y];//store key Program/Region no
int key_s=main_mem_arr[2][y];//store key size
int key_t=main_mem_arr[3][y];//store key time
int z;
for(z=0;z<=(y-i);z++)
{
main_mem_arr[0][(y-z)] = main_mem_arr[0][(y-z-1)];//exchange region av
main_mem_arr[1][(y-z)] = main_mem_arr[1][(y-z-1)];//exchange Program/Region no
main_mem_arr[2][(y-z)] = main_mem_arr[2][(y-z-1)];//exchange size
main_mem_arr[3][(y-z)] = main_mem_arr[3][(y-z-1)];//exchange time
}
main_mem_arr[0][i] = key_a;//exchange key av
main_mem_arr[1][i] = key_r;//exchange key Program/Region no
main_mem_arr[2][i] = key_s;//exchange key size
main_mem_arr[3][i] = key_t;//exchange key time
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
int pro_count=0;
while(pro_count < num_pro)
{
int y1;
for(y1=0;y1<p;y1++)//loop through main_mem_arr
{
int y5;
if(y1%num_regions == 0)
{
if(y1 != 0)//skip the initial loop
sort_array((int *)mem_buffer,num_regions);
for(y5=0;y5<num_regions;y5++)
{
mem_buffer[0][y5] = 0;
}
}
if(main_mem_arr[0][y1] == 0)//check used or not
{
int y2;
bool skip=false;
for(y2=0;y2<num_pro;y2++)//use to check already used or not
{
if(output_arr[0][y2] == main_mem_arr[1][y1])//check current main mem arr program used or not
{
skip=true;
break;
}
}
if(skip == true)
continue;
else
{
int y3,diff=0,mem_no;
bool found=false;
for(y3=0;y3<num_regions;y3++)//loop through mem_buffer
{
if((mem_buffer[0][y3] == 0) && (mem_buffer[2][y3] >= main_mem_arr[2][y1]))//check memory region not in used and memory size is sufficient
{
if(found == false)
{
diff = mem_buffer[2][y3] - main_mem_arr[2][y1];
mem_no = y3;//pass mem_duffer position
found=true;
}
else if(diff >= (mem_buffer[2][y3] - main_mem_arr[2][y1]))//check for the closest difference
{
diff = mem_buffer[2][y3] - main_mem_arr[2][y1];
mem_no = y3;//pass mem_duffer position
}
}
}
main_mem_arr[0][y1] = 1;//set main mem arr as used (1 = used, 0 = not used)
mem_buffer[0][mem_no] = 1;//set mem buffer as used (1 = used, 0 = not used)
output_arr[0][pro_count] = main_mem_arr[1][y1];//pass program no
output_arr[1][pro_count] = mem_buffer[1][mem_no];//pass region no
output_arr[2][pro_count] = mem_buffer[3][mem_no];//pass start time
output_arr[3][pro_count] = main_mem_arr[3][y1];//pass end time
pro_count++;//update program count
turnaround_time+=(mem_buffer[3][mem_no] + main_mem_arr[3][y1]);//update turnaround time
mem_buffer[3][mem_no] += main_mem_arr[3][y1];//update time
}
}
}
}
//----[-- Main Coding End --]-------------------------
for(y=1;y<num_pro;y++)//Sort Output_array according to program number
{
int i=0;
while(output_arr[0][y] > output_arr[0][i])
{
i++;
}
int key_a=output_arr[0][y];//store key program no
int key_r=output_arr[1][y];//store key Region no
int key_s=output_arr[2][y];//store key start
int key_t=output_arr[3][y];//store key end
int z;
for(z=0;z<=(y-i);z++)
{
output_arr[0][(y-z)] = output_arr[0][(y-z-1)];//exchange program no
output_arr[1][(y-z)] = output_arr[1][(y-z-1)];//exchange Region no
output_arr[2][(y-z)] = output_arr[2][(y-z-1)];//exchange start
output_arr[3][(y-z)] = output_arr[3][(y-z-1)];//exchange end
}
output_arr[0][i] = key_a;//exchange key program no
output_arr[1][i] = key_r;//exchange key Region no
output_arr[2][i] = key_s;//exchange key start
output_arr[3][i] = key_t;//exchange key end
}
double t_t = (double)turnaround_time/num_pro;
printf("Average Turnaround Time => %.2f\n\n",t_t);
for(y=0;y<num_pro;y++)
{
printf("Program %d runs in Region %d from %d to %d \n",(output_arr[0][y]+1),(output_arr[1][y]+1),output_arr[2][y],(output_arr[2][y] + output_arr[3][y]));
}
//--------------------------------------------------
printf("\n");
return 0;
}
void declare_of_regions(int *i)
{
printf("Enter Number of Regions = ");
scanf("%d",&*i);
if(*i <= 0 || *i > 10)
{
printf("Invalid No of Regions (min = 1,max = 10)\n");
declare_of_regions(&*i);
}
}
void declare_of_programs(int *i)
{
printf("Enter Number of Programs = ");
scanf("%d",&*i);
if(*i <= 0 || *i > 50)
{
printf("Invalid No of Programs (min = 1,max = 50)\n");
declare_of_programs(&*i);
}
}
//+++++++++++++++++++ [ INSERTION SORT ] ++++++++++++++++++++++++++++++++
void sort_array(int *arr,int n)
{
int y;
for(y=1;y<n;y++)//Sort Memory Regions
{
int i=0;
while(*((arr+3*n) + y) > *((arr+3*n) + i))
{
i++;
}
int key_a=*((arr+0*n) + y);//store key availability
int key_r=*((arr+1*n) + y);//store key Program/Region no
int key_s=*((arr+2*n) + y);//store key size
int key_t=*((arr+3*n) + y);//store key time
int z;
for(z=0;z<=(y-i);z++)
{
*((arr+0*n) + (y-z)) = *((arr+0*n) + (y-z-1));//exchange region av
*((arr+1*n) + (y-z)) = *((arr+1*n) + (y-z-1));//exchange Program/Region no
*((arr+2*n) + (y-z)) = *((arr+2*n) + (y-z-1));//exchange size
*((arr+3*n) + (y-z)) = *((arr+3*n) + (y-z-1));//exchange time
}
*((arr+0*n) + i) = key_a;//exchange key av
*((arr+1*n) + i) = key_r;//exchange key Program/Region no
*((arr+2*n) + i) = key_s;//exchange key size
*((arr+3*n) + i) = key_t;//exchange key time
}
}
//+++++++++++++++++++ [ INSERTION SORT END ] ++++++++++++++++++++++++++++