Question

In: Computer Science

Given a list of positive integers c[0...n − 1], and a positive integer v, decides whether...

Given a list of positive integers c[0...n − 1], and a positive integer v, decides whether we can use numbers from c[0...n − 1] to make a sum of v, allowing any particular number in the list to be used multiple times. Or, mathematically, this means that there exists non-negative integer coefficients, x0, x1, ..., xn−1, such that v = x0c[0] + x1c[1] + ...xn−1c[n − 1].

For example, given c[0...3] = {2, 4, 6, 10}, and v = 17, the function shall return false, as it’s impossible to use numbers from c to add up to 17.

Given c[0...3] = {2, 4, 6, 10}, and v = 34, the function shall return true, as 34 can be expressed as 17 2’s added up (there are other ways to make value 34 as well).

Based upon the pseudocode given at the end,

(a) implement and test the following C++ function (minimally, you shall test the function with examples given above.

       /* Return true if v can be expressed as sums of values from coins (repetition is allowed)
       @param coins is a vector of positive int values
       @param v is a positive int
       @return true if v can be written as a sum of values from coins, return false otherwise
       */
       bool CoinChange (vector<int> coins, int v)

(b) In comment of your code, provide a tracing of the function’s execution with input: coins[0...3] = {3, 4, 6, 10}, and v = 14. You should:

• list in order the recursive calls made, with the parameters passed and values returned for each call.

• Comment on when backtracking happens.

Pseudocode

/*
Return true if v can be expressed as sum of values from c[0...n-1] @param c[0...n-1]: stores n positive integers
@param v: non-negative integers

2

*/
bool CoinChange (c[0...n-1], v) {

if (v==0)

for i=0 to n-1: if (c[i]==v)

//fill in the blank here
 //fill in the blank here

else if (c[i]<v)
//Think: if we include c[i] to make change amount of
// v, then we only need to make change amount of _____
if (CoinChange (c, ) == True): //fill in the parameter for the

                   return true; //function call above
    // If we haven’t returned true by now, return false
    return false;

}

Solutions

Expert Solution

The required code followed by sample output has been given below, also the required answers for tracing and listing the recursive calls has been done in the comment lines, also for getting the list of recursive calls of any functions you can uncomment the first line in the function.

CODE:


#include<iostream>
using namespace std;

bool CoinChange (int c[],int n, int v) {                                 // when v equals 14, and c = {3,4,6,10}

    // cout<<"CoinChange[c,n,"<<v<<"] --> ";                              // for the list of function call just uncomment this line, the list will be showed in the output

    if (v==0){

        return true;
    }


    for (int i=0;i<n;i++){

        if (c[i]==v)
                                                                            // will be checked for i = 0 and c[0] = 3 != 14 so go it else condition
            return true;


        else if (c[i]<v){                                              //  3 < 14 so statements under else if body executed

             if (CoinChange (c,n,v-c[i]) == true)                        // the same function get called for (c,n,14-3) = (c,n,11) now it will be checked for the value v = 11 for same coins, again it comes here (same statement)
                return true;                                             // and then will call (c,n,11-3) = (c,n,8) and so on...


        }                                                            // so for v = 14, the trace of function execution will be, (lets call the function CC)
                                                                    // CC(c,n,14) --> CC(c,n,11) --> CC(c,n,8) --> CC(c,n,5) --> CC(c,n,2) --> CC(c,n,1) --> CC(c,n,4)
                                                                    //  CC(c,n,1) --> True

    }
    return false;                                                      // backtracking can happens when the first time CC[c,n,2] get called because there is no element less or equal to 2 so it returns false so the program backtrack from here
}

/* The order of recursive calls will be

    CC[c,n,14] --> True
    CC[c,n,11] --> True ( when i = 0 for v = 14)
    CC[c,n,8] --> True  (when i = 0 for v = 11)
    CC[c,n,5] --> False (when i = 0 for v= 8)
    CC[c,n,2] --> False ( when i = 0 for v = 5)
    CC[c,n,1] --> False (when i = 1 for v = 5)   ( backtracking)
    CC[c,n,4] --> True (when i = 1 for v = 8)
    CC[c,n,1] --> False ( when i = 0 for v = 4) and i gets increased for v = 4, so i becomes 1 and c[1] = 4 and v = 4 so it returns true.

*/



int main(){

    int c[] = {2,4,6,10};
    int n = 4;
    int v = 34;
    cout<<"\nFor v = 34 and c = {2,4,6,10}"<<endl;
    if (CoinChange(c,n,v))
        cout<<"True"<<endl;
    else
        cout<<"False"<<endl;

    v = 17;
    cout<<"\nFor v = 17 and c = {2,4,6,10}"<<endl;
    if (CoinChange(c,n,v))
        cout<<"True"<<endl;
    else
        cout<<"False"<<endl;

    c[0] = 3;
    v = 14;
    cout<<"\nFor v = 14 and c = {3,4,6,10}"<<endl;
    if (CoinChange(c,n,v))
        cout<<"True"<<endl;
    else
        cout<<"False"<<endl;



    return 0;


}

OUTPUT:

NOTE: In case of any query, you are always welcomed in the comment section. HAPPY LEARNING!!


Related Solutions

Determine, for a given graph G =V,E and a positive integer m ≤ |V |, whether...
Determine, for a given graph G =V,E and a positive integer m ≤ |V |, whether G contains a clique of size m or more. (A clique of size k in a graph is its complete subgraph of k vertices.) Determine, for a given graph G = V,E and a positive integer m ≤ |V |, whether there is a vertex cover of size m or less for G. (A vertex cover of size k for a graph G =...
Statement: For a given integer N, print all the squares of positive integers where the square...
Statement: For a given integer N, print all the squares of positive integers where the square is less than or equal to N, in ascending order. Programming Tasks: Prompt the user to input the value of N Output to the screen all squares of positive integers <= N Tests: Item Test 1 Test 2 Test 3 Inputs: 50 9 100 Outputs: 1 4 9 16 25 36 49 1 4 9 1 4 9 16 25 36 49 64 81...
In C++ Given a sorted list of integers, output the middle integer. A negative number indicates...
In C++ Given a sorted list of integers, output the middle integer. A negative number indicates the end of the input (the negative number is not a part of the sorted list). Assume the number of integers is always odd. Ex: If the input is: 2 3 4 8 11 -1 the output is: Middle item: 4 The maximum number of inputs for any test case should not exceed 9. If exceeded, output "Too many numbers". Hint: First read the...
in C++ Description: For a given integer n > 1, the smallest integer d > 1...
in C++ Description: For a given integer n > 1, the smallest integer d > 1 that divides n is a prime factor. We can find the prime factorization of n if we find d and then replace n by the quotient of n divided by d, repeating this until n becomes 1.             Write a program that determines the prime factorization of n in this manner, but that displays the prime factors in descending order. (When you find a...
Given a positive integer k and an array A[1..n] that contains the quiz scores of n...
Given a positive integer k and an array A[1..n] that contains the quiz scores of n students in ascending order, design a divide and conquer algorithm to efficiently count the number of students that have quiz scores in (100(i − 1)/k, 100i/k] for integers 1 ≤ i ≤ k. Let group i be the set of students with quiz scores in (100(i − 1)/k, 100i/k] for integers 1 ≤ i ≤ k. The counting result should be stored in G[1..k],...
Let An = {ai} n i=1 denote a list of n distinct positive integers. The median...
Let An = {ai} n i=1 denote a list of n distinct positive integers. The median mA of An is a value in An such that half the elements in An are less than m (and so, the other half are greater than or equal m). In fact, the median element is said to have a middle rank. (a) Develop an algorithm that uses Sorting to return mA given An. (6%) (b) Now assume that one is given another list...
ATestforPrimalityisthefollowing: Given an integer n > 1, to test whether n is prime check to see...
ATestforPrimalityisthefollowing: Given an integer n > 1, to test whether n is prime check to see if it is divisible by a prime number less than or equal to it’s square root. If it is not divisible by an of these numbers then it is prime. We will show that this is a valid test. prove ∀n, r, s ∈ N+, r s ≤ n → (r ≤ √n ∨ s ≤ √n) b) Prove ∀ n ∈ N+ ,...
Given an array A[1..n] of integers - all of whose numbers are in the range [0,...
Given an array A[1..n] of integers - all of whose numbers are in the range [0, n^3 − 1] give an algorithm which sorts them in O(n) time.
For a given integer n > 1, list all primes not exceeding n. Example: n=10, output:...
For a given integer n > 1, list all primes not exceeding n. Example: n=10, output: 2,3,5,7 n=16, output: 2,3,5,7,11,13 In Java please
C++ language or Python. Linked Lists You are given a linked list that contains N integers....
C++ language or Python. Linked Lists You are given a linked list that contains N integers. You are to perform the following reverse operation on the list: Select all the subparts of the list that contain only even integers. For example, if the list is {1,2,8,9,12,16}, then the selected subparts will be {2,8}, {12,16}. Reverse the selected subpart such as {8,2} and {16,12}. The list should now be {1,8,2,9,16,12}. Your node definition should consist of 2 elements: the integer value...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT