Thread: C++ homework help

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

    C++ homework help

    I have this homework assignment that i need help with and below is the code that i have so far. any help would be appreciated. thank you.


    Write a program that tells what coins to give out for any amount of change from 1 cent to 99 cents.
    For example, if the amount is 86 cents, the output would be something like the following:

    86 cents can be given as 3 quarter(s) 1 dime(s) amd 1 penny(pennies).
    Use coin denominations of 25 cents (quarters),
    10 cents (dimes) and 1 cent (pennies).
    Do not use nickel and half-dollar coins.
    Your program will use the following function (amoung others): void computeCoin(int coinValue, int& number, int& amountLeft); /* Preconditions:
    0 < coinValue < 100;
    0 <= amountLeft < 100.
    Postconditions: number has been set equal to the maximum number of coins of dem=nomination conValue cents that can be obtained from amountLeft cents.
    AmountLeft has been decreased by the value of the coins, that is, decreased by number*coinValue.

    For example, suppose the value of the variable amountLeft is 86. Then, after the following call, the value of number will be 3 and the value of amountLeft will be 11 (because if you take three quarters from 86 cents, that leaves 11 cents):computeCoins(25, number, amountLeft);

    Include a loop that lets the user repeat this computation for new input values untill the user says he or she wants to end the program.
    (Hint: Use integer division and the % operator to implement this function.) p>

    here is what i have so far i just need to implement this function in this program
    void computeCoin(int coinValue, int& number, int& amountLeft);
    how do i do that.

    Code:
    #include <iostream>
    #include <iomanip>
    #include <cmath>
    using namespace std;
    void computeCoin(int coinValue, int& number, int& amountLeft); 
    // Main Program
    int main( )
    {
    
    
     
    // Variable Declations
    int coin_value;
    int quarters;
    int dimes;
    int pennies;
    int total_amount;
    char yes = 'y';
    char Yes = 'Y';
    char agian;
    
    
    
    // Enter code below
    
    do 
    {
    quarters = 0;
    dimes = 0;
    
    pennies = 0;
    
    
    cout<< " Enter the amount of change ";
    cin >> coin_value;
    cout << "\n";
    
    if (coin_value >= 25 && coin_value <= 100)
    {
    quarters = coin_value / 25;
    coin_value = coin_value % 25; 
    }
    
    
    if (coin_value <= 24 && coin_value >= 10)
    {
    dimes = coin_value /10;
    coin_value = coin_value % 10; 
    }
    
    
    
    
    
    if (coin_value <= 4 && coin_value >= 1)
    {
    pennies = coin_value; 
    }
    
    
    ////////
    total_amount =( quarters * 25)+ (dimes * 10) + pennies;
    cout << total_amount << " Cents Can be given as " ;
    ////////
    
    
    if (quarters == 1) 
    {
    cout << quarters << " quarter, ";
    }
    
    
    if (quarters == 2 || quarters == 3)
    {
    cout << quarters << " quarters, ";
    }
    
    
    if (dimes == 1)
    {
    cout << dimes << " dime, ";
    }
    
    
    if (dimes == 2 )
    {
    cout << dimes << " dimes, ";
    }
    
    
    
    
    if (pennies == 1) 
    {
    cout << pennies << " penny. " << endl;
    }
    
    
    if (pennies < 4 && pennies > 2)
    {
    cout << pennies << " pennies. " << endl;
    }
    
    
    cout << "\n";
    cout << " Would you like to run the program agian (Y or N)? ";
    cin >> agian;
    
    }
    
    
    while (agian == yes || agian == Yes);
    
    cout << "\n\nEnd Program.\n";
    return 0;
    }

  2. #2
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    A few points:
    1. Your code formatting and indentation needs work.
    2. Misspelling the variable "agian" is a really bad idea.

    As for the function, you've already implemented the logic. All you need to do is generalize is and stick it into a function. Which part of that are you having trouble with? What have you attempted so far?
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 11-03-2001, 04:39 PM
  2. Homework
    By kermi3 in forum C Programming
    Replies: 10
    Last Post: 09-27-2001, 04:49 PM
  3. Homework
    By kermi3 in forum Windows Programming
    Replies: 5
    Last Post: 09-15-2001, 11:48 AM
  4. Homework
    By kermi3 in forum C Programming
    Replies: 0
    Last Post: 09-10-2001, 01:26 PM