-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path280lab2.cpp
More file actions
177 lines (129 loc) · 3.03 KB
/
Copy path280lab2.cpp
File metadata and controls
177 lines (129 loc) · 3.03 KB
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
// REQUIRES: n >=1
// EFFECTS: Prints the hailstone sequence starting at n.
// Uses iteration.
void hailstoneIterative(int n){
// REPLACE WITH YOUR CODE
cout << n << endl;
while (n!=1)
{
if (n%2 == 0)
{
cout << n/2 << endl;
n = n/2;
}
else if (n%2 == 1)
{
cout << 3*n+1 << endl;
n = 3*n+1;
}
}
cout << 1 << endl;
}
// REQUIRES: n >= 1
// EFFECTS: Prints the hailstone sequence starting at n.
// Uses recursion.
void hailstoneTail(int n){
// REPLACE WITH YOUR CODE
if (n == 1)
{
cout << 1 << endl;
//hailstoneTest(n);
return;
}
if (n % 2 ==0)
{
cout << n/2 <<endl;
//hailstoneTest(n/2);
hailstoneTail(n/2);
}
else if (n % 1 == 1)
{
cout << 3*n+1 << endl;
//hailstoneTest(3*n+1);
hailstoneTail(3*n+1);
}
}
// Helper function for testing code
void hailstoneTest(int n){
cout << endl << " Hailstone test for n = " << n << endl;
cout << " Iterative: "; hailstoneIterative(n); cout << endl;
cout << " Recursive: "; hailstoneTail(n); cout << endl;
}
// Testing code for hailstone
int main(){
cout << "Running hailstone tests for lab 2..." << endl;
hailstoneTest(5);
hailstoneTest(8);
hailstoneTest(14);
}
// REQUIRES: 0 <= digit <= 9, n >=0
// EFFECTS: Returns the number of times "digit" appears in "n".
// Uses iteration.
int count_i = 0;
int count_r = 0;
int count_t = 0;
int countDigitsIterative(int n, int digit){
while (!(n<=9))
{
int last_digit = n%10;
if (last_digit == digit)
{
count_i++;
}
n = n/10;
}
if (n == digit)
{
count_i++;
}
return count_i;
}
// REQUIRES: 0 <= digit <= 9, n >= 0
// EFFECTS: Returns the number of times "digit" appears in "n".
// Uses recursion.
int countDigitsRecursive(int n, int digit){
if (n <= 9)
{
if (n == digit) count_r++;
return count_r;
}
int last_digit = n%digit;
if (last_digit == digit)
{
count_r++;
}
countDigitsIterative(n/10, digit);
}
// REQUIRES 0 <= digit <= 9, n >= 0
// EFFECTS: Returns the number of times "digit" appears int "n".
// Uses tail recursion.
int countDigitsTail(int n, int digit){
if (n <= 9)
{
if (n == digit) count_t++;
return count_t;
}
int last_digit = n%digit;
if (last_digit == digit)
{
count_t++;
}
countDigitsIterative(n/10, digit);
}
// Helper function for testing code
void countDigitsTest(int n, int digit){
cout << endl << " CountDigits test. How many " << digit << "s in " << n << endl;
cout << " Iterative: " << countDigitsIterative(n, digit) << endl;
cout << " Recursive: " << countDigitsRecursive(n, digit) << endl;
cout << " Tail Recursive: " << countDigitsTail(n, digit) << endl;
}
// Testing code for countDigits
int main() {
cout << "Running tests for countDigits..." << endl;
countDigitsTest(201220130, 2);
countDigitsTest(201220130, 0);
countDigitsTest(201220130, 7);
countDigitsTest(0, 0);
countDigitsTest(0, 8);
return 0;
}