Question

Here are five functions, with descriptions of what they are supposed to do. They are incorrectly...

Here are five functions, 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:
//      comboSum([2, 4, 8], 3, 10) => true
//      comboSum([2, 4, 8], 3, 12) => true
//      comboSum([2, 4, 8], 3, 11) => false
//      comboSum([], 0, 0)         => true
//
bool comboSum(const int a[], int size, int target)
{
    return false;  // This is not always correct.
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Thanks for the question, here is the implementation of the comboSum() function.

====================================================================

# include <iostream>

using namespace std;

bool comboSum(const int a[], int size, int target) {

               

                if(size==0) return target==0;

               

                for(int i = size-2; i>=0; i--){

                                if(a[size-1]+a[i] == target){

                                                return true;

                                }

                }

                return comboSum(a,size-1,target);

}

int main(){

               

                int a[] = {2,4,8};

                if(comboSum(a,3,9)) cout<<"true";

                else cout<<"false";

}

====================================================================

Add a comment
Know the answer?
Add Answer to:
Here are five functions, with descriptions of what they are supposed to do. They are incorrectly...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • / Animal.hpp #ifndef ANIMAL_H_ #define ANIMAL_H_ #include <string> class Animal { public: Animal(); Animal(std::string, bool domestic=false,...

    / Animal.hpp #ifndef ANIMAL_H_ #define ANIMAL_H_ #include <string> class Animal { public: Animal(); Animal(std::string, bool domestic=false, bool predator=false); std::string getName() const; bool isDomestic() const; bool isPredator() const; void setName(std::string); void setDomestic(); void setPredator(); protected: // protected so that derived class can directly access them std::string name_; bool domestic_; bool predator_; }; #endif /* ANIMAL_H_ */ //end of Animal.h // /////////////////////////////////////////////////////////////////Animal.cpp #include "Animal.h" Animal::Animal(): name_(""),domestic_(false), predator_(false){ } Animal::Animal(std::string name, bool domestic, bool predator): name_(name),domestic_(domestic), predator_(predator) { } std::string Animal::getName() const{ return...

  • Concepts tested by the program: Working with one dimensional parallel arrays Use of functions Use of...

    Concepts tested by the program: Working with one dimensional parallel arrays Use of functions Use of loops and conditional statements Description The Lo Shu Magic Square is a grid with 3 rows and 3 columns shown below. The Lo Shu Magic Square has the following properties: The grid contains the numbers 1 – 9 exactly The sum of each row, each column and each diagonal all add up to the same number. s is shown below: Write a program that...

  • Requirements Write functions isMemberR, a recursive function, and isMemberI, which will use an iterative approach, to...

    Requirements Write functions isMemberR, a recursive function, and isMemberI, which will use an iterative approach, to implement a binary search algorithm to determine whether a given element is a member of a given sequence Each function will have two parameters, aseq, a sorted sequence, and target. isMemberR and isMemberI will return True if target is an element of the sequence, and False otherwise. Your implementations must implement the binary search algorithm described above. When function i sMemberR recursively invokes itself,...

  • A_____error causes the program to prematurely terminate during execution. ==.!=.>.<= are examples of____operators. A_____loop always executes...

    A_____error causes the program to prematurely terminate during execution. ==.!=.>.<= are examples of____operators. A_____loop always executes its body at least once. The function_______lets the compiler know that the function will be defined later. The_____statement skips to the end of the body of a loop. The_____loop has a header which consists of three sections separated by semicolons. The____of an infinite loop is always true. A(n)____statement cannot exist without an if statement. The_____of a variable tells the compiler which values it can...

  • Use a B-Tree to implement the set.h class. #ifndef MAIN_SAVITCH_SET_H #define MAIN_SAVITCH_SET_H #include <cstdlib> // Provides...

    Use a B-Tree to implement the set.h class. #ifndef MAIN_SAVITCH_SET_H #define MAIN_SAVITCH_SET_H #include <cstdlib> // Provides size_t namespace main_savitch_11 { template <class Item> class set { public: // TYPEDEFS typedef Item value_type; // CONSTRUCTORS and DESTRUCTOR set( ); set(const set& source); ~set( ) { clear( ); } // MODIFICATION MEMBER FUNCTIONS void operator =(const set& source); void clear( ); bool insert(const Item& entry); std::size_t erase(const Item& target); // CONSTANT MEMBER FUNCTIONS std::size_t count(const Item& target) const; bool empty( ) const...

  • Add a recursive Boolean function called checkRecurse to class IntegerLinkedList to check if any two consecutive...

    Add a recursive Boolean function called checkRecurse to class IntegerLinkedList to check if any two consecutive integers in the linked list are equal (return true in this case, false otherwise). You may assume that the list is not empty. A recursion “helper” function is already included in class IntegerLinkedList. You only need to write the recursive function. For example, checkRecurseHelper() should return true for the linked list shown below. A main function (prob3.cpp) is given to you to add data...

  • IntList Recursion Assignment Specifications: You will add some additional recursive functions to your IntList class as...

    IntList Recursion Assignment Specifications: You will add some additional recursive functions to your IntList class as well as make sure the Big 3 are defined. IntNode class I am providing the IntNode class you are required to use. Place this class definition within the IntList.h file exactly as is. Make sure you place it above the definition of your IntList class. Notice that you will not code an implementation file for the IntNode class. The IntNode constructor has been defined...

  • Problem 1 1. Consider the following function (K is the size of array A and L...

    Problem 1 1. Consider the following function (K is the size of array A and L is the size of array B) bool func (int A[], int K, int B[], int L, int start) { if (L > K-start) return false; for (int i =0; i < L; i++) { if(A[start+i] != B[i]) return false } return true; } What are the input and output variables to this function. Trace it for the following call: int F[] = {1, 3,...

  • I'm just not sure how to tackle all the template class this header wants me to...

    I'm just not sure how to tackle all the template class this header wants me to write. I got this far into making the template for public and private and would like help on the template functions. Thank you! This is in C++ by the way. #include <iostream> #include <cassert> using namespace std; #ifndef ARRAYLIST_H #define ARRAYLIST_H template<typename T> class arrayList { public:    arrayList(); //Constructor with default parameter. //Sets maxSize = 100 and length = 0 if no parameter...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT