Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 131,637 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,337 people online right now. Registration is fast and FREE... Join Now!




How Many ways can you break a dollar?

 
Reply to this topicStart new topic

How Many ways can you break a dollar?

wizardsfan0608
post 6 May, 2008 - 11:58 AM
Post #1


New D.I.C Head

*
Joined: 6 May, 2008
Posts: 3

I understand the logic of how to solve the problem, but I can't figure out how to write a code for it. Please help
User is offlineProfile CardPM

Go to the top of the page


Amadeus
post 6 May, 2008 - 12:01 PM
Post #2


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,162



Thanked 32 times

Dream Kudos: 25
My Contributions


Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Post your code like this: code.gif

Thanks.
User is online!Profile CardPM

Go to the top of the page

gabehabe
post 6 May, 2008 - 12:05 PM
Post #3


Working Girl.

Group Icon
Joined: 6 Feb, 2008
Posts: 5,377



Thanked 94 times

Dream Kudos: 2625

Expert In: Dingleberries

My Contributions


There was a post like this recently, you might be able to work from that - try searching the forum for cash dispenser
User is offlineProfile CardPM

Go to the top of the page

wizardsfan0608
post 6 May, 2008 - 12:08 PM
Post #4


New D.I.C Head

*
Joined: 6 May, 2008
Posts: 3

The assignment actually has two parts, the first part is to provide the most efficient way to provide a change for x amount. Then the second part is to tell how many different ways to make that change.

I've completed the first part an I'm just having problems starting the second part.

CODE


#include <iostream>
using namespace std;

int main()

{
    //Declare Money Array and Money Quantity Array
    int money[12] = {10000, 5000, 2000, 1000, 500, 200, 100, 50, 25, 10, 5, 1};
    char name[12][20] = {"$100", "$50", "$20", "$10", "$5", "$2", "$1", "Half Dollar", "Quarter", "Dime", "Nickel", "Penny"};
    int quantity[12];
    int quantity2[12];

    //Declare other Variables
    double change, change1, change2;
    int index, index2, num_ways, test;

    //Ask user for change amount
    cout << "How much change did you get? ";
    cin >> change;
    cout << " " << endl;

    //Display the results
    cout << "The most efficient way to provide the change amount $" << change
        << " is:" << endl << endl;

    change1 = change * 100;


    //Loop
    for (index = 0; index < 12; index++)
    {
        quantity[index] = 0;
        while (change1 >= money[index])
        {
            quantity[index]++;
            change1 = change1 - money[index];
        }
        if (quantity[index] > 0)
            cout << quantity[index] << " " << name[index] << endl;
    }

    cout << endl << endl;


    // PART 2 - Provide the number of different ways to provide change if it is $10 or less
    cout << "************PART 2************" << endl << endl;
    cout << endl << "If your change is $10 or less," << endl
         << "I will show you the number of different ways you can get your change."
         << endl << endl;
    cout << "Change is " << change << endl;

    change2 = change * 100;
    
    if (change > 10)
        cout << "Your change is greater than $10." << endl;

    else if (change == 0)
        cout << "You don't have any change." << endl;

    else
    {
        num_ways = 1;    //Number of Ways to break the change
        test = 0;        //Test to see if we need to keep going


        for (index = 10; index > 2; index--)
        {
            while (quantity[index] > 0)
            {
                change2 = money[index];
                for (index2 = 3; index2 < 12; index2++)
                {
                    change2 = change2 - money[index];
                    quantity2[index]++;
                }
                quantity[index]--;
            }
        }

    
        cout << "There are " << num_ways << " different ways to provide the change amount of $"
             << change << "." << endl;

    }

    return 0;
}
User is offlineProfile CardPM

Go to the top of the page

jeronimo0d0a
post 6 May, 2008 - 12:50 PM
Post #5


D.I.C Head

**
Joined: 3 Mar, 2008
Posts: 141


My Contributions


There are so many ways to do the change it's going to take some work. For example, you could give 89 cents as 89 pennies, 84 pennies and one nickel, 79 pennies and two nickels, 74 pennies and 3 nickels and so on. If you have change as $10, it gets crazy. the logic is fairly simple, but the code is tedious so I would ask for clarification of the assignment.
Keep at it.
User is offlineProfile CardPM

Go to the top of the page

wizardsfan0608
post 6 May, 2008 - 06:46 PM
Post #6


New D.I.C Head

*
Joined: 6 May, 2008
Posts: 3

I worked on it a bit more and here's what I have so far. It's getting close, but I'm still not getting the correct answers. Again, the program has 2 parts, Part 1 is done and working alright. I just need you guys to look at Part 2, where I have to output the number of different ways to provide a certain change.
So if the user inputs 10cents, the output should be 4 (10pennies, 1 dime, 2 nickels, 1nickel and 5pennies)
or if the user inputs 25cents, it should output 13

Here's the code and I appreciate any help
CODE


#include <iostream>
using namespace std;

int calculate(int);

int num_ways = 1;    //Number of Ways to break the change
int num_ways2 = 0;    //num_ways used in the recursive case
int index, index2, num, test;

//Declare Money Array and Money Quantity Array
int money[12] = {10000, 5000, 2000, 1000, 500, 200, 100, 50, 25, 10, 5, 1};
char name[12][20] = {"$100", "$50", "$20", "$10", "$5", "$2", "$1", "Half Dollar", "Quarter", "Dime", "Nickel", "Penny"};
int quantity[12];
int quantity2[12];

//Declare other Variables
double change, change1, change2;

int main()

{


    //Ask user for change amount
    cout << "How much change did you get? ";
    cin >> change;
    cout << " " << endl;

    //Display the results
    cout << "The most efficient way to provide the change amount $" << change
        << " is:" << endl << endl;

    change1 = change * 100;


    //Loop
    for (index = 0; index < 12; index++)
    {
        quantity[index] = 0;
        while (change1 >= money[index])
        {
            quantity[index]++;
            change1 = change1 - money[index];
        }
        if (quantity[index] > 0)
            cout << quantity[index] << " " << name[index] << endl;
    }

    cout << endl << endl;


    // PART 2 - Provide the number of different ways to provide change if it is $10 or less
    cout << "************PART 2************" << endl << endl;
    cout << endl << "If your change is $10 or less," << endl
         << "I will show you the number of different ways you can get your change."
         << endl << endl;
    cout << "Change is " << change << endl;

    change2 = change * 100;
    
    if (change > 10)
        cout << "Your change is greater than $10." << endl;

    else if (change == 0)
        cout << "You don't have any change." << endl;

    else
    {
        test = 1;
        while (test > 0)
        {
            test = 0;
            for (index = 10; index > 2; index--)
            {
                while (quantity[index] > 0)
                {
                    change2 = quantity[index] * money[index];
                    calculate(quantity[index]);
                    num_ways = num_ways + (num_ways * num_ways2);
                    test = test + quantity[index];
                    quantity[index]--;
                }
            }
        }

    
        cout << "There are " << num_ways << " different ways to provide the change amount of $"
             << change << "." << endl;

    }

    return 0;
}


// START RECURSION
int calculate(int num)
{
    if  (num == 0)
        return 1;
    else
    {
        while (change2 >= money[index + 1])
        {
            quantity[index + 1]++;
            change2 = change2 - money[index + 1];
        }
        num_ways2++;
        return (num - 1);
    }

}
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/20/08 05:56AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month