Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 12-17-2008, 10:43 PM   #1 (permalink)
Black.Ice.'s Avatar
 
Junior Techie

Join Date: Aug 2007

Location: Boston, MA

Posts: 54

Black.Ice. is on a distinguished road

Send a message via AIM to Black.Ice. Send a message via MSN to Black.Ice.
Default Need Help with Program

Hello all. Im taking AP Comp Sci and I'm little stuck on one of the programs we were assigned.

The program basically takes a user input of cents and converts that into all possible combinations of quarters, dimes, nickels and pennies. It then prints out the total number of possible configurations.

For example:

Code:
How Many Cents: 6
6 cents = 0 quarters + 0 dimes + 0 nickels + 6 pennies
6 cents = 0 quarters + 0 dimes + 1 nickel + 1 pennies
There are 2 possible ways to make 6 cents using coins.
Here is what I have so far:

Code:
import java.util.Scanner;

public class MakeChange 
{

  public static void main (String[] args)
  {
     Scanner input = new Scanner(System.in);
     System.out.println ("How many cents? ");
     int cents = input.nextInt();
     
     int quarters = 0;
     int dimes = 0; 
     int nickels = 0; 
     int pennies = 0;
     
     while (cents > 0)
     	
     	if (cents >= 25)
     	{
     	  quarters++; 
     	  cents -= 25;
     	}
     
        else if (cents >= 10)
        {
          dimes++;
          cents -= 10;
        }
        
        else if (cents >= 5)
        {
          nickels++;
          cents -= 5 * nickels;
        }
        
        else if (cents >= 1) 
        {
          pennies++;
          cents -= 1;
        }
  
     System.out.println(cents + " cents = " + quarters + " quarters + " + dimes + " dimes + " + nickels + " nickels + " + pennies + " pennies");
  }
}
However, I know this is wrong because it only prints out the combination with the least amount of coins. It is supposed to print ALL possible combinations. Can anyone help?

Also, Im supposed to be using nested loops for these.

Thanks Again.
__________________
$500 rig ftw:

Case: CM 690 Black ATX Mid Tower
Mobo ASUS M2N32-SLI Deluxe AM2 nForce 590 SLI
CPU: AMD Athlon 64 X2 6400+ Black Edition @ 3.2GHz 23 C idle 37 load :O
GFX: 8600 GT XXX 256 mb
RAM: Corsair XMS2 2GB DDR2 800 mhz PC2 6400
HDD: Seagate Barracuda ST3320620AS 320GB 7200 RPM w/ Slave Western Digital Caviar WD800BB 80GB 7200 RPM
HSF: Zalman CNPS9700 LED 110mm 2 Ball
PSU: CM eXtreme Power Dual 12V rails @ 36A 650W

Tee Hee!
Black.Ice. is offline  
Old 12-18-2008, 01:26 AM   #2 (permalink)
 
Newb Techie

Join Date: Dec 2008

Posts: 13

phpGuy is on a distinguished road

Default Re: Need Help with Program

here this is a general algorithm that should get you close
since it is an assignment I will not give you the actual code.

Quote:
//make sure this is integer division
intQuartCase = cents / 25

for each quarter case
//starting with most quarters first
quarters = intQuartCase - quarter loop counter

//calculate how many cents are left using this number of quarters
qNewCents = cents - ( 25 * quarters )

//now repeat for dimes
intDimeCase = qNewCents / 10
for each dime case
dimes = intDimeCase - dime loop counter
dNewCents = qNewCents - (10 * dimes)
//now repeat for nickels
//now pennies equals dNewCents - ( 5 * nickels ) because after calculating the nickels
// all that is left is the pennies
//before exiting the nickels for loop add one to a counter
//print quarters dimes nickels pennies
//end nickels loop
//end dimes loop
//end quarters loop
//print number of possibilities using counter mentioned in nickels loop


Last edited by phpGuy; 12-18-2008 at 01:27 AM. Reason: indentation is off
phpGuy is offline  
Old 12-18-2008, 05:27 AM   #3 (permalink)
 

Join Date: Jul 2005

Location: England

Posts: 2,041

kmote has a spectacular aura aboutkmote has a spectacular aura about

Default Re: Need Help with Program

What a horrible project. With 6 cents well that's OK but what about 80 cents. This would have to display
80 cents,
(80 cents - theSizeOfYourNextDenomination) 1
(80 cents - 2* theSizeOfYourNextDenomination) 2
...
yawn

I hate pointless programs

EDIT: And php, while I remember, why the aversion to mod?
__________________
MSI P43 Neo|Enermax Pro82+ 425W|E5200|silent 8500GT|250GB Samsung spinpoint F1|Samsung SATA DVD RW|4GB Corsair|Antec SOLO|openSUSE11


There are in order of increasing severity: lies, darn lies, statistics, and computer benchmarks. - diskinfo man page

Last edited by kmote; 12-18-2008 at 05:29 AM.
kmote is offline  
Old 12-18-2008, 01:38 PM   #4 (permalink)
 
Newb Techie

Join Date: Dec 2008

Posts: 13

phpGuy is on a distinguished road

Default Re: Need Help with Program

for the record make sure in each case include the zero case.

and kmote, I don't think mod would work in this case. It should actually be the floor( cents / 25 )...I am not sure of how java handles integer division. But back to mod...if cents is 50 then then 50 mod 25 = 0. Doesn't really tell you much except that 50 can be evenly divided by 25. If that is not what you are talking about then I am not sure where you would use it.
phpGuy is offline  
Old 12-18-2008, 07:15 PM   #5 (permalink)
 

Join Date: Jul 2005

Location: England

Posts: 2,041

kmote has a spectacular aura aboutkmote has a spectacular aura about

Default Re: Need Help with Program

Please note this is not the way I would do it, just an example

cents = getUserInput()

quarters = cents/25
theRest = cents%25

dimes = theRest/10
theRest = theRest%10

Also please excuse me if I'm being a fool and missing something obvious. I've not got nearly enough sleep as of late.
__________________
MSI P43 Neo|Enermax Pro82+ 425W|E5200|silent 8500GT|250GB Samsung spinpoint F1|Samsung SATA DVD RW|4GB Corsair|Antec SOLO|openSUSE11


There are in order of increasing severity: lies, darn lies, statistics, and computer benchmarks. - diskinfo man page
kmote is offline  
Old 12-19-2008, 02:25 AM   #6 (permalink)
 
Newb Techie

Join Date: Dec 2008

Posts: 13

phpGuy is on a distinguished road

Default Re: Need Help with Program

quarters = cents/25
theRest = cents%25

doesn't work for every iteration.

if the user inputs 55 cents it works for 2 quarters but how do you account for only one quarter?
phpGuy is offline  
Old 12-19-2008, 10:52 PM   #7 (permalink)
Black.Ice.'s Avatar
 
Junior Techie

Join Date: Aug 2007

Location: Boston, MA

Posts: 54

Black.Ice. is on a distinguished road

Send a message via AIM to Black.Ice. Send a message via MSN to Black.Ice.
Default Re: Need Help with Program

Thanks all for the help. I got the program working a few days ago.

Code:
/**
 * This program takes a user input of money in cents and prints all the possible
 * representations of that amount as a combination of quarters, deimes, nickels, 
 * and pennies. It then displays the total number of possible configurations. 
 *
 */

import java.util.Scanner;

public class MakeChange 
{ 
  public static void main (String[] args)
  {
    Scanner input = new Scanner(System.in);
    System.out.println ("How many cents? ");    
    int cents = input.nextInt();            // User input of cents
    
    int counter = 0;   // declares the variable counter which keeps track of total configurations
   
    for(int quarters = 0; quarters <= cents / 25; quarters++)   // finds number of quarters
    {
      int centsAfterQuarters = cents - (quarters * 25);
            
      for(int dimes = 0; dimes <= centsAfterQuarters / 10; dimes++)     // finds number of dimes
      {
        int centsAfterDimes = centsAfterQuarters - (dimes * 10);
         
        for(int nickels = 0; nickels <= centsAfterDimes / 5; nickels ++)     // finds number of nickels
        {
          int centsAfterNickels = centsAfterDimes - (nickels * 5);            // remaining cents are the pennies         
          counter++;           //increments number of configurations after each iteration by 1
          
          System.out.println(cents + " cents = " + quarters + " quarters + " + dimes + " dimes + " + nickels + " nickels + " + centsAfterNickels + " pennies");                 
        }
      } 
    }
    System.out.println("There are " + counter + " possible ways to make " + cents + " cents using coins.");
  } 
}

__________________
$500 rig ftw:

Case: CM 690 Black ATX Mid Tower
Mobo ASUS M2N32-SLI Deluxe AM2 nForce 590 SLI
CPU: AMD Athlon 64 X2 6400+ Black Edition @ 3.2GHz 23 C idle 37 load :O
GFX: 8600 GT XXX 256 mb
RAM: Corsair XMS2 2GB DDR2 800 mhz PC2 6400
HDD: Seagate Barracuda ST3320620AS 320GB 7200 RPM w/ Slave Western Digital Caviar WD800BB 80GB 7200 RPM
HSF: Zalman CNPS9700 LED 110mm 2 Ball
PSU: CM eXtreme Power Dual 12V rails @ 36A 650W

Tee Hee!
Black.Ice. is offline  
Old 01-08-2009, 01:46 PM   #8 (permalink)
StealthTools's Avatar
 
Hardware/VB.net/Anti-vir

Join Date: Mar 2008

Location: TN - Intel, Samsung, and NVIDIA 4 EvR!

Posts: 515

StealthTools is on a distinguished road

Send a message via MSN to StealthTools
Default Re: Need Help with Program

I had to do this exact project for QBasic, VB.NET, and C++!
StealthTools is offline  
Old 01-16-2009, 08:17 PM   #9 (permalink)
 
Newb Techie

Join Date: Jan 2009

Posts: 9

darkender is on a distinguished road

Default Re: Need Help with Program

Ya I did very similar stuff in Visual Basic too
darkender is offline  
Old 01-16-2009, 08:21 PM   #10 (permalink)
Baez's Avatar
 

Join Date: Sep 2005

Location: Toronto, Canada

Posts: 5,424

Baez is a glorious beacon of lightBaez is a glorious beacon of lightBaez is a glorious beacon of lightBaez is a glorious beacon of lightBaez is a glorious beacon of light

Default Re: Need Help with Program

Alright let this thread die guys. He's solved his problem now.
__________________

Baez is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
BFG Announces Graphics Card 100 Day Trade Up Program Osiris Other Computer HW Topics 5 03-18-2008 08:34 PM
Join The Windows Feedback Program and grab some free apps Osiris Windows Operating Systems and Software 4 12-14-2007 05:45 PM
friends log Static_11 HijackThis Logs (finished) 11 11-25-2007 10:27 PM
Aim program, not aim Max Power Windows Operating Systems and Software 3 10-31-2007 01:53 AM