Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. //----------------------------------------------------------------------------//
  2. // Name: William Lee                                                          //
  3. // Student ID: 1012604                                                        //
  4. // Assignment: 1. Find the best crop for Old MacDonald to plant.              //
  5. //----------------------------------------------------------------------------//
  6.  
  7. #include <iostream>
  8. #include <iomanip>
  9. #define SIZE 4 //number of types of crops
  10. using namespace std;
  11.  
  12. void calculate(float cost, int yield, float price, float increase, float &mnP, float &mxP, float &avgP);
  13. void getData(string &crop, float &cost, int &yield, float &price, float &increase);
  14. void printResults(string crop, float mnP, float mxP, float avgP);
  15.  
  16. int main()
  17. {
  18.       //Declarations
  19.       string crop, bestCrop;
  20.       float cost, price, increase;
  21.       float mnP, mxP, avgP, big = 0;
  22.       int yield, i;
  23.  
  24.       //for loop for 4 crops
  25.       for (i=0;i<SIZE;i++)
  26.       {
  27.           getData(crop, cost, yield, price, increase);
  28.           calculate(cost, yield, price, increase, mnP, mxP, avgP);
  29.           printResults(crop, mnP, mxP, avgP);
  30.  
  31.           //if statement for best crop
  32.           if (avgP > big)
  33.           {
  34.               big = avgP;
  35.               bestCrop = crop;
  36.           }
  37.  
  38.       }
  39.       cout << endl << "Old MacDonald, you should plant " << bestCrop << endl << endl;
  40.       return 0;
  41. }
  42.  
  43. //Gets data from main function and returns min, max, and avg profit
  44. void calculate( float cost, int yield, float price, float increase, float &mnP, float &mxP, float &avgP)
  45. {
  46.     mnP = (yield * price * 1000) - (1000 * cost);
  47.     mxP = (yield * price * 1000) + ((yield * price * 1000) * (increase / 100)) - (1000 * cost);
  48.     avgP = (mxP + mnP)/2;
  49. }
  50.  
  51. //Gets user input of data for crops
  52. void getData(string &crop, float &cost, int &yield, float &price, float &increase)
  53. {
  54.     cout << "Enter the crop name: ";
  55.     getline(cin, crop);
  56.     cout << "Enter cost, yield, price per bushel, and increase data: ";
  57.     cin >> cost >> yield >> price >> increase;
  58.     cin.ignore();
  59. }
  60.  
  61. //Prints crop name, minimum profit, maximum profit, average profit
  62. void printResults(string crop, float mnP, float mxP, float avgP)
  63. {
  64.     cout << endl;
  65.     cout << setw(30) << "Minimum Profit" << setw(20) << "Maximum Profit" << setw(20) << "Average profit" << endl;
  66.     cout << left << setw(11) << crop;
  67.     cout << fixed << setprecision(2) << right << setw(8) << "$" << mnP << setw(12) << "$" << mxP << setw(12) << "$" << avgP << endl << endl;
  68.     //find out how to align values
  69. }
  70.  
  71. /*
  72. Enter the crop name: Sweet corn
  73. Enter cost, yield, price per bushel, and increase data: 45.25 173 0.54 4.7
  74.  
  75.                 Minimum Profit      Maximum Profit      Average profit
  76. Sweet corn        $48170.00           $52560.74           $50365.38
  77.  
  78. Enter the crop name: Wheat
  79. Enter cost, yield, price per bushel, and increase data: 43.00 200 0.43 3.1
  80.  
  81.                 Minimum Profit      Maximum Profit      Average profit
  82. Wheat             $43000.00           $45666.00           $44333.00
  83.  
  84. Enter the crop name: Soybeans
  85. Enter cost, yield, price per bushel, and increase data: 43.50 157 0.61 4.3
  86.  
  87.                 Minimum Profit      Maximum Profit      Average profit
  88. Soybeans          $52270.00           $56388.11           $54329.06
  89.  
  90. Enter the crop name: Green beans
  91. Enter cost, yield, price per bushel, and increase data: 40.80 118 0.72 2.7
  92.  
  93.                 Minimum Profit      Maximum Profit      Average profit
  94. Green beans       $44160.00           $46453.93           $45306.96
  95.  
  96.  
  97. Old MacDonald, you should plant Soybeans
  98.  
  99.  
  100. Process returned 0 (0x0)   execution time : 54.076 s
  101. Press any key to continue.
  102. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement