Question

Coin Change Problem) Given a list of positive integers c[0...n − 1], and a positive integer...

Coin Change Problem) 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.
(c) (Extra credit): how to make the function prints out the numbers that are used to make the
change?
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
*/
bool CoinChange (c[0...n-1], v)
{
if (v==0)
//fill in the blank here
for i=0 to n-1:
2
if (c[i]==v)
//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;
}

Homework Answers

Answer #1

C++ Implementation of the required pseudocode.

#include<bits/stdc++.h>
using namespace std;
bool CoinChange(vector<int> coins,int v)
{
    int i;
    if(v == 0)          // When amount of 0 is to be made return TRUE as it can be made by discarding all coins.
        return true;
    for(i=0;i<coins.size();i++) // For each coin in the vector check the inside conditions
    {
        if(coins[i] == v)       // If amount to be made is equal to coins[i] then just by selecting this coin
            return true;        // required change is made so return TRUE.
        else if(coins[i] < v)
        {                                       // If coins[i] is less than the amount select the coins[i] and then
            if(CoinChange(coins,v - coins[i]))  // check if rest amount i.e. v - coins[i] is possible or not.
                return true;
        }
    }
    return false;               // If by all denominations value the given amount is not formed then return FALSE.
}
int main()
{
    vector<int> coins = {3,4,6,10}; // Test vector
    cout<<CoinChange(coins,14);     // Test function call
    return 0;
}

A recursive call tree for the given example is drawn here.

We can add an auxiliary vector in the function parameter and store the coin denomination that we are accepting at each step and finally print the auxiliary vector if function return true.

Know the answer?
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
Similar Questions
question: Complete the function remove number such that given a list of integers and an integer...
question: Complete the function remove number such that given a list of integers and an integer n, the function removes every instance of n from the list. Remember that this function needs to modify the list, not return a new list. please edit the Python form without errors def remove_number(lst: List[int], number: int) -> None:     """         Remove every instance of number in lst. Do this         *in-place*, i.e. *modify* the list. Do NOT         return a new list....
a. Find an algorithm for solving the following problem: Given a positive integer n, find the...
a. Find an algorithm for solving the following problem: Given a positive integer n, find the list of positive integers whose product is the largest among all the lists of positive integers whose sum is n. For example, if n is 4, the desired list is 2, 2 because 2 × 2 is larger than 1 × 1 × 1 × 1, 2 × 1 × 1, and 3 × 1. If n is 5, the desired list is 2,...
/*C PROGRAMMING: HOW TO INSERT ERROR BELOW CODE? QUESTION: This program reads integers from standard input....
/*C PROGRAMMING: HOW TO INSERT ERROR BELOW CODE? QUESTION: This program reads integers from standard input. The first integer indicates the number of values that will follow. Read that many values, and return their sum, ignoring any additional values that may follow. However, if there are fewer integers than specified in the input, print "Error" and terminate. Hint: int n, success; ... success = scanf("%d", &n); // FIRST INTEGER INPUT reads an integer from stdin, returning 1 if the integer...
getting an error for the code below when attempting to compile . Not fully sure what...
getting an error for the code below when attempting to compile . Not fully sure what the error means and confused why I am getting the error. I know there are a of other logical errors but im just looking for the solution to this specific one right now. /tmp/ccq2rdty.o: In function `main': IntArrayPlay.cpp:(.text+0x42): undefined reference to `fillArray(int*, int&)' IntArrayPlay.cpp:(.text+0x55): undefined reference to `displayArray(int*, int&)' IntArrayPlay.cpp:(.text+0x114): undefined reference to `displayArray(int*, int&)' IntArrayPlay.cpp:(.text+0x15f): undefined reference to `displayArray(int*, int&)' collect2: error: ld...
from typing import List def longest_chain(submatrix: List[int]) -> int: """ Given a list of integers, return...
from typing import List def longest_chain(submatrix: List[int]) -> int: """ Given a list of integers, return the length of the longest chain of 1's that start from the beginning. You MUST use a while loop for this! We will check. >>> longest_chain([1, 1, 0]) 2 >>> longest_chain([0, 1, 1]) 0 >>> longest_chain([1, 0, 1]) 1 """ i = 0 a = [] while i < len(submatrix) and submatrix[i] != 0: a.append(submatrix[i]) i += 1 return sum(a) def largest_rectangle_at_position(matrix: List[List[int]], x:...
1. Let n be an odd positive integer. Consider a list of n consecutive integers. Show...
1. Let n be an odd positive integer. Consider a list of n consecutive integers. Show that the average is the middle number (that is the number in the middle of the list when they are arranged in an increasing order). What is the average when n is an even positive integer instead? 2. Let x1,x2,...,xn be a list of numbers, and let ¯ x be the average of the list.Which of the following statements must be true? There might...
It is N queens problem please complete it use this code //*************************************************************** // D.S. Malik //...
It is N queens problem please complete it use this code //*************************************************************** // D.S. Malik // // This class specifies the functions to solve the n-queens // puzzle. //*************************************************************** class nQueensPuzzle { public: nQueensPuzzle(int queens = 8);     //constructor     //Postcondition: noOfSolutions = 0; noOfQueens = queens;     // queensInRow is a pointer to the array     // that store the n-tuple.     // If no value is specified for the parameter queens,     // the default value, which is 8, is assigned to it. bool...
Given the following function:      int C(int n, int k)                  {              
Given the following function:      int C(int n, int k)                  {                     if (k= =0) return 1;                        else return (C(n-1, k-1) * n)/k;                                       } What type of function is this? Recursive or Iterative. Explain your answer.
Python, smallestFactor() 1. #Given an integer n (which you can assume is positive), # return the...
Python, smallestFactor() 1. #Given an integer n (which you can assume is positive), # return the largest factor of n less than n. # If n is 1, then you should return 1. def largestFactor(n): # TO DO: Finish function return
II. Answer part A, B, and C. Remember that 0 is not a positive value! 2a)...
II. Answer part A, B, and C. Remember that 0 is not a positive value! 2a) Return true if at least one parameter value is positive, and false otherwise. atLeastOnePositive(1, 0, 3) → true atLeastOnePositive(-10, 0, -4) → false atLeastOnePositive(3, -2, -4) → true 2b) Return true if exactly one parameter value is positive, and false otherwise. exactlyOnePositive(1, 0, 3) → false exactlyOnePositive(-10, 0, -4) → false exactlyOnePositive(3, -2, -4) → true 2c) Return the middle of three integer values....
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT