Mixed Nuts Debug


MixedNutsDebug.cpp

Program listing

/****************************************************************************
*                                                                           *
*   Program:    MixedNutsDebug                                              *
*                                                                           *
*   Purpose:    This program will calculate the percent of peanuts and      *
*               the percent of cashews to mix to attain a specified         *
*               price for the mixture.                                      *
*                                                                           *
*   Note:       The symbol DEBUG is used to switch debugging features       *
*               on or off                                                   *
*                                                                           *
****************************************************************************/

#include <iostream>
#include <string>
#include <cmath>

using namespace std;

#define DEBUG

/****************************************************************************
*                                                                           *
*   Function:   main                                                        *
*                                                                           *
*   Purpose:    To find the amount of peanuts and cashews to mix to get     *
*               a specified selling price for the mixture                   *
*                                                                           *
****************************************************************************/

int main() {

    const float PEANUT_PRICE = 5.00;                // Price per lb of peanuts
    const float CASHEW_PRICE = 10.00;               // Price per lb of cashews

    float LowerPrice = PEANUT_PRICE;
    float HigherPrice = CASHEW_PRICE;
    
    if (HigherPrice < LowerPrice) {
        int Temp = HigherPrice;
        HigherPrice = LowerPrice;
        LowerPrice = Temp;
    }

#ifdef DEBUG
    cout << "DEBUG: HigherPrice = " << HigherPrice << endl;
    cout << "DEBUG: LowerPrice = " << LowerPrice << endl;
#endif
    
    if (HigherPrice == LowerPrice) {
        cout << "The peanut and cashew prices are equal, so it doesn't";
        cout << " matter how much of each you mix.  The mix price will be $";
        cout << PEANUT_PRICE << " per pound." << endl;
    }
    else
    {
    
    //  Get the price of the mix

        cout << "Enter the price of the mix:  " << flush;
        float MixPrice;                                 // Price of the mixed nuts
        cin >> MixPrice;
        if (MixPrice > HigherPrice || MixPrice < LowerPrice) {
            cout << "That price is not within the proper range." << endl;
        }
        else
        {

        //  Compute the amounts of peanuts and cashews

            float PeanutPart = (MixPrice - CASHEW_PRICE)/(PEANUT_PRICE - CASHEW_PRICE);
                                                            // Fraction of mix that is peanuts
            float CashewPart = 1 - PeanutPart;              // Fraction that is cashews
            float PeanutPercent = round(100*PeanutPart);    // Express as a percent
            float CashewPercent = round(100*CashewPart);    // Express as a percent

#ifdef DEBUG
            cout << "DEBUG:  MixPrice = " << MixPrice << endl;
            cout << "DEBUG:  PeanutPart = " << PeanutPart << endl;
            cout << "DEBUG:  CashewPart = " << CashewPart << endl;
#endif

        //  Print the results

            cout << "The mix should contain " << PeanutPercent << "% peanuts and ";
            cout << CashewPercent << "% cashews" << endl;
        }
    }
    return 0;
}



Return to Lectures page


e-mail me at robbk@hsc.edu

This page was last modified on Tue Nov 30 09:11:26 1999 .

URL: http://people.hsc.edu/faculty-staff/robbk/Coms261/Examples/MixedNutsDebug.html

Return to Coms 261 home page