Change calculator

Page 1 of 1

4 Replies - 8733 Views - Last Post: 03 March 2010 - 10:43 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=159673&amp;s=8f4a6445b2981a044c1b836b0481e9b6&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 [email protected]

Reputation: 1
• Posts: 6
• Joined: 03-March 10

Change calculator

Posted 03 March 2010 - 09:28 PM

Hey guys, so I'm having a little problem with an assignment. So this is a homework lab that requires me to write a method that uses a (ref int) so that I can return to values. The basic idea of the program is to take a number from 1-99 and return how many halves, quarters, dimes, nickels and pennies it will take. So if you say 75 it will say:
1 halves
1 quarter
0 dimes
0 nickels
0 pennies
My problem is that with the method that I wrote if i put in say 30 it will give me an output of:
1 halves
1 quarters
0 dimes
0 nickels
0 pennies
As I step through my code I see that for halves even though it doesn't get any it will still give the first (1) and output it to the halves spot. What am I missing? Any hints?

Also most of the code was provided for us the only part we needed to fill in was the method.

```using System;

class Program
{
// some  class level constants
const int HALVES = 50;
const int QUARTERS = 25;
const int DIMES = 10;
const int NICKELS = 5;
const int PENNIES = 1;

static void Main()
{

int money;  // the value we want to count change for

Console.WriteLine("I will make change for you.");
Console.Write("Enter in an amount between 1 and 99: ");

Console.WriteLine("For {0} you get:", money);

Console.WriteLine("{0} halves", ComputeChange(ref money, HALVES) );
Console.WriteLine("{0} quarters", ComputeChange(ref money, QUARTERS) );
Console.WriteLine("{0} dimes", ComputeChange(ref money, DIMES) );
Console.WriteLine("{0} nickels", ComputeChange(ref money, NICKELS) );
Console.WriteLine("{0} pennies\n", ComputeChange(ref money, PENNIES) );
}

// The ComputeChange Method
// you provide the method to compute change in the space below
// Purpose: This method figures out the amount of coins you need starting largest to smallest,
// Inputs: The method takes two imputs changeValue and coinValue.
// Returns: Coins needed
// Preconditions: Start at the largest coin (hald dollar) work down to smalles (penny).
// Postconditions: The return value must be the full change amount in coins.
// -----------------------------------------------------------
static int ComputeChange(ref int changeValue, int coinValue)
{
//compute the .50 pieces
if (changeValue >= HALVES)
{
coinValue = changeValue / HALVES;

//compute new value after .50 pieces
changeValue = changeValue - (coinValue * HALVES);
}
//compute the .25 pieces
else if (changeValue >= QUARTERS)
{
coinValue = changeValue / QUARTERS;
//compute the value after .25 pieces
changeValue = changeValue - (coinValue * QUARTERS);
}
//compute the .10 pieces
else if (changeValue >= DIMES)
{
coinValue = changeValue / DIMES;
//compute the value after .10 pieces
changeValue = changeValue - (coinValue * DIMES);
}
//compute the .05 pieces
else if (changeValue >= NICKELS)
{
coinValue = changeValue / NICKELS;
//compute the value after .05 pieces
changeValue = changeValue - (coinValue * NICKELS);
}
//compute the .01 pieces
else
{
coinValue = changeValue / PENNIES;
}
//compute the value after .01 pieces
return coinValue;

}
}

```

Is This A Good Question/Topic? 0

Replies To: Change calculator

#2 rs4

Reputation: 29
• Posts: 153
• Joined: 01-February 09

Re: Change calculator

Posted 03 March 2010 - 10:19 PM

Well firstly it is kind of weird passing a coinVaule to ComputeChange and not using it. Maybe you should use coinValue to decide which of your if statements should be run. Also look at your else's and think about what they are doing in the current code.

How you got that output for 30 is because the first time you call ComputeChange the amount is under Halves so the quarters come out (and so on), which is why you need to check against coinValue passed.

#3 [email protected]

Reputation: 1
• Posts: 6
• Joined: 03-March 10

Re: Change calculator

Posted 03 March 2010 - 10:26 PM

Thanks, I do realize why the output was getting messed up. Are you saying that in addition to (changeValue >= HALVES) i need to add something with the coinValue?

#4 rs4

Reputation: 29
• Posts: 153
• Joined: 01-February 09

Re: Change calculator

Posted 03 March 2010 - 10:39 PM

You don't even need to check if change value is greater than HALVES. Instead just check if you want to return HALVES. So change 'if(changeValue >= HALVES)'
to 'if(coinValue == HALVES)' for all.

#5 [email protected]

Reputation: 1
• Posts: 6
• Joined: 03-March 10

Re: Change calculator

Posted 03 March 2010 - 10:43 PM

Oo ... it all makes sense now lol, I always miss something like that. Thanks very much!