Nofxthepirate

Chapter 4 Exercises

Feb 11th, 2018
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.36 KB | None | 0 0
  1. //Exercise 4.13 - Gas Mileage Calculator
  2. //I was trying to make a function for the while loop which would call in the mileage function but I couldn't get it to work
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. double miles{0}, gallons{0}, totalMiles{0}, totalGallons{0}, mpg{0};
  7.  
  8.  
  9. double mileage(double m, double g) {
  10.     mpg = m / g;
  11.     return mpg;
  12. }
  13.  
  14. int main()
  15. {
  16.     cout << "Enter miles driven (-1 to quit): ";
  17.     cin >> miles;
  18.     while (miles != -1) {
  19.  
  20.         totalMiles += miles;
  21.         cout << "Enter gallons used: ";
  22.         cin >> gallons;
  23.         totalGallons += gallons;
  24.         cout << "MPG this trip: ";
  25.         cout << mileage(miles, gallons) << endl;
  26.         cout << "Total MPG: " << mileage(totalMiles, totalGallons) << endl;
  27.         cout << "\n";
  28.         cout << "Enter miles driven (-1 to quit): ";
  29.         cin >> miles;
  30.     }
  31. }
  32.  
  33.  
  34.  
  35. //Exercise 4.17 - Find the Largest
  36. //This program finds the largest of 10
  37. #include <iostream>
  38.  
  39. using namespace std;
  40.  
  41. void salesCalc(int counter, int number, int largest) {
  42.     while (counter <= 10) {
  43.         cout << "please input salesperson " << counter << "'s sales total: ";
  44.         cin >> number;
  45.             if (number > largest) {
  46.                 largest = number;
  47.             }
  48.         counter++;
  49.     }
  50.     cout << "Largest sales total: " << largest << endl;
  51. }
  52.  
  53. int main()
  54. {
  55.     int i{1}, number{0}, largest{0};
  56.  
  57.     salesCalc(i, number, largest);
  58. }
  59.  
  60.  
  61. //Exercise 4.26 - Square of Asterisks
  62. //I did this 2 different ways because I initially created a single function
  63. //So I recreated it using 3 much neater functions.
  64.  
  65. #include <iostream>
  66. using namespace std;
  67.  
  68. void solidRow(int rowLength){
  69.     int i{1};
  70.     while (i <= rowLength) {
  71.         cout << "*";
  72.         i++;
  73.         }
  74.     cout << endl;
  75. }
  76.  
  77. void hollowRow(int rowLength) {
  78.     int i{2};
  79.     cout << "*";
  80.     while (i < rowLength) {
  81.         cout << " ";
  82.         i++;
  83.     }
  84.     cout << "*" << endl;
  85. }
  86.  
  87. void multiFunctionSquare(int rowLength) {
  88.     int i{2};
  89.     solidRow(rowLength);
  90.     while (i < rowLength) {
  91.         hollowRow(rowLength);
  92.         i++;
  93.     }
  94.     if (rowLength != 1) {
  95.         solidRow(rowLength);
  96.     }
  97. }
  98.  
  99. void singleFunctionSquare(int length) {
  100.     int i{1};
  101.     int j{0};
  102.         while (i <= length) {
  103.             while ((i == 1) && (j < length)) {
  104.                 cout << "*";
  105.                 j++;
  106.             }
  107.         cout << endl;
  108.         i++;
  109.         j = 0;
  110.             while ((i > 1) && (i < length)) {
  111.                 if (j == 0) {
  112.                     cout << "*";
  113.                     j++;
  114.                 }
  115.                 else if((j > 0) && (j < (length - 1))){
  116.                     cout << " ";
  117.                     j++;
  118.                 }
  119.                 else if (j == (length - 1)) {
  120.                     cout << "*" << endl;
  121.                     j = 0;
  122.                     i++;
  123.                 }
  124.             }
  125.             while ((i == length) && (j < length)) {
  126.             cout << "*";
  127.             j++;
  128.             }
  129.     cout << endl;
  130.     i++;
  131.     }
  132.  
  133. }
  134.  
  135. int main()
  136. {
  137.     int length{0};
  138.     cout << "Please enter a number:";
  139.     cin >> length;
  140.  
  141.     cout << "Multi Function Square:" << endl;
  142.     multiFunctionSquare(length);
  143.     cout << "Single Function Square:" << endl;
  144.     singleFunctionSquare(length);
  145. }
  146.  
  147.  
  148.  
  149. // exercise 4.35 - Factorial and e calculations
  150.  
  151. #include <iostream>
  152. #include <iomanip>
  153. #include <cmath>
  154.  
  155. using namespace std;
  156.  
  157. double factorial(double input) {
  158.     int i{1};
  159.     double j{input};
  160.     while (i < j) {
  161.         input *= i;
  162.         i++;
  163.     }
  164.     return input;
  165. }
  166. double eulers() {
  167.     int i{1};
  168.     double e{1};
  169.  
  170.     while (i < 20) {
  171.         e += (1 / factorial(i));
  172.         i++;
  173.     }
  174.     return e;
  175. }
  176.  
  177. double ePowers(double x){
  178.     double i{1};
  179.     double e{1};
  180.  
  181.     while (i < 20) {
  182.         e += (pow(x, i) / factorial(i));
  183.         i++;
  184.     }
  185.     return e;
  186. }
  187. int main()
  188. {
  189.     int num{0};
  190.     double x{0};
  191.  
  192.     cout << "Please enter a number you would like the factorial for: ";
  193.     cin >> num;
  194.     cout << "Factorial is " << factorial(num) << endl;
  195.  
  196.     cout << "Please enter the terms of e to display:";
  197.     cin >> num;
  198.  
  199.     cout << "e is: " << setprecision(num) << eulers() << endl;
  200.  
  201.     cout << "Please enter the power of e:";
  202.     cin >> x;
  203.  
  204.     cout << "enter the terms in e^x to display: ";
  205.     cin >> num;
  206.  
  207.     cout << "e to the power of " << x << " displayed to " << num
  208.      << " terms is: " << setprecision(num) << ePowers(x) << endl;
  209. }
  210.  
  211. //Exercise 4.36 Account Class Modification
  212. // Fig. 3.8: Account.h
  213. // Account class with name and balance data members, and a
  214. // constructor and deposit function that each perform validation.
  215. #include <string>
  216. #include <iomanip>
  217.  
  218. class Account {
  219. public:
  220.    // Account constructor with two parameters
  221.    Account(std::string accountName, int initialBalance)
  222.       : name{accountName} { // assign accountName to data member name
  223.  
  224.       // validate that the initialBalance is greater than 0; if not,
  225.       // data member balance keeps its default initial value of 0
  226.       if (initialBalance > 0) { // if the initialBalance is valid
  227.          balance = initialBalance; // assign it to data member balance
  228.       }
  229.    }
  230.  
  231.    // function that deposits (adds) only a valid amount to the balance
  232.    void deposit(int depositAmount) {
  233.       if (depositAmount > 0) { // if the depositAmount is valid
  234.          balance = balance + depositAmount; // add it to the balance
  235.       }
  236.    }
  237.  
  238.    // function returns the account balance
  239.    double getBalance() const {
  240.       return balance;
  241.    }
  242.  
  243.    // function that sets the name
  244.    void setName(std::string accountName) {
  245.       name = accountName;
  246.    }
  247.  
  248.    // function that returns the name
  249.    std::string getName() const {
  250.       return name;
  251.    }
  252.  
  253. private:
  254.    std::string name; // account name data member
  255.    double balance{0}; // data member with default initial value
  256. }; // end class Account
  257.  
  258.  
  259. /**************************************************************************
  260.  * (C) Copyright 1992-2017 by Deitel & Associates, Inc. and               *
  261.  * Pearson Education, Inc. All Rights Reserved.                           *
  262.  *                                                                        *
  263.  * DISCLAIMER: The authors and publisher of this book have used their     *
  264.  * best efforts in preparing the book. These efforts include the          *
  265.  * development, research, and testing of the theories and programs        *
  266.  * to determine their effectiveness. The authors and publisher make       *
  267.  * no warranty of any kind, expressed or implied, with regard to these    *
  268.  * programs or to the documentation contained in these books. The authors *
  269.  * and publisher shall not be liable in any event for incidental or       *
  270.  * consequential damages in connection with, or arising out of, the       *
  271.  * furnishing, performance, or use of these programs.                     *
  272.  *************************************************************************/
  273.  
  274.  
  275.  
  276. //exercise 4.36 - Account Class Modification Main File
  277.  
  278.  
  279. // Fig. 3.9: AccountTest.cpp
  280. // Displaying and updating Account balances.
  281. #include <iostream>
  282. #include <iomanip>
  283. #include "Account.h"
  284.  
  285.  
  286. using namespace std;
  287.  
  288. void displayAccount (Account accountToDisplay) {
  289.  cout << "Account: " << accountToDisplay.getName()
  290.  << " balance is $" << accountToDisplay.getBalance() << endl;
  291.  }
  292.  
  293. int main()
  294. {
  295.     cout << setprecision(2) << fixed;
  296.    Account account1{"Jane Green", 50};
  297.    Account account2{"John Blue", -7};
  298.  
  299.  
  300.    // display initial balance of each object
  301.    displayAccount(account1);
  302.    displayAccount(account2);
  303.  
  304.    cout << "\n\nEnter deposit amount for account1: "; // prompt
  305.    int depositAmount;
  306.    cin >> depositAmount; // obtain user input
  307.    cout << "adding " << depositAmount << " to account1 balance";
  308.    account1.deposit(depositAmount); // add to account1's balance
  309.  
  310.    // display balances
  311.    displayAccount(account1);
  312.    displayAccount(account2);
  313.  
  314.    cout << "\n\nEnter deposit amount for account2: "; // prompt
  315.    cin >> depositAmount; // obtain user input
  316.    cout << "adding " << depositAmount << " to account2 balance";
  317.    account2.deposit(depositAmount); // add to account2 balance
  318.  
  319.    // display balances
  320.    displayAccount(account1);
  321.    displayAccount(account2);
  322. }
  323.  
  324. /**************************************************************************
  325.  * (C) Copyright 1992-2017 by Deitel & Associates, Inc. and               *
  326.  * Pearson Education, Inc. All Rights Reserved.                           *
  327.  *                                                                        *
  328.  * DISCLAIMER: The authors and publisher of this book have used their     *
  329.  * best efforts in preparing the book. These efforts include the          *
  330.  * development, research, and testing of the theories and programs        *
  331.  * to determine their effectiveness. The authors and publisher make       *
  332.  * no warranty of any kind, expressed or implied, with regard to these    *
  333.  * programs or to the documentation contained in these books. The authors *
  334.  * and publisher shall not be liable in any event for incidental or       *
  335.  * consequential damages in connection with, or arising out of, the       *
  336.  * furnishing, performance, or use of these programs.                     *
  337.  *************************************************************************/
Add Comment
Please, Sign In to add comment