Mixed Nuts


Program Listing

MixedNuts.cpp

/****************************************************************************
*                                                                           *
*   Program:    MixedNuts                                                   *
*                                                                           *
*   Purpose:    This program will calculate the percent of peanuts and      *
*               the percent of cashews to mix to attain a specified         *
*               price for the mixture.                                      *
*                                                                           *
****************************************************************************/

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

using namespace std;

int main() {

//  Define program constants

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

//  Get the desired price of the mix

    cout << "Enter the desired price of the mix:  " << flush;
    float MixPrice;                                 // Price of the mixed nuts
    cin >> MixPrice;

//  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

//  Print the results

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

Sample Run #1

Enter the desired price of the mix:  8.50
The mix should contain 30% peanuts and 70% cashews

Sample Run #2

Enter the desired price of the mix:  12.00
The mix should contain -40% peanuts and 140% cashews



Return to Lectures page


e-mail me at robbk@hsc.edu

This page was last modified on Tue Dec 7 16:04:31 1999 .

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

Return to Coms 261 home page