-
Notifications
You must be signed in to change notification settings - Fork 0
/
Project-1-14.c
76 lines (61 loc) · 1.94 KB
/
Project-1-14.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
// bits and pieces file, latest full before this is v10 from me and overall on the github
#include <stdio.h>
#include <stdlib.h>
struct event {
int eventtime;
int countchange;
} eventlog[8]; // size not known in actual program, add more later with malloc?
// sorts the eventlog struct array
int cmpevents(const void *a, const void *b)
{
const struct event *pa = a;
const struct event *pb = b;
if (pa->eventtime != pb->eventtime) {
return (pa->eventtime > pb->eventtime) - (pa->eventtime < pb->eventtime);
}
else {
return (pa->countchange > pb->countchange) - (pa->countchange < pb->countchange);
}
}
// takes an array of starttimes and endtimes, and simulates the month's time
// returns the maximum no. of cmds running at the same time
int simMonth(struct event *eventlog, int length)
{
int count = 0;
int maxcount = 0;
for (int i = 0; i < length; i++) {
count += eventlog[i].countchange;
if (count > maxcount) {
maxcount = count;
}
if (maxcount == 20) {
return 20;
}
}
return maxcount;
}
void placehfunc()
{
eventlog[0] = (struct event) {120332, 1};
eventlog[1] = (struct event) {124300, -1};
eventlog[2] = (struct event) {45099, 1};
eventlog[3] = (struct event) {120332, -1};
eventlog[4] = (struct event) {120332, 1};
eventlog[5] = (struct event) {171230, -1};
eventlog[6] = (struct event) {41230, 1};
eventlog[7] = (struct event) {150000, -1};
// test eventlog
// in actual program size will have to be grown before each new storing?
}
int main(int argc, char *argv[])
{
placehfunc(); // placeholder test function
int numEvents = sizeof(eventlog) / sizeof(*eventlog);
qsort(eventlog, numEvents, sizeof *eventlog, cmpevents);
for (int n = 0; n < numEvents; n++) {
printf("%d, %d\n", eventlog[n].eventtime, eventlog[n].countchange);
} // testing block
int maxSimEvents = simMonth(eventlog, numEvents);
printf("%d\n", maxSimEvents);
return EXIT_SUCCESS;
}