1. Write a C++ program that implements the recursive function isMember as declared above. Pass an array to the function by reference. Here are two sample runs of the program.
The elements of the array are: 0 4 5 6 7 Enter the element you want to find: 6 Element is in the array Press any key to continue . . . |
#include <iostream> using namespace std; bool isMember(int *arr, int size, int item) { if (size == 0) return false; else return isMember(arr, size - 1, item) || arr[size - 1] == item; } int main() { int arr[] = {0, 4, 5, 6, 7}; int size = sizeof(arr) / sizeof(arr[0]); cout << "The elements of the array are: "; for (int i = 0; i < size; ++i) { cout << arr[i] << " "; } cout << endl << "Enter the element you want to find: "; int item; cin >> item; if (isMember(arr, size, item)) { cout << "Element is in the array" << endl; } else { cout << "Element is not in the array" << endl; } return 0; }
Get Answers For Free
Most questions answered within 1 hours.