-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjobSchedule.c
73 lines (65 loc) · 1.62 KB
/
jobSchedule.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
//Job Scheduling problem with deadline
#include <stdio.h>
#include <time.h>
int n, i, j, k, t;
int check(int s[], int p)
{
int ptr = 0;
for (int i = 0; i < n; i++)
{
if (s[i] == p)
ptr++;
}
if (ptr == 0)
return 1;
else
return 0;
}
int main()
{
time_t start, end;
start = time(NULL);
printf("enter the no of jobs : ");
scanf("%d", &n);
int slot[n], profit, p[n], d[n];
for (i = 0; i < n; i++)
{
printf("\n enter the profit of job #%d : ", i + 1);
scanf("%d", &p[i]);
printf("\n enter the deadline of job #%d : ", i + 1);
scanf("%d", &d[i]);
}
for (i = 0; i < n; i++)
for (j = i + 1; j < n; j++)
if (p[i] < p[j])
{
t = p[i];
p[i] = p[j];
p[j] = t;
t = d[i];
d[i] = d[j];
d[j] = t;
}
for (i = 0; i < n; i++)
slot[i] = 0;
for (i = 0; i < n; i++)
for (j = d[i]; j > 0; j--)
{
if (check(slot, j) == 1)
{
slot[i] = j;
break;
}
}
printf("\n\n INDEX PROFIT DEADLINE SLOT ALLOTTED ");
for (i = 0; i < n; i++)
{
if (slot[i] > 0)
printf("\n\n %d %d %d [%d - %d]", i + 1, p[i], d[i], (slot[i] - 1), slot[i]);
else
printf("\n\n %d %d %d REJECTED", i + 1, p[i], d[i]);
}
end = time(NULL);
printf("\n Time taken by program is %.2f seconds", difftime(end, start));
return 0;
}