Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Here is a recursive program that prints all the possible ways that an amount x (in cents) can be made up using Australian coins (which

Here is a recursive program that prints all the possible ways that an amount x (in cents) can be made up using Australian coins (which come in 5, 10, 20, 50, 100, and 200 cent denominations). To avoid repetition, each possible decomposition is ordered.

change <- function(x, y.vec = c()) {

# finds possible ways of making up amount x using Australian coins

# x is given in cents and we assume it is divisible by 5

# y.vec are coins already used (so total amount is x + sum(y.vec))

if (x == 0) {

cat(y.vec, "")

} else {

coins <- c(200, 100, 50, 20, 10, 5)

new.x <- x - coins

new.x <- new.x[new.x >= 0]

for (z in new.x) {

y.tmp <- c(y.vec, x - z)

if (identical(y.tmp, sort(y.tmp))) {

change(z, y.tmp)

}

}

}

return(invisible(NULL))

}

Rewrite this program so that instead of writing its output to the screen it returns it as a list, where each element is a vector giving a possible decomposition of x.

Step by Step Solution

3.48 Rating (164 Votes )

There are 3 Steps involved in it

Step: 1

AN... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Probability And Statistics

Authors: Morris H. DeGroot, Mark J. Schervish

4th Edition

9579701075, 321500466, 978-0176861117, 176861114, 978-0134995472, 978-0321500465

More Books

Students also viewed these Finance questions