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
|
#include <iostream>
#include <iomanip>
#include <algorithm> // for min, max
using namespace std;
// ===========ADD==================
int add2_fx(int a, int b) { // functional (for illustration only), PROVIDED, DO NOT CHANGE
return a+b;
}
int add2_loop(int a, int b) { // looping, PROVIDED, DO NOT CHANGE
int sum=a;
if (b>0)
for (int i=0; i<b; ++i) ++sum;
else // b<=0
for (int i=b; i<0; ++i) --sum;
return sum;
}
// Rule: Do NOT use the *, /, +, =, *=, /=, +=, -=, ^, &, <<, >> operators.
// DO NOT USE bitwise operators like: (a & b) << 1; These are not part of the course.
// You MAY use: ++ and/or -- (as done in add2_loop)
// Constraints: No loops allowed; no static local or global variables.
int add2_recurse(int a, int b) { // recursive
top:
if(a > 0) { --a; ++b; goto top; }
if(a < 0) { ++a; --b; goto top; }
return b;
}
// ===========MULTIPLY==================
int mult2_fx(int a, int b) { // functional (for illustration only), PROVIDED, DO NOT CHANGE
return a*b;
}
int mult2_loop(int a, int b) { // looping, PROVIDED, DO NOT CHANGE
int product=0;
if (b>0)
for (int i=0; i<b; ++i) product+=a;
else // b<=0
for (int i=b; i<0; ++i) product-=a;
return product;
}
int mult2_recurse(int a, int b) { // recursive
// Rule: you may NOT use the *, *=, / or /= operators.
// You MAY use: +, -, +=, -= operators (as done in mult2_loop)
// Constraints: No loops allowed; no static local or global variables.
// Your new code goes here; modify...
return 0;
}
// ===========SEARCH LOOP==================
int search_loop(int array[], int size, int target) { // looping, PROVIDED, DO NOT CHANGE
for (int i=0; i<size; ++i)
if (array[i]==target) {return i;}
return -1;
}
// Constraints: No loops allowed; no static local or global variables.
// Your new code goes here; modify...
int search_recurse(int array[], int size, int target) {// recursive
size--;
int rec;
if (size >= 0){
if (array[size] == target)
return size;
else
rec = search_recurse(array, size, target);
}
else
return -1;
return rec;
}
// ===========MESSAGE LAYOUT==================
enum control_flow_t {functional, looping, recursive};
void show_test(int n, string s, control_flow_t control_flow) {
// utility function to format test output
// n: test number; s: "description"; control_flow: functional, looping or recursive
static const string fx="functional", sl="looping", sr="recursive";
int max_len=max(fx.size(), max(sl.size(), sr.size()));
string msg;
switch (control_flow) {
case functional: msg=fx; break;
case looping: msg=sl; break;
case recursive: msg=sr; break;
default: msg="??"; break;
}
char iorr=msg[0];
msg=" ("+msg+"): ";
cout<<"\n"<<n<<iorr<<") "<<s<<setw(max_len+5)<<left<<msg;
}
void infinite_recursion() {
// try at your own risk! Error message can be interesting.
infinite_recursion();
}
// This code may be helpful when developing your recursive functions.
void recurse(int times_to_call) {
// a generalized recursion outline; has 5 locations to do work...
cout << "recurse head... " << "("<<times_to_call<<") " <<endl; // ALWAYS
if (times_to_call>1) { // decision to recursive call
cout << "recurse before call... " << "("<<times_to_call<<") " <<endl;
recurse(times_to_call-1); // work (problem simplification) can be done inside the parameter list!
cout << "recurse after call... " << "("<<times_to_call<<") " <<endl;
} else {
cout << "recurse else option... " << "("<<times_to_call<<") " <<endl;
}
cout << "recurse ...tail " << "("<<times_to_call<<") " <<endl; // ALWAYS
}
int main () {
// ----- DO NOT CHANGE ANY CODE BELOW in main(). CALLS FUNCTIONS ABOVE FOR TESTING -----
cout<<"start...\n";
show_test(4, "add2", functional);
cout<<add2_fx( 4, 5); cout<<" "; // correct: 9
cout<<add2_fx(-5, 15); cout<<" "; // correct: 10
cout<<add2_fx(20, -9); cout<<" "; // correct: 11
cout<<add2_fx(-7, -5); cout<<" "; // correct: -12
show_test(4, "add2", looping);
cout<<add2_loop( 4, 5); cout<<" "; // correct: 9
cout<<add2_loop(-5, 15); cout<<" "; // correct: 10
cout<<add2_loop(20, -9); cout<<" "; // correct: 11
cout<<add2_loop(-7, -5); cout<<" "; // correct: -12
show_test(4, "add2", recursive);
cout<<add2_recurse( 4, 5); cout<<" "; // correct: 9
cout<<add2_recurse(-5, 15); cout<<" "; // correct: 10
cout<<add2_recurse(20, -9); cout<<" "; // correct: 11
cout<<add2_recurse(-7, -5); cout<<" "; // correct: -12
cout<<endl;
show_test(5, "mult2", functional);
cout<<mult2_fx( 50, 2); cout<<" "; // correct: 100
cout<<mult2_fx( 5, -40); cout<<" "; // correct: -200
cout<<mult2_fx(-100, 3); cout<<" "; // correct: -300
cout<<mult2_fx(-4, -100); cout<<" "; // correct: 400
show_test(5, "mult2", looping);
cout<<mult2_loop( 50, 2); cout<<" "; // correct: 100
cout<<mult2_loop( 5, -40); cout<<" "; // correct: -200
cout<<mult2_loop(-100, 3); cout<<" "; // correct: -300
cout<<mult2_loop(-4, -100); cout<<" "; // correct: 400
show_test(5, "mult2", recursive);
cout<<mult2_recurse( 50, 2); cout<<" "; // correct: 100
cout<<mult2_recurse( 5, -40); cout<<" "; // correct: -200
cout<<mult2_recurse(-100, 3); cout<<" "; // correct: -300
cout<<mult2_recurse(-4, -100); cout<<" "; // correct: 400
cout<<endl;
int primes[] {211, 307, 401, 503, 601, 701, 809, 907, 1009, 1103}; // some numbers to search for
int size_primes=sizeof(primes)/sizeof(primes[0]); // get #elements in array
show_test(6, "search", looping);
cout<<search_loop(primes, size_primes, 211)<<", ";
cout<<search_loop(primes, size_primes, 401)<<", ";
cout<<search_loop(primes, size_primes, 907)<<", ";
cout<<search_loop(primes, size_primes, 1103);
show_test(6, "search", recursive);
cout<<search_recurse(primes, size_primes, 211)<<", ";
cout<<search_recurse(primes, size_primes, 401)<<", ";
cout<<search_recurse(primes, size_primes, 907)<<", ";
cout<<search_recurse(primes, size_primes, 1103)<<endl;
// infinite_recursion(); // uncomment to experience infinite recursion (crash, error message)
cout<<"\n...end\n";
return 0;
}
|