// // Program: in_array // // Purpose: test recursive function for looking for an element // in an array. // // Input: // N integers for the array (currently N = 10), then a // sequence of numbers to look up in the array (program // will prompt for these). // Output: // for each number to look up, whether it is in the array. // #include #include // has EXIT_SUCCESS // Recursive function to look for element 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 in_array(int x, int n, int a[]) { if (n == 0) return false; else return (x == a[n-1]) || in_array(x, n-1, a); } // Main program. int main(void) { const int N = 10; int nums[N]; int n; char prompt[] = "Enter a number to search for, or control-D " "to end:\n"; cout << "Enter " << N << " numbers for array:\n"; for (int i = 0; i < N; i++) cin >> nums[i]; cout << prompt; while (cin >> n) { if (in_array(n, N, nums)) cout << "found\n"; else cout << "not found\n"; cout << prompt; } return EXIT_SUCCESS; }