Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ MyProgrammingLab problem: Define a function bool canMakeInRange( int nums[],

ID: 3562305 • Letter: C

Question

C++ MyProgrammingLab problem: 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

To write the function, first it has to checked for the min and max values.

If the min<=0 and max >=0 is true then it will enter into the total of the subset of the nums array.

Next step is to verify whether the total value is with in the range of min and max. If they staisfy the condition then it will return the overall boolean value true. if not it will return false.

bool canMakeInRange(int nums[], int length, int min, int max)

     {

          int mn = 1, mx=6;

          int b[] = new int[(mx-min)+1];

          int l = mx-mn;

          b=subArray(nums,mn,mx);

          int x = 0;

          boolean t = true;

          if(min <= 0 && max >= 0)

          {

              for(int i=0;i < b.length;i++)

              {

                   x+=b[i];

              }

              if(x >= min && x<= max)

              {

                   t=true;

              }

          }

          else if(min > 0 && max < 0 && length ==0)

          {

              t=false;

          }

          return t;

     }

     int[] subArray(int[] n, int min, int max)

     {

          int a[]=new int[(max-min)+1];

          int j=0;

          for(int i=min;i<=max; i++)

          {

              a[j]=n[i];

              j++;

          }

          return a;

     }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote