Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Time Series Databases New Ways To Store And Access Data

Authors: Ted Dunning, Ellen Friedman

1st Edition

1491914726, 978-1491914724

More Books

Students also viewed these Databases questions

Question

7. Explain why retirees may be valuable as part-time employees.

Answered: 1 week ago

Question

3. Provide advice on how to help a plateaued employee.

Answered: 1 week ago