Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //4.35
- #include <iostream>
- using namespace std;
- double factorial (double num) {
- double result{1};
- double counter{0};
- while (counter < num) {
- result = result * (num-counter);
- counter++;
- }
- return result;
- }
- double calculateE (double accuracy) {
- double e{1};
- double counter{1};
- while (counter < accuracy+1) {
- e = e + 1/(factorial(counter));
- counter++;
- }
- return(e);
- }
- double power (double num, double power) {
- double counter{0};
- double result{1};
- while (counter < power) {
- result = result*num;
- counter++;
- }
- return result;
- }
- double calculateEX (double accuracy, double x) {
- double e{1};
- double counter{1};
- while (counter < accuracy+1) {
- e = e + power(x,counter)/(factorial(counter));
- counter++;
- }
- return(e);
- }
- int main()
- {
- double num{0};
- cout << "Please input a number to calculate its factorial: " << endl;
- cin >> num;
- cout << factorial(num) << endl;
- cout << "Please input a degree of accuracy for our e calculation " << endl;
- cin >> num;
- cout << "e approx =" << calculateE(num) << endl;
- cout << "Please input e^x for what value of x?" << endl;
- cin >> num;
- cout << "e^" << num << "= " << calculateEX(10,num) << endl;
- return 0;
- }
- //4.26
- #include <iostream>
- using namespace std;
- void solidline(int length) {
- int counter{0};
- while (counter < length) {
- cout << "*";
- counter++;
- }
- cout << endl;
- }
- void hollowline(int length) {
- int counter{0};
- while (counter < length) {
- if (counter == 0 || counter == length-1) {
- cout << "*";
- }
- else { cout << " ";}
- counter++;
- }
- cout << endl;
- }
- int main() {
- int sideLength{0};
- int counter{0};
- cout << "Please enter a side length for your square." << endl;
- cin >> sideLength;
- solidline(sideLength);
- while (counter < sideLength-2) {
- hollowline(sideLength);
- counter++;
- }
- solidline(sideLength);
- return 0;
- }
- //4.17
- #include <iostream>
- using namespace std;
- int main()
- {
- int counter{0};
- int inputNum{0};
- int currentLargest{0};
- while (counter < 10) {
- cout << "Please enter a number." << endl;
- cin >> inputNum;
- if (inputNum > currentLargest)
- {
- currentLargest = inputNum;
- }
- counter++;
- }
- cout << "The largest number was: " << currentLargest << endl;
- return 0;
- }
- //4.13
- #include <iostream>
- using namespace std;
- int main()
- {
- string quit{"moo"};
- int miles{0};
- int gallons{0};
- int ttlMiles{0};
- int ttlGallons{0};
- while (quit != "n") {
- cout << "Please enter the miles driven: " << endl;
- cin >> miles;
- cout << "Please enter the gallons used: " << endl;
- cin >> gallons;
- cout << "MPG this trip: " << miles / gallons << endl;
- ttlMiles = ttlMiles + miles;
- ttlGallons = ttlGallons + gallons;
- cout << "Total MPG so far: " << ttlMiles / ttlGallons << endl;
- cout << "Would you like to continue entering data? (y/n)" << endl;
- cin >> quit;
- }
- return 0;
- }
- //4.36 (main)
- #include <iostream>
- #include "account.h";
- #include <iomanip>
- using namespace std;
- int main()
- {
- Account test1("Jones", 400.00);
- cout.precision(2);
- cout << "Hello world!" << endl;
- cout << "Current balance is.... " << fixed << test1.getBalance() << endl;
- cout << test1.withdraw(100.00) << endl;
- cout << fixed << test1.getBalance();
- return 0;
- }
- //4.36 (header)
- #include <string>
- #include <iostream>
- class Account {
- public:
- Account(std::string accountName, double initialBalance)
- : name{accountName} {
- if (initialBalance > 0) {
- balance = initialBalance;
- }
- }
- void deposit(double depositAmount) {
- if (depositAmount > 0) {
- balance = balance + depositAmount;
- }
- }
- double getBalance() const {
- return balance;
- }
- void setName(std::string accountName) {
- name = accountName;
- }
- std::string getName() const {
- return name;
- }
- std::string withdraw(double withdrawAmount) {
- if (withdrawAmount > balance) {
- return("You do not have enough money in your account to do that.");
- }
- else {
- balance = balance - withdrawAmount;
- return("Money withdrawn succesfully.");
- }
- }
- private:
- std::string name;
- double balance{0};
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement