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

There are 3 Steps involved in it

Step: 1

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

Database Processing

Authors: David Kroenke

11th Edition

0132302675, 9780132302678

More Books

Students also viewed these Databases questions

Question

uyou are incharge of electricity transmission insurance consultant

Answered: 1 week ago

Question

2. Discuss the steps in preparing a manager to go overseas.

Answered: 1 week ago