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

No helper functions or Auxiliary functions. You must use recursion. Use magic fu

ID: 3856388 • Letter: N

Question

No helper functions or Auxiliary functions. You must use recursion. Use magic functions. Your code must be such that if we insert it into a suitable test framework with a main routine and appropriate #include directives, it compiles. Please show the base case and the recursive case. Here is a function, with descriptions of what they are supposed to do. They are incorrectly implemented. Replace the incorrect implementations of these functions with correct ones that use recursion in a useful way: your solution must not use the keywords while, for, or goto. You must not use global variables or variables declared with the keyword static, and you must not modify the function parameter lists. You must not use any references or pointers as parameters except for the parameters representing arrays. //Return true if the sum of any combination of elements in the array //a equals the value of the target. ////Pseudocode Example: //sumCombination([2, 4, 8], 3, 10)- > true//sumCombination([2, 4, 8], 3, 12) = > true //sumCombination([2, 4, 8], 3, 11) = > false //sumCombination(O.0, 0) = > true //bool sumCombination(const int all, int size, int target) { return false;//This is not always correct. }

Explanation / Answer

#include <iostream>

using namespace std;

bool sumCombination(const int a[], int size, int target)
{
   int sum = 0;
   int index;
   for (int i = 0; i < size; i++)
   {
       sum = sum + a[i];
   }

   if (sum == target)
   {
       return true;
   }

   else
   {
       for (int i = 0; i < size; i++)
       {
           int *array = new int[size-1];
           index = 0;
           for (int j = 0; j < size; j++)
           {
               if (j != i)
               {
                   array[index] = a[j];
                   index++;
               }
           }

           if (sumCombination(array, size-1, target) == true)
           {
               return true;
           }
           delete [] array;
       }
   }
   return false; // default
}

int main()
{
   int a[] = {2,4,8};
   int b[] = {};
   cout << sumCombination(a, 3, 10) << endl;
   cout << sumCombination(a, 3, 12) << endl;
   cout << sumCombination(a, 3, 11) << endl;
   cout << sumCombination(b, 0, 0) << endl;
  
   return 0;
}

----------------------

OUTPUT:

$ g++ sumistarget.cpp
$ ./a.out
1
1
0
1

NOTE: According to the question, you have to write only the function part. But I have given you the whole program so that you can see it working. If you have any query or need more help, you can ask in the comments.

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