In c++
Implement the following recursive function, that returns true if all elements in the given array are smaller than the given value, otherwise false. Return false for the empty array and NULL array cases.
bool all_smaller_r(int *a1, int size, int value)
{
}
C++ Code to check if all elements are smaller than a value or not
bool all_smaller_r(int *a1, int size, int value)
{
// return false if array is null
if ( a1==NULL )
{
return false;
}
// if all elements are processed and none are greater than
value
// then it means it has reached end of array so size is 0
// hence return true
if( size==0)
{
return true;
}
// if any element we found > value then return false
if ( *a1 > value )
{
return false;
}
// call the function by doing three things
// 1. Shift the pointer to next value
// 2. Decrement the size
// 3 No change in value
return all_smaller_r(++a1,size-1,value);
}
Code Screen Shot and Output
Here value is 5 and as All the values are smaller than 5 so We get Output as 1 which means True
This is how we can write a recursive code in C++ to check each element is smaller than given value or not
Thank You
If u like the answer do Upvote it and have any doubt ask in comments
Get Answers For Free
Most questions answered within 1 hours.