// // example of recursion: // function to check for element in array, plus test program // // test program input: 10 integers for array, plus 0 or more // integers to search for (input terminated by EOF) // test program output: results of searches. // #include #include // has EXIT_SUCCESS using namespace std; // Function to determine whether element is in array. // Pre: n >= 0, and array a has at least n elements // Post: returns true if x is in a[0] .. a[n-1], false // otherwise bool inArray(const int x, const int n, const int a[]) { if (n <= 0) return false; else return (x == a[n-1]) || inArray(x, n-1, a); } // Main program. int main(void) { const int SIZE = 10; int nums[SIZE]; int x; cout << "enter " << SIZE << " integers for array:\n"; for (int i = 0; i < SIZE; ++i) cin >> nums[i]; char prompt[] = "enter a number to search for, or control-D " "to end:\n"; cout << prompt; while (cin >> x) { if (inArray(x, SIZE, nums)) cout << "found\n"; else cout << "not found\n"; cout << prompt; } return EXIT_SUCCESS; }