Change calculator

I need a little help with the last part of my change calculator.

Page 1 of 1

4 Replies - 2427 Views - Last Post: 03 March 2010 - 10:43 PM Rate Topic: -----

#1 kcsoccer52@yahoo.com  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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: ");
	    money = int.Parse(Console.ReadLine( ) );
	
    	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) );	
	    Console.ReadLine( );
}

    // 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  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 29
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 kcsoccer52@yahoo.com  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#4 rs4  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 29
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 kcsoccer52@yahoo.com  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1