Description of the Problem:

In my program, i need to write a program that will determine how many coins of each denomination (1¢, 5¢, 10¢, 25¢, and 50¢) are required to make up a given amount of change. For example, to make 57¢ we could use 1 half dollar, 1 nickel, and 2 pennies.

Given an amount of change to make, it should be obvious that many combinations of coins are possible. In the example above, we could make up 57¢ with: 57 pennies; 5 dimes, a nickel, and 2 pennies; 2 quarters and 7 pennies; and so on. In this program I need to use an algorithm that figures out how many of teh largest denomination coin i need first, and works down through each succeeding coin demonination.

Alright, I have been looking over this program for awhile now and I cant seem to get my function right. I have ran over what I had so far with my professor and he said that it is all correct except for my function that i built, so he deleted it and told me to start fresh. I was wondering if someone could point me into the right direction to compute my function for this code to run properly.

HERE IS MY CODE:

// This program will make change
#include <iostream>
#include <cassert>
using namespace std;

// The function prototype
int computeChange(int& changeValue, int coinValue);

// some constants
const int HALVES = 50;
const int QUARTERS = 25;
const int DIMES = 10;
const int NICKELS = 5;
const int PENNIES = 1;
const string HALF = "halves";
const string QUARTER = "quarters";
const string DIME = "dimes";
const string NICKEL = "nickels";
const string PENNY = "pennies";

int main ( )
{
int money; // the value i want to count change for

cout << "\nI will make change for you.";
cout << "\nEnter in an amount between 1 and 99: ";
cin >> money;

cout << "\nFor " << money << " you get:";

cout << "\n" << computeChange(money, HALVES) << " " << HALF;
cout << "\n" << computeChange(money, QUARTERS) << " " << QUARTER;
cout << "\n" << computeChange(money, DIMES) << " " << DIME;
cout << "\n" << computeChange(money, NICKELS) << " " << NICKEL;
cout << "\n" << computeChange(money, PENNIES) << " " << PENNY; 
cout << endl;

system("PAUSE");
return 0;
}

int computeChange(int& changeValue, int coinValue)
{

// THIS IS WHERE I NEED HELP???? CAN ANYONE HELP ME?? THANKS

}

You will notice that the code is suppose to do the same thing over and over again, but with different values for n and for the coin denomination. In this case, i want a function that takes as its inputs n, the amount of change left, and a coin denomination. The function should do as follows

1. Compute the number of coins of the given denomination it can take out of n, and
2. Compute the new value of n, after taking out those coins.

Im completely stuck. I hope it looks like i have made some effort to solve this problem. I just simply have ran out of ideas. Can anyone help me?? If you would like anything else please just ask and i can simply answer the questions.

Recommended Answers

All 6 Replies

A few different things I see offhand.

Where do you have string declared/included?

Also I would suggest looking into a case statement. I know its not much help offhand but it will make sense if you look at it :)

Cameron

Your problem is, how many of a coin do you get from the money? How do you do this in real life?

Take 83 cents as an example. How many halves come out of that? How did you determine that? If half dollars were not available (quarter being your biggest coin) how would you determine that 3 of them come out of 83 cents? Once you know how many coins come out, simply calculate what's left over when you remove coins*value amount.

Ponder this a bit.

Series group of restaurants This group has more than one branch in more than one place in the capital city . The group owners want to make a program which is stored data of applicants for the post of Manager in one of the branches associated with this group. The information will be stored are manager's name, address, and phone number. In addition to the branch address and number who wishes to work with it. This information will be stored after making sure of the nonexistence any manager in that branch. Only then can the amendment to the data stored in the program. By

Divide 'changeValue' by 'coinValue' and store it in a variable like 'numOfCoins' - this is the value to be returned by the function. Before returning 'numOfCoins', update 'changeValue' by subtracting the product of 'numOfCoins' and 'coinValue' from it.

Although I think the function should just take the 'changeValue' arguement and print the values of the different coinage. The function should not need to be called 5 times.

Try use

int COINAGE[] = {50, 25, 10, 5, 1, 0};

and loop through each demonination within the function with a for loop:

for(int *coinPtr = &COINAGE[0]; *coinPtr > 0; ++coinPtr)
include<iostream.h>
main()
{
      int mon,q,d,n,p;

        q=mon/25;
        mon=mon-(25*q);
        d=mon/10;
        mon=mon-(10*d);
        n=mon/5;
        mon=mon-(5*n);
        p=mon/1;

cout<<q<<" quarters, "<<d<<" dimes, "<<n<<" nickels, "<<p<<" pennys"<<endl;

      }


}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.