-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinaryCounter.cpp
77 lines (70 loc) · 1.41 KB
/
binaryCounter.cpp
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
#include <vector>
#include <iostream>
using namespace std;
class BinaryCounter
{
int kBits;
vector<int> ctr;
public:
float cost;
BinaryCounter()
{
kBits = 8;
cost = 0;
ctr.resize(kBits);
cout << "Initial binary no.: ";
for (auto el : ctr)
cout << el;
}
BinaryCounter(int k)
{
kBits = k;
cost = 0;
ctr.resize(kBits);
cout << "Initial binary no.: ";
for (auto el : ctr)
cout << el;
}
void increment();
};
void BinaryCounter::increment()
{
int i;
i = ctr.size() - 1;
while (ctr[i] != 0)
{
ctr[i--] = 0;
cost++;
}
if (i > -1)
{
ctr[i] = 1;
cost++;
}
// cout << "cost: " << cost << endl;
for (auto el : ctr)
{
cout << el;
}
}
int main()
{
BinaryCounter b;
int i{1};
char decision{'Y'};
cout << endl;
float n{0}, acc{0};
while (decision == 'Y' || decision == 'y')
{
cout << "Increment no. " << i << "\nBinary no.: ";
b.increment();
n++;
i++;
cout << "\nContinue increment? (Y/any character): ";
cin >> decision;
}
cout << endl;
cout << "No. of operations: " << n << endl;
cout << "Amortized Complexity: " << b.cost / n << endl;
return 0;
}