473,404 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

counting change algorithm

9
I need to write a recursive program to count the number of ways to make change from a given number of cents using quarters, dimes, and nickles. I cant figure out what the algorithm is - do you have any advice for me???
Feb 5 '08 #1
5 4990
Nepomuk
3,112 Expert 2GB
I need to write a recursive program to count the number of ways to make change from a given number of cents using quarters, dimes, and nickles. I cant figure out what the algorithm is - do you have any advice for me???
Do you only have to count the possible ways or do you have to list them as well? Here's a hint, on how to count them:

Expand|Select|Wrap|Line Numbers
  1. countQuarters(int cents)
  2. {
  3.    If "cents" can be paid with a quarter, add 1 to the amount of possibilities and reduce "cents" by the value of a quarter.
  4.    If there's the value of a quarter or more left, call "countQuarters" and
  5.    if there's the value of a dime or more left, call "countDimes" and
  6.    if there's the value of a nickle or more left, call "countNickles".
  7. }
  8.  
With that help, you should be able to write a recursive algorithm which does the job.
If you don't only have to count them, you'll have to print out, what you're doing in a suitable format.

Greetings,
Nepomuk
Feb 5 '08 #2
RedSon
5,000 Expert 4TB
Do you only have to count the possible ways or do you have to list them as well? Here's a hint, on how to count them:

Expand|Select|Wrap|Line Numbers
  1. countQuarters(int cents)
  2. {
  3.    If "cents" can be paid with a quarter, add 1 to the amount of possibilities and reduce "cents" by the value of a quarter.
  4.    If there's the value of a quarter or more left, call "countQuarters" and
  5.    if there's the value of a dime or more left, call "countDimes" and
  6.    if there's the value of a nickle or more left, call "countNickles".
  7. }
  8.  
With that help, you should be able to write a recursive algorithm which does the job.
If you don't only have to count them, you'll have to print out, what you're doing in a suitable format.

Greetings,
Nepomuk
Nepomuk,

This is borderline spoon feeding, but given the simplicity of the OPs task I can't really think of another way to put it. Maybe without the countQuarters method signature...I don't know. Ahh who cares, they should be able to figure it out easy enough anyway...right?

lol
Feb 5 '08 #3
Nepomuk
3,112 Expert 2GB
Nepomuk,

This is borderline spoon feeding, but given the simplicity of the OPs task I can't really think of another way to put it. Maybe without the countQuarters method signature...I don't know. Ahh who cares, they should be able to figure it out easy enough anyway...right?

lol
Believe me, I had much more than that and then reduced it as much as I could without making it completely worthless... But I also remember when I first used recursion and at that time, this task would have seemed quite tricky. Anyway, there's still work to do for the OP and he can hardly do that without understanding the algorithm, can he? ^^

Greetings,
Nepomuk
Feb 5 '08 #4
OCD
9
But how does this algorithm determine the number of ways? doesnt it just determine the least amount of coins needed? shouldnt the algorithm be
Number of ways to change amount A using N kinds of coins =
Number of ways to change amount A using all but the first
kind of coin
+
Number of ways to change amount A - D using all N kinds
of coins, where D is the denomination of the first kind of coin.
Feb 6 '08 #5
Nepomuk
3,112 Expert 2GB
But how does this algorithm determine the number of ways? doesnt it just determine the least amount of coins needed? shouldnt the algorithm be
Number of ways to change amount A using N kinds of coins =
Number of ways to change amount A using all but the first
kind of coin
+
Number of ways to change amount A - D using all N kinds
of coins, where D is the denomination of the first kind of coin.
Have another look at what I wrote and maybe think about how you would do the task without a computer - say you had 0.50$ and coins with the values 0.25$, 0.10$ and 0.01$ - how would you find all possibilities?

Greetings,
Nepomuk
Feb 6 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Paolino | last post by:
I have a self organizing net which aim is clustering words. Let's think the clustering is about their 2-grams set. Words then are instances of this class. class clusterable(str): def...
8
by: ben | last post by:
hello, i'm trying to answer a "prove an upper bound on the number of machine instructions required to process M connections on N objects" exercise from an algorithms book. i don't understand a...
29
by: Roy Gourgi | last post by:
Hi, I am new to C#. I have the same time scheduling program written in C++ and it is 5 times faster than my version in C#. Why is it so slow as I thought that C# was only a little slower than...
7
by: Pohihihi | last post by:
where can I get code/process/algorithm/or any kind of example to calculate what day of the week (or month or year) is say 5th of sep in 1767.
2
by: Benjamin Johnston | last post by:
Hi, I've found lots of discussion about the JavaScript garbage collector, but no clear answer to this question: Is the garbage collection algorithm used in JavaScript on ASP based on...
4
by: aaronfude | last post by:
Hi, Please consider the following class (it's not really my class, but it's a good example for my question): class Vector { int myN; double *myX; Vector(int n) : myN(n), myX(new double) { }...
4
by: bigbagy | last post by:
Notes The programs will be compiled and tested on the machine which runs the Linux operating system. V3.4 of the GNU C/C++ compiler (gcc ,g++) must be used. A significant amount coding is...
0
by: Chris Thomasson | last post by:
Here are the pre-alpha code downloads: http://appcore.home.comcast.net/vzoom/refcount/ (both C and C++ api here...) Here is some pre-alpha documentation: ...
12
by: psychofish25 | last post by:
I'm brand new to C++, yet I have experience in many other languages. This script I wrote prompts the user for a change amount, then calculates the amounts of each coin that amount is. For example, 37...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.