#include using namespace std; /* * Base case: The case where we stop recursion * If we've reached that case, we return WITHOUT calling ourselves again * General case, or recursive step * This must move toward being done * Includes a recursive call (a call to the function we're writing) */ void pia_recur(int *array, size_t length){ if(!length) return; cout << *array << ", "; pia_recur(array + 1, length - 1); } void print_int_array(int* array, size_t length){ // Convert this to a do while loop cout << "["; pia_recur(array, length); cout << "\b\b]\n"; } void task_one(int startpoint){ if(startpoint <= 0) // Called the base case return; task_one(startpoint - 1); cout << startpoint - 1 << " "; } void task_two(int *array, size_t length){ if(length == 0) { cout << "\n"; return; } cout << *array << " "; // Same as: cout << array[0] << " "; task_two(array + 1, length - 1); // Same as: task_two(&array[1], length - 1); } void task_three(int end_number){ if(end_number < 0){ return; } task_three(end_number - 1); cout << end_number + 1 << " "; } void task_four(int *numbers, size_t length, int multiplier){ if(!length){ cout << "\n"; return; } cout << *numbers * multiplier << " "; task_four(numbers + 1, length - 1, multiplier); } int main(){ // Task One for(int i = 0; i < 10; i++) cout << i << " "; cout << "\n"; // Recursive solution task_one(10); cout << "\n"; // Task Two int numbers[] = {3, 6, 9, 12, 15}; int j = 0; do { cout << numbers[j] << " "; j++; } while(j < 5); cout << "\n"; // Recursive solution task_two(numbers, 5); // Task Three int k = 0; while(true){ if(k++ > 10) break; cout << k << " "; } cout << endl; // Recursive Solution task_three(10); cout << "\n"; // Task Four for(int i : numbers) cout << i*5 << " "; cout << "\n"; // Recursive Solution task_four(numbers, 5, 5); // Task Five int l = 0; top: cout << numbers[l] * 100 << " "; l++; if(l < 5) goto top; cout << "\n"; // Recursive Solution task_four(numbers, 5, 100); print_int_array(numbers, 5); return 0; }