-
Notifications
You must be signed in to change notification settings - Fork 0
/
binarycounter.c
111 lines (104 loc) · 2.88 KB
/
binarycounter.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
#include <stdio.h>
#include <math.h>
void aggregateMethod(float n) {
float tot = 0;
int arr[4] = {0};
printf("Counter:0, Cost:0 \n");
for (int j = 0; j < n; j++) {
int i = 0;
while (arr[i] == 1 && i < 4) {
arr[i] = 0;
tot++;
i++;
}
if (i < 4) {
tot++;
arr[i] = 1;
}
printf("Counter:%d, Cost:%f \n", j + 1, tot);
}
float avgcost = tot / n;
printf("Avg Cost:%f \n", avgcost);
}
void accountingMethod() {
int size[16];
printf("\n");
int tot[10];
int credit[11] = {0};
int arr[4] = {0};
for (int j = 0; j < 10; j++) {
int total = 0;
int i = 0;
while (arr[i] == 1 && i < 4) {
arr[i] = 0;
total++;
i++;
}
if (i < 4) {
total++;
arr[i] = 1;
}
credit[j + 1] = 2 - total + credit[j];
printf("Counter:%d, ", j + 1);
printf("Cost:%d \n", credit[j]);
}
}
void potentialMethod(float n) {
float tot = 0;
int arr[4] = {0};
printf("Counter:0, Cost:0\n");
float prev_potential = 0;
float prevones = 0;
for (int j = 0; j < n; j++) {
float currentones = 0;
float potential = 0;
int i = 0;
while (arr[i] == 1 && i < 4) {
arr[i] = 0;
tot++;
i++;
}
if (i < 4) {
tot++;
arr[i] = 1;
}
for (int k = 0; k < 4; k++)
if (arr[k] == 1)
currentones++; //Calculates the number of ones in the counter array.
float current_cost = tot;
potential = current_cost + potential - prev_potential;
prev_potential = current_cost;
float amort = potential + (currentones - prevones);
prevones = currentones;
printf("Counter:%d, Cost:%f, Current Cost:%f, amort Cost:%f\n", j + 1, current_cost, potential, amort);
}
float avg_cost = tot / n;
printf("Avg Cost:%f\n", avg_cost);
}
int main() {
int choice;
printf("Choose a method (1: Aggregate, 2: Accounting, 3: Potential): ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value between 0 to %f\n", pow(2, 4));
float n_aggregate;
scanf("%f", &n_aggregate);
aggregateMethod(n_aggregate);
break;
case 2:
accountingMethod();
break;
case 3:
printf("Enter value between 0 to %f\n", pow(2, 4));
float n_potential;
scanf("%f", &n_potential);
potentialMethod(n_potential);
break;
default:
printf("Invalid choice.\n");
}
return 0;
}
// Enter value between 0 to 16.000000
// 15