c++ please.
Write a recursive method that takes the following parameters:
* a sorted array of ints
* an int representing the size of the array
* an int representing a value to be searched for
Your function should return a bool. If the element is in the array, return true. Otherwise, return false. Your function should never produce memory access errors, regardless of the size of the array.
C++ CODE :
#include <iostream>
using namespace std;
bool func(int a[], int n, int num)
{
if (a[n] == num)
{
return true; // return true if they found element
}
else if (n == -1)
{
return false; // return false if nothing found
}
else
{
return func(a, n - 1, num); // recursion function
}
}
int main()
{
int n, num;
cout <<"Enter the size of array : ";
cin >> n; // read the size of array
int a[n];
cout <<"\nEnter the elements of array : ";
for(int i = 0; i < n; i++)
{
cin >> a[i]; // read array of elements
}
cout <<"\nEnter the element to search in array : ";
cin >> num; // read the element to be searched
if(func(a, n, num)) // call function
{
cout << "\nTrue"; // if element found
}
else
{
cout << "\nFalse"; // if element not found
}
return 0;
}
OUTPUT :
c++ please. Write a recursive method that takes the following parameters: * a sorted array of...
Write a function named findTarget that takes three parameters: - numbers, an array of integers - size, an integer representing the size of array - target, a number The function returns true if the target is inside array and false otherwise
using c++ Write a function that returns true if a given integer array is sorted(in ascending order) and false if it is not. The function should perform no more than "size" comparisons. bool isSorted(int A[], int size);
1. Write a function named findTarget that takes three parameters: numbers, an array of integers - size, an integer representing the size of array target, a number The function returns true if the target is inside array and false otherwise 2. Write a function minValue that takes two parameters: myArray, an array of doubles size, an integer representing the size of array The function returns the smallest value in the array 3. Write a function fillAndFind that takes two parameters:...
In c++ 1. Write a function named findTarget that takes three parameters - numbers: an array of integers size: an integer representing the size of array target: a number The function returns true if the target is inside the array and false otherwise 2. Write a function min Valve that takes two parameters: myArray an array of doubles - size: an integer representing the size of array The function returns the smallest value in the array 3 Wrile a funcion...
The aim of this exercise is to check the presence of a number in an array. Specifications: • The items are integers arranged in ascending order. • The array can contain up to 1 million items. • The array is never NULL. Implement the method bool Answer::exists(int ints[], int size, int k) so that it returns true if k belongs to ints, otherwise the method should return false. size contains the size of ints. Important note: Try to save CPU...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. To test your function, write a main that prompts a user for a list of 15 integers and outputs the index and value of the first occurrence of the smallest value. The program should print out Enter 15 integers: The position of the first occurrence of the smallest element in...
I am stuck on trying to get this recursive function's logic to make it work. //Write a recursive function that returns true if the sequence of 0 < n integers in A is sorted in non-increasing order and false otherwise. iostream and cmath libraries only bool is_sorted(const int *A, unsigned int n){ if (n == 0) return false; if (A > A+1) return true; else return false; }
Write a function named insertBeforeKey that takes the parameters as follows list as an char array, capacity for the capacity of the array, numItems has the number of items in the array, key is an element of the array before which newVal is to inserted. Assume key is always present in the array. and newVal has the new value to be inserted into the array. If the array is at capacity then the function should return -1, otherwise return 0...
1.) Write a function called canMakeSum() that takes a sorted array of n integers and determines whether two distinct elements of the array add up to some specified value, "key". If so, return 1. Otherwise, return 0. The function signature is: int canMakeSum(int *array, int n, int key);
Write a function named numPerfect that takes as parameters: an array of integer scores between zero and 100 (inclusive) the size of the array and returns the number of perfect scores in the array. Also write a main function that creates and initializes an array of ints (in the appropriate range), calls the function with that array, and prints out the return value.