Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //----------------------------------------------------------------------------//
- // Name: William Lee //
- // Student ID: 1012604 //
- // Assignment: 1. Find the best crop for Old MacDonald to plant. //
- //----------------------------------------------------------------------------//
- #include <iostream>
- #include <iomanip>
- #define SIZE 4 //number of types of crops
- using namespace std;
- void calculate(float cost, int yield, float price, float increase, float &mnP, float &mxP, float &avgP);
- void getData(string &crop, float &cost, int &yield, float &price, float &increase);
- void printResults(string crop, float mnP, float mxP, float avgP);
- int main()
- {
- //Declarations
- string crop, bestCrop;
- float cost, price, increase;
- float mnP, mxP, avgP, big = 0;
- int yield, i;
- //for loop for 4 crops
- for (i=0;i<SIZE;i++)
- {
- getData(crop, cost, yield, price, increase);
- calculate(cost, yield, price, increase, mnP, mxP, avgP);
- printResults(crop, mnP, mxP, avgP);
- //if statement for best crop
- if (avgP > big)
- {
- big = avgP;
- bestCrop = crop;
- }
- }
- cout << endl << "Old MacDonald, you should plant " << bestCrop << endl << endl;
- return 0;
- }
- //Gets data from main function and returns min, max, and avg profit
- void calculate( float cost, int yield, float price, float increase, float &mnP, float &mxP, float &avgP)
- {
- mnP = (yield * price * 1000) - (1000 * cost);
- mxP = (yield * price * 1000) + ((yield * price * 1000) * (increase / 100)) - (1000 * cost);
- avgP = (mxP + mnP)/2;
- }
- //Gets user input of data for crops
- void getData(string &crop, float &cost, int &yield, float &price, float &increase)
- {
- cout << "Enter the crop name: ";
- getline(cin, crop);
- cout << "Enter cost, yield, price per bushel, and increase data: ";
- cin >> cost >> yield >> price >> increase;
- cin.ignore();
- }
- //Prints crop name, minimum profit, maximum profit, average profit
- void printResults(string crop, float mnP, float mxP, float avgP)
- {
- cout << endl;
- cout << setw(30) << "Minimum Profit" << setw(20) << "Maximum Profit" << setw(20) << "Average profit" << endl;
- cout << left << setw(11) << crop;
- cout << fixed << setprecision(2) << right << setw(8) << "$" << mnP << setw(12) << "$" << mxP << setw(12) << "$" << avgP << endl << endl;
- //find out how to align values
- }
- /*
- Enter the crop name: Sweet corn
- Enter cost, yield, price per bushel, and increase data: 45.25 173 0.54 4.7
- Minimum Profit Maximum Profit Average profit
- Sweet corn $48170.00 $52560.74 $50365.38
- Enter the crop name: Wheat
- Enter cost, yield, price per bushel, and increase data: 43.00 200 0.43 3.1
- Minimum Profit Maximum Profit Average profit
- Wheat $43000.00 $45666.00 $44333.00
- Enter the crop name: Soybeans
- Enter cost, yield, price per bushel, and increase data: 43.50 157 0.61 4.3
- Minimum Profit Maximum Profit Average profit
- Soybeans $52270.00 $56388.11 $54329.06
- Enter the crop name: Green beans
- Enter cost, yield, price per bushel, and increase data: 40.80 118 0.72 2.7
- Minimum Profit Maximum Profit Average profit
- Green beans $44160.00 $46453.93 $45306.96
- Old MacDonald, you should plant Soybeans
- Process returned 0 (0x0) execution time : 54.076 s
- Press any key to continue.
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement