Advertisement
bwukki

Untitled

Feb 5th, 2018
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.60 KB | None | 0 0
  1. //4.35
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. double factorial (double num) {
  7.     double result{1};
  8.     double counter{0};
  9.     while (counter < num) {
  10.         result = result * (num-counter);
  11.         counter++;
  12.     }
  13.     return result;
  14. }
  15.  
  16. double calculateE (double accuracy) {
  17.     double e{1};
  18.     double counter{1};
  19.     while (counter < accuracy+1) {
  20.         e = e + 1/(factorial(counter));
  21.         counter++;
  22.     }
  23.     return(e);
  24. }
  25.  
  26.  
  27. double power (double num, double power) {
  28.     double counter{0};
  29.     double result{1};
  30.     while (counter < power) {
  31.         result = result*num;
  32.         counter++;
  33.     }
  34.     return result;
  35.  
  36. }
  37.  
  38. double calculateEX (double accuracy, double x) {
  39.     double e{1};
  40.     double counter{1};
  41.     while (counter < accuracy+1) {
  42.         e = e + power(x,counter)/(factorial(counter));
  43.         counter++;
  44.     }
  45.     return(e);
  46. }
  47.  
  48. int main()
  49. {
  50.     double num{0};
  51.     cout << "Please input a number to calculate its factorial: " << endl;
  52.     cin >> num;
  53.     cout << factorial(num) << endl;
  54.     cout << "Please input a degree of accuracy for our e calculation " << endl;
  55.     cin >> num;
  56.     cout << "e approx =" << calculateE(num) << endl;
  57.     cout << "Please input e^x for what value of x?" << endl;
  58.     cin >> num;
  59.     cout << "e^" << num << "= " << calculateEX(10,num) << endl;
  60.     return 0;
  61. }
  62.  
  63. //4.26
  64.  
  65. #include <iostream>
  66.  
  67. using namespace std;
  68.  
  69.  
  70. void solidline(int length) {
  71.     int counter{0};
  72.     while (counter < length) {
  73.         cout << "*";
  74.         counter++;
  75.     }
  76.     cout << endl;
  77. }
  78.  
  79. void hollowline(int length) {
  80.     int counter{0};
  81.     while (counter < length) {
  82.         if (counter == 0 || counter == length-1) {
  83.           cout << "*";
  84.         }
  85.         else { cout << " ";}
  86.         counter++;
  87.     }
  88.     cout << endl;
  89. }
  90.  
  91. int main() {
  92.     int sideLength{0};
  93.     int counter{0};
  94.     cout << "Please enter a side length for your square." << endl;
  95.     cin >> sideLength;
  96.     solidline(sideLength);
  97.     while (counter < sideLength-2) {
  98.       hollowline(sideLength);
  99.       counter++;
  100.     }
  101.     solidline(sideLength);
  102.     return 0;
  103. }
  104.  
  105. //4.17
  106. #include <iostream>
  107.  
  108. using namespace std;
  109.  
  110. int main()
  111. {
  112.     int counter{0};
  113.     int inputNum{0};
  114.     int currentLargest{0};
  115.     while (counter < 10) {
  116.         cout << "Please enter a number." << endl;
  117.         cin >> inputNum;
  118.         if (inputNum > currentLargest)
  119.         {
  120.             currentLargest = inputNum;
  121.         }
  122.         counter++;
  123.     }
  124.     cout << "The largest number was: " << currentLargest << endl;
  125.     return 0;
  126. }
  127.  
  128. //4.13
  129. #include <iostream>
  130.  
  131. using namespace std;
  132.  
  133. int main()
  134. {
  135.     string quit{"moo"};
  136.     int miles{0};
  137.     int gallons{0};
  138.     int ttlMiles{0};
  139.     int ttlGallons{0};
  140.     while (quit != "n") {
  141.         cout << "Please enter the miles driven: " << endl;
  142.         cin >> miles;
  143.         cout << "Please enter the gallons used: " << endl;
  144.         cin >> gallons;
  145.         cout << "MPG this trip: " << miles / gallons << endl;
  146.         ttlMiles = ttlMiles + miles;
  147.         ttlGallons = ttlGallons + gallons;
  148.         cout << "Total MPG so far: " << ttlMiles / ttlGallons << endl;
  149.         cout << "Would you like to continue entering data? (y/n)" << endl;
  150.         cin >> quit;
  151.     }
  152.     return 0;
  153. }
  154.  
  155. //4.36 (main)
  156. #include <iostream>
  157. #include "account.h";
  158. #include <iomanip>
  159. using namespace std;
  160.  
  161. int main()
  162. {
  163.     Account test1("Jones", 400.00);
  164.     cout.precision(2);
  165.     cout << "Hello world!" << endl;
  166.     cout << "Current balance is.... " << fixed << test1.getBalance() << endl;
  167.     cout << test1.withdraw(100.00) << endl;
  168.     cout << fixed << test1.getBalance();
  169.     return 0;
  170. }
  171.  
  172. //4.36 (header)
  173. #include <string>
  174. #include <iostream>
  175.  
  176. class Account {
  177. public:
  178.     Account(std::string accountName, double initialBalance)
  179.         : name{accountName} {
  180.  
  181.         if (initialBalance > 0) {
  182.             balance = initialBalance;
  183.     }
  184.     }
  185.     void deposit(double depositAmount) {
  186.     if (depositAmount > 0) {
  187.         balance = balance + depositAmount;
  188.     }
  189.     }
  190.  
  191.     double getBalance() const {
  192.     return balance;
  193.     }
  194.  
  195.     void setName(std::string accountName) {
  196.     name = accountName;
  197.     }
  198.  
  199.     std::string getName() const {
  200.     return name;
  201.     }
  202.  
  203.     std::string withdraw(double withdrawAmount) {
  204.     if (withdrawAmount > balance) {
  205.         return("You do not have enough money in your account to do that.");
  206.     }
  207.     else {
  208.         balance = balance - withdrawAmount;
  209.         return("Money withdrawn succesfully.");
  210.     }
  211.     }
  212.  
  213. private:
  214.     std::string name;
  215.     double balance{0};
  216. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement