Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Exercise 4.13 - Gas Mileage Calculator
- //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
- #include <iostream>
- using namespace std;
- double miles{0}, gallons{0}, totalMiles{0}, totalGallons{0}, mpg{0};
- double mileage(double m, double g) {
- mpg = m / g;
- return mpg;
- }
- int main()
- {
- cout << "Enter miles driven (-1 to quit): ";
- cin >> miles;
- while (miles != -1) {
- totalMiles += miles;
- cout << "Enter gallons used: ";
- cin >> gallons;
- totalGallons += gallons;
- cout << "MPG this trip: ";
- cout << mileage(miles, gallons) << endl;
- cout << "Total MPG: " << mileage(totalMiles, totalGallons) << endl;
- cout << "\n";
- cout << "Enter miles driven (-1 to quit): ";
- cin >> miles;
- }
- }
- //Exercise 4.17 - Find the Largest
- //This program finds the largest of 10
- #include <iostream>
- using namespace std;
- void salesCalc(int counter, int number, int largest) {
- while (counter <= 10) {
- cout << "please input salesperson " << counter << "'s sales total: ";
- cin >> number;
- if (number > largest) {
- largest = number;
- }
- counter++;
- }
- cout << "Largest sales total: " << largest << endl;
- }
- int main()
- {
- int i{1}, number{0}, largest{0};
- salesCalc(i, number, largest);
- }
- //Exercise 4.26 - Square of Asterisks
- //I did this 2 different ways because I initially created a single function
- //So I recreated it using 3 much neater functions.
- #include <iostream>
- using namespace std;
- void solidRow(int rowLength){
- int i{1};
- while (i <= rowLength) {
- cout << "*";
- i++;
- }
- cout << endl;
- }
- void hollowRow(int rowLength) {
- int i{2};
- cout << "*";
- while (i < rowLength) {
- cout << " ";
- i++;
- }
- cout << "*" << endl;
- }
- void multiFunctionSquare(int rowLength) {
- int i{2};
- solidRow(rowLength);
- while (i < rowLength) {
- hollowRow(rowLength);
- i++;
- }
- if (rowLength != 1) {
- solidRow(rowLength);
- }
- }
- void singleFunctionSquare(int length) {
- int i{1};
- int j{0};
- while (i <= length) {
- while ((i == 1) && (j < length)) {
- cout << "*";
- j++;
- }
- cout << endl;
- i++;
- j = 0;
- while ((i > 1) && (i < length)) {
- if (j == 0) {
- cout << "*";
- j++;
- }
- else if((j > 0) && (j < (length - 1))){
- cout << " ";
- j++;
- }
- else if (j == (length - 1)) {
- cout << "*" << endl;
- j = 0;
- i++;
- }
- }
- while ((i == length) && (j < length)) {
- cout << "*";
- j++;
- }
- cout << endl;
- i++;
- }
- }
- int main()
- {
- int length{0};
- cout << "Please enter a number:";
- cin >> length;
- cout << "Multi Function Square:" << endl;
- multiFunctionSquare(length);
- cout << "Single Function Square:" << endl;
- singleFunctionSquare(length);
- }
- // exercise 4.35 - Factorial and e calculations
- #include <iostream>
- #include <iomanip>
- #include <cmath>
- using namespace std;
- double factorial(double input) {
- int i{1};
- double j{input};
- while (i < j) {
- input *= i;
- i++;
- }
- return input;
- }
- double eulers() {
- int i{1};
- double e{1};
- while (i < 20) {
- e += (1 / factorial(i));
- i++;
- }
- return e;
- }
- double ePowers(double x){
- double i{1};
- double e{1};
- while (i < 20) {
- e += (pow(x, i) / factorial(i));
- i++;
- }
- return e;
- }
- int main()
- {
- int num{0};
- double x{0};
- cout << "Please enter a number you would like the factorial for: ";
- cin >> num;
- cout << "Factorial is " << factorial(num) << endl;
- cout << "Please enter the terms of e to display:";
- cin >> num;
- cout << "e is: " << setprecision(num) << eulers() << endl;
- cout << "Please enter the power of e:";
- cin >> x;
- cout << "enter the terms in e^x to display: ";
- cin >> num;
- cout << "e to the power of " << x << " displayed to " << num
- << " terms is: " << setprecision(num) << ePowers(x) << endl;
- }
- //Exercise 4.36 Account Class Modification
- // Fig. 3.8: Account.h
- // Account class with name and balance data members, and a
- // constructor and deposit function that each perform validation.
- #include <string>
- #include <iomanip>
- class Account {
- public:
- // Account constructor with two parameters
- Account(std::string accountName, int initialBalance)
- : name{accountName} { // assign accountName to data member name
- // validate that the initialBalance is greater than 0; if not,
- // data member balance keeps its default initial value of 0
- if (initialBalance > 0) { // if the initialBalance is valid
- balance = initialBalance; // assign it to data member balance
- }
- }
- // function that deposits (adds) only a valid amount to the balance
- void deposit(int depositAmount) {
- if (depositAmount > 0) { // if the depositAmount is valid
- balance = balance + depositAmount; // add it to the balance
- }
- }
- // function returns the account balance
- double getBalance() const {
- return balance;
- }
- // function that sets the name
- void setName(std::string accountName) {
- name = accountName;
- }
- // function that returns the name
- std::string getName() const {
- return name;
- }
- private:
- std::string name; // account name data member
- double balance{0}; // data member with default initial value
- }; // end class Account
- /**************************************************************************
- * (C) Copyright 1992-2017 by Deitel & Associates, Inc. and *
- * Pearson Education, Inc. All Rights Reserved. *
- * *
- * DISCLAIMER: The authors and publisher of this book have used their *
- * best efforts in preparing the book. These efforts include the *
- * development, research, and testing of the theories and programs *
- * to determine their effectiveness. The authors and publisher make *
- * no warranty of any kind, expressed or implied, with regard to these *
- * programs or to the documentation contained in these books. The authors *
- * and publisher shall not be liable in any event for incidental or *
- * consequential damages in connection with, or arising out of, the *
- * furnishing, performance, or use of these programs. *
- *************************************************************************/
- //exercise 4.36 - Account Class Modification Main File
- // Fig. 3.9: AccountTest.cpp
- // Displaying and updating Account balances.
- #include <iostream>
- #include <iomanip>
- #include "Account.h"
- using namespace std;
- void displayAccount (Account accountToDisplay) {
- cout << "Account: " << accountToDisplay.getName()
- << " balance is $" << accountToDisplay.getBalance() << endl;
- }
- int main()
- {
- cout << setprecision(2) << fixed;
- Account account1{"Jane Green", 50};
- Account account2{"John Blue", -7};
- // display initial balance of each object
- displayAccount(account1);
- displayAccount(account2);
- cout << "\n\nEnter deposit amount for account1: "; // prompt
- int depositAmount;
- cin >> depositAmount; // obtain user input
- cout << "adding " << depositAmount << " to account1 balance";
- account1.deposit(depositAmount); // add to account1's balance
- // display balances
- displayAccount(account1);
- displayAccount(account2);
- cout << "\n\nEnter deposit amount for account2: "; // prompt
- cin >> depositAmount; // obtain user input
- cout << "adding " << depositAmount << " to account2 balance";
- account2.deposit(depositAmount); // add to account2 balance
- // display balances
- displayAccount(account1);
- displayAccount(account2);
- }
- /**************************************************************************
- * (C) Copyright 1992-2017 by Deitel & Associates, Inc. and *
- * Pearson Education, Inc. All Rights Reserved. *
- * *
- * DISCLAIMER: The authors and publisher of this book have used their *
- * best efforts in preparing the book. These efforts include the *
- * development, research, and testing of the theories and programs *
- * to determine their effectiveness. The authors and publisher make *
- * no warranty of any kind, expressed or implied, with regard to these *
- * programs or to the documentation contained in these books. The authors *
- * and publisher shall not be liable in any event for incidental or *
- * consequential damages in connection with, or arising out of, the *
- * furnishing, performance, or use of these programs. *
- *************************************************************************/
Add Comment
Please, Sign In to add comment