Ask Question

Write a function [change, flag] = makeChangeRecursive (cost, paid) that takes the cost of an item cost (a 1x1 double) and the amount paid by the buyer paid (a 1x1 double), and returns a n x 1 column array of doubles change containing the bills that the seller should give back to the buyer in decreasing order and flag a 1x1 logical true or false saying whether the transaction was "completed". If paid is less than cost, flagis false and you should display a warning in the command window saying "That's not enough to buy that item." and change is assigned as an empty array. If cost and paid are equal the change is assigned as an empty array and flag is true. If paid is greater than cost, flag is true and change is the difference between the two made by combining the denominations: 100, 50, 20, 10, 5, 2, and 1.

+5
Answers (1)
  1. 15 January, 06:07
    0
    The answer to this question can be given as:

    function [change, flag]=makeChangeRecursive (cost, paid)

    //define function

    change=[];

    flag=false; / /use flag for true or false.

    if ((paid-cost) >=100) / /checking condition value greater then equal to 100

    [m, f]=makeChangeRecursive (cost, paid-100);

    change=[100; m];

    flag=true;

    return;

    elseif ((paid-cost) >=50) / /checking condition value greater then equal to 50

    [m, f]=makeChangeRecursive (cost, paid-50); / /holding value.

    change=[50; m];

    flag=true;

    return;

    elseif ((paid-cost) >=20) / /checking condition value greater then equal to 20

    [m, f]=makeChangeRecursive (cost, paid-20);

    change=[20; m];

    flag=true;

    return;

    elseif ((paid-cost) >=10) / /checking condition value greater then equal to 10

    [m, f]=makeChangeRecursive (cost, paid-10);

    change=[10; m];

    flag=true;

    return;

    elseif ((paid-cost) >=5) / /checking condition value greater then equal to 5

    [m, f]=makeChangeRecursive (cost, paid-5);

    change=[5; m];

    flag=true;

    return;

    elseif ((paid-cost) >=2) / /checking condition value greater then equal to 2

    [m, f]=makeChangeRecursive (cost, paid-2);

    change=[2; m];

    flag=true;

    return;

    elseif ((paid-cost) >=1) / /checking condition value greater then equal to 1

    [m, f]=makeChangeRecursive (cost, paid-1);

    change=[1; m];

    flag=true;

    return;

    elseif ((paid-cost) <0) / /checking condition value less then 0

    warning ('That''s not enough to but an item.'); / /print message

    flag=false;

    return;

    elseif (paid==cost) / /checking condition if value is equal.

    flag=1; / /falg value.

    change=[];

    return;

    end

    end / /end function

    Explanation:

    In the above program code the function must use the recursion to change the variable. This function run in 4 case i. e. case 1 In this case when we pass the value [change, flag]=makeChangeRecursive (2,100) in the function so the change value will be 50,20,20,5,2,1, and flag=,1. In case 2 when we pass the value (3,20) in the function so the change value will be 10,5,2, and flag=,1. In case 3 when we pass the value (20,20) in the function so the change value will be change = {}, flag = 1. In case 4 when we pass the value (59,20) in the function so the change value will be warning message i. e That''s not enough to but an item.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Write a function [change, flag] = makeChangeRecursive (cost, paid) that takes the cost of an item cost (a 1x1 double) and the amount paid ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers