-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecursionPractice
More file actions
192 lines (160 loc) · 3.72 KB
/
RecursionPractice
File metadata and controls
192 lines (160 loc) · 3.72 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// RecursionPractice.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
//The following 2 declarations are used in problems 27, 28, 29
/*1*/const int MAX_CAPACITY = 20;
/*2*/typedef int ElementType;
/*3*/typedef ElementType ArrayType;
int getNumber()
{
int n = 0;
cout << "Enter a number: ";
cin >> n;
return n;
}
int f(char ch1, char ch2, int &n)
{
n++;
if (ch1 > ch2)
return 0;
if (ch1 + 1 == ch2)
return 1;
//else
cout << "Characters being returned ch1: " << static_cast<char>(ch1 + 1) << " , ch2: " << static_cast<char>(ch2 - 1) << endl;
return f(ch1 + 1, ch2 - 1, n) + 2;
}
unsigned problem21(unsigned n)
{
if (n == 0)
return 0;
//else
return 1 + problem21(n / 10);
}
int problem23(int n)
{
if (n < 10)
cout << n;
else
{
cout << (n % 10);
return problem23(n / 10);
}
}
void reverseArray(ArrayType a[], int first, int last)
//Reverse the contents of a[first], ... , a[last]
{
if (last == first)
return;
else
{
cout << a[--last] << " ";
reverseArray(a, first, last);
}
}
int sumArray(ArrayType a[], int n)
//Return the sum of a[0], ... , a[n-1]
{
if (n == 0)
return 0;
//else
return a[n] + sumArray(a, --n);
}
int location(ArrayType a[], int first, int last, ElementType elm)
//Return the location of elm in a[first], ..., a[last]
{
if (first == last)
return 0;
if (elm != a[first])
location(a, first + 1, last, elm);
else
return first + 1;
}
int hailstone(int n)
{
static int i = 0;
if (n == 1)
return i;
if (n % 2) //If n is odd, remainder != 0
{
hailstone(3 * n + 1);
i++;
}
else if (!(n % 2)) //If n is even, remainder is zero
{
hailstone(n / 2);
i++;
}
}
void fillArray(int *p, int maxValue)
{
if (maxValue == 0)
return;
else
{
*p = maxValue;
p--;
fillArray(p, maxValue - 1);
}
}
int main()
{
/*cout << "Exercises 10.1 #7" << endl;
int num;
int count = 0;
num = f('a', 'e', count);
cout << num << endl;
cout << "coutn: " << count << endl;
count = 0;
num = f('h', 'c', count);
cout << num << endl;
cout << "count: " << count << endl;*/
cout << "PROBLEM 21" << endl;
int z = 0;
z = getNumber();
cout << "Number of digits in nonnegative integer 12345: " << problem21(z) << endl;
cout << endl;
cout << "PROBLEM 23" << endl;
z = getNumber();
problem23(z);
cout << endl;
cout << "PROBLEM 27" << endl;
cout << "Fill array with 1, 3, 5, 7, 9" << endl;
ElementType ary[MAX_CAPACITY];
ElementType *ptr;
ptr = &ary[MAX_CAPACITY-1];
fillArray(ptr, MAX_CAPACITY); //Fill array function for problem # 4
reverseArray(ary, 0, MAX_CAPACITY);
cout << endl;
cout << "PROBLEM 28" << endl;
cout << "Use Problem 27 array" << endl;
cout << "Sum of array: " << sumArray(ary, MAX_CAPACITY) << endl;
cout << endl;
cout << "PROBLEM 29" << endl;
cout << "Use Problem 27 array" << endl;
int value = 0, index = 0;
value = getNumber();
index = location(ary, 0, MAX_CAPACITY, value);
if (index)
cout << "The number " << value << " is located in index " << index << endl;
else
cout << "Value not found" << endl;
cout << endl;
cout << "2. HAILSTONES ALGORITHM" << endl;
int iterations = 0;
iterations = hailstone(getNumber());
cout << "It takes " << iterations << " iterations of the hailstone algorithm "
<< "for value to reach 1." << endl;
cout << endl;
cout << "3. Print out Linked List program in reverse recursively";
//Included in separate file
cout << "Included in separate file" << endl;
cout << endl;
cout << "4. Fill array of size 20 with values of 1-20" << endl;
// 'ElementType *ptr = &ary[MAX_CAPACITY];' declared on line 146
// fillArray(ptr, MAX_CAPACITY);
cout << "Function was utilized in problem 1, first used on line 148" << endl;
cout << endl;
return 0;
}