Question

c++ please. Write a recursive method that takes the following parameters: * a sorted array of...

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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 :

Enter the size of array : 5 Enter the elements of array : 1 2 3 4 5 Enter the element to search in array : 3 True ... Program

Add a comment
Know the answer?
Add Answer to:
c++ please. Write a recursive method that takes the following parameters: * a sorted array of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a function named findTarget that takes three parameters: - numbers, an array of integers -...

    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...

    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 -...

    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...

    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....

    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 binsearch that carries out the binary search algorithm on a sorted array...

    Write a C++ function binsearch that carries out the binary search algorithm on a sorted array of integers. Your function takes as parameters an array of integers (sorted in increasing order), the size of the array, and a target integer to search for. The function returns the index of the target in the array, and returns -1 if the target is not in the array. The file main1.txt on the webpage contains code that generates a specific array of integers...

  • Write a C++ function, smallestIndex, that takes as parameters an int array and its size and...

    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 can not get this recursive binary search to wok for an edge case and it...

    I can not get this recursive binary search to wok for an edge case and it works but not for all, If you could look at it and try to find what is wrong to make this work for all edge cases. //Write a function that will return true if an element k is found in an array of integers A and false otherwise. The input to your //function is the array, its length 0 < n <= 10000, and...

  • I am stuck on trying to get this recursive function's logic to make it work. //Write...

    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,...

    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...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT