Thread: Change Machine Problem

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    1

    Change Machine Problem

    In my introduction to computer programming course i was a given a project asking to create a change maker. Specifically, i must write a program that will make change in coins for any amount up to 99 cents using the fewest possible coins. The program should prompt the user to enter an amount, and then print out the number of each type of coin to make that amount of change. If there are no coins of a particular type, then the program should not print a line for that coin.

    Examples:

    Enter the change amount: 93
    Quarters: 3
    Dimes: 1
    Nickels: 1
    Pennies: 3

    Enter the change amount: 89
    Quarters: 3
    Dimes: 1
    Pennies: 4


    This is what i have so far. The program works unless the "change" value falls under 25 so i'm assuming the problem involves the "if" statements. I have been stuck for a while and was hoping someone could provide some suggestions.

    Code:
    #include <stdio.h>
    main ()
    {
    int change;
    int coins;
    int remainder;
    
    printf ("Please enter an amount of change from 0-99.\n");
    scanf ("%d", &change);
    
    if (change>=25)
    {
    coins = change/25;
    printf ("Quarters: %d\n", coins);
    remainder = change%25;
    }
    
    if (remainder>=10)
    {
    coins = remainder/10;
    printf ("Dimes: %d\n", coins);
    remainder = remainder%10;
    }
    
    if (remainder>=5)
    {
    coins = remainder/5;
    printf ("Nickles: %d\n", coins);
    remainder = remainder%5;
    }
    
    if (remainder>0)
    {
    printf ("Pennies: %d\n", remainder);
    }
    
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Suppose change == 24. What is the value of remainder at the end of the main function?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    'remainder' is never initialized if the original amount is < 25.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c programm slot machine change
    By schugh in forum C Programming
    Replies: 2
    Last Post: 05-17-2011, 05:23 PM
  2. Replies: 4
    Last Post: 01-18-2008, 07:05 PM
  3. problem with connecting to my own machine
    By EvBladeRunnervE in forum Networking/Device Communication
    Replies: 10
    Last Post: 07-24-2003, 04:16 PM
  4. IDEA: A Slot Machine (aka a fruit machine)
    By ygfperson in forum Contests Board
    Replies: 0
    Last Post: 08-12-2002, 11:13 PM
  5. A change.... a problem!
    By CodeMonkey in forum C++ Programming
    Replies: 2
    Last Post: 03-15-2002, 10:16 PM