Question
In C++ Define a function bool canMakeInRange(int nums[], int length, int min,int max); that returns true if the values in some subset of nums add
In C++
Define a function bool canMakeInRange(int nums[], int length, int min,int max); that returns true if the values in some subset of nums add up to a total in range [min, max], and false otherwise. If min <= 0 and max >= 0 then the function should return true. (That's because the empty sum, i.e. adding no values at all, is by convention equal to zero.)
If min > 0 or max < 0 and length == 0 then the function should return false. Of course any time the initial value of min is strictly greater than the initial value of max the function would also return false, e.g. min = 3 and max = 2. Your function is tested with a subarray of {-4, 5, -7, 4, 7, 100, 1000, 10000, 100000} and various values of min and max. The driver will print out which values of min and max are used and what the subarray is. Hint for coding: Consider two cases: either the last element of nums in used or not. If you don't use the last element then min and max are unchanged. If you do use the last element then min and max are both decreased by that amount for your recursive call.
The driver used to test your code is below. Warning: this exercise has many test cases so be prepared to wait a while for the tests to complete.
bool canMakeInRange(int nums[], int length, int min,int max) ; int main() { int numbers[12] = {-4, 5, -7, 4, 7, };
int min,max, numberOfValues; cin >> min >> max;
cin >> numberOfValues;
cout << "The array is ";
for (int i = 0; i < numberOfValues; i++) cout << numbers[i] << " ";
cout << "." << endl;
cout << "The min and max are " << min << " and " << max << "."; cout << "The return value is " << canMakeInRange(numbers, numberOfValues, min,max) << ".";
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started