Define a function bool canMakeInRange( int nums[], int length, int min, int max)
ID: 3562359 • Letter: D
Question
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 . If min > 0 or max < 0 and length == 0 then the function should return false .
Your function is tested with the array {-4, 5, -7, 4, 7} and various values of min and max.
Hint: consider two cases: either the last element of nums is used in the total or not.
Explanation / Answer
#include <iostream>
using namespace std;
bool canMakeInRange(int nums[], int length, int min, int max);
int main()
{
cout << "Hello World" << endl;
int nums[5]={-4, 5, -7, 4, 7};
int max = -9;
int min = -7;
int length=5;
bool b = canMakeInRange(nums, length, min,max);
if(b)
cout<<"true";
else
cout<<"false";
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------
bool canMakeInRange(int nums[], int length, int min, int max)
{
if( min <= 0 && max >= 0)
return true;
if((min > 0 || max < 0) && length == 0)
return false;
}
-----------------------------------------------------------------------------------------------------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.