Created
February 21, 2014 19:26
-
-
Save dillmo/9141528 to your computer and use it in GitHub Desktop.
Solutions for C++ Problem Set 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Problem Set 3 Question 12 solution by Dillon Morse | |
* -------------------------------------------------- | |
* A bank charges $10 per month plus the following check fees for a commercial | |
* checking account: | |
* | |
* $0.10 each for fewer than 20 checks | |
* $0.08 each for 20-39 checks | |
* $0.06 each for 40-59 checks | |
* $0.04 each for 60 or more checks | |
* | |
* The bank also charges an extra $15 if the balance of the account falls below | |
* $400 (before any check fees are applied). Write a program that asks for the | |
* beginning balance and the number of checks written. Compute and display the | |
* bank's service fees for the month. | |
* | |
* Input Validation: Do not accept a negative value of the number of checks | |
* written. If a negative value is given for the beginning balance, display an | |
* urgent message indicating the account is overdrawn. | |
*/ | |
#include<iostream> | |
using namespace std; | |
double get_balance() { | |
double balance; | |
cout << "What is your account's balance?" << '\n'; | |
cin >> balance; | |
return balance; | |
} | |
int get_checks_written() { | |
int checks_written; | |
cout << "How many checks did you write?" << '\n'; | |
cin >> checks_written; | |
return checks_written; | |
} | |
double compute_charges(double balance, int checks_written) { | |
// fees is initialized as 0 so I can add to it later on. | |
double fees = 0; | |
if(balance < 400) { | |
fees = fees + 15; | |
} | |
if(checks_written < 20) { | |
fees = fees + 0.1 * checks_written; | |
} else if(checks_written < 40) { | |
fees = fees + 0.08 * checks_written; | |
} else if(checks_written < 60) { | |
fees = fees + 0.06 * checks_written; | |
} else { | |
fees = fees + 0.04 * checks_written; | |
} | |
return fees; | |
} | |
int main() { | |
double balance = get_balance(); | |
int checks_written = get_checks_written(); | |
double charges; | |
// Input validation | |
if(checks_written < 0) { | |
cout << "Cannot accept a negative number of checks written. " << | |
"The transaction will not complete." << '\n'; | |
} else { | |
// Input validation | |
if(balance < 0) { | |
cout << "Warning: your account is overdrawn." << '\n'; | |
} | |
charges = compute_charges(balance, checks_written); | |
cout << "Service fees: $" << charges << '\n'; | |
} | |
// Necessary to prevent warnings from some compilers. | |
// I'm not keeping the program running in a paused state because I am running | |
// it from a CLI, so letting it close is most convenient. | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Problem Set 3 Question 21 Solution by Dillon Morse | |
* -------------------------------------------------- | |
* Write a program that displays the following menu: | |
* | |
* Geometry Calculator | |
* | |
* 1. Calculate the Area of a Circle | |
* 2. Calculate the Area of a Rectangle | |
* 3. Calculate the Area of a Triangle | |
* 4. Quit | |
* | |
* Enter your choice (1-4): | |
* | |
* If the user enters 1, the program should ask for the radius of the circle | |
* and then display its area. | |
* | |
* If the user enters 2, the program should ask for the length and width of | |
* the rectangle and then display the rectangle's area. | |
* | |
* If the user enters 3, the program should ask for the length of the | |
* triangle's base and its height, and then display its area. | |
* | |
* If the user enters 4, the program should end. | |
* | |
* Input validation: Display an error message if the user enters a number | |
* outside the range of 1 through 4 when selecting an item from the menu. Do | |
* not accept negative values for the circle's radius, the rectangle's length | |
* or width, or the triangle's base or height. | |
* | |
* Make use of switch statements for selecting the operation in your solution. | |
*/ | |
#include<iostream> | |
#include<cmath> | |
using namespace std; | |
// Define the constant pi. Beware that this only works in C++ 11. | |
constexpr double pi() { return std::atan(1)*4; } | |
int get_choice() { | |
int choice; | |
cout << "Geometry Calculator" << '\n' | |
<< '\n' | |
<< "1. Calculate the Area of a Circle" << '\n' | |
<< "2. Calculate the Area of a Rectange" << '\n' | |
<< "3. Calculate the Area of a Triangle" << '\n' | |
<< "4. Quit" << '\n' | |
<< '\n' | |
<< "Enter your choice (1-4):" << '\n'; | |
cin >> choice; | |
return choice; | |
} | |
double circle() { | |
double radius; | |
double area; | |
cout << "What is the radius of your circle?" << '\n'; | |
cin >> radius; | |
area = pow(radius, 2) * pi(); | |
if(radius < 0) { | |
// Performed for error handling | |
return -1; | |
} else { | |
return area; | |
} | |
} | |
double rectangle() { | |
double length; | |
double width; | |
double area; | |
cout << "Enter the length and width of your rectangle, seperated by a " | |
<< "carriage return." << '\n'; | |
cin >> length >> width; | |
area = length * width; | |
if((length < 0) || (width < 0)) { | |
// Performed for error handling | |
return -1; | |
} else { | |
return area; | |
} | |
} | |
double triangle() { | |
double base; | |
double height; | |
double area; | |
cout << "Enter the length of the base and the height of your triangle, " | |
<< "seperated by a carriage return." << '\n'; | |
cin >> base >> height; | |
area = 0.5 * base * height; | |
if((base < 0) || (height < 0)) { | |
// Performed for error handling | |
return -1; | |
} else { | |
return area; | |
} | |
} | |
int check_errors(double input, double area) { | |
if((input < 1) || (input > 4)) { | |
cout << "Error: you entered an invalid choice"; | |
return -1; | |
} | |
else if(area < 0) { | |
cout << "Error: you entered a negative number"; | |
return -1; | |
} | |
else return 0; | |
} | |
int main() { | |
int input = get_choice(); | |
double area; | |
switch(input) { | |
case 1: | |
area = circle(); | |
break; | |
case 2: | |
area = rectangle(); | |
break; | |
case 3: | |
area = triangle(); | |
break; | |
case 4: | |
// Exits the program | |
return 0; | |
default: | |
// Breaks from the loop if there are errors | |
break; | |
} | |
// Exits the program if there are errors | |
if(check_errors(input, area) == -1) return 0; | |
else { | |
cout << "The area of your figure is " << area << " units squared." << '\n'; | |
// Necessary to prevent warnings from some compilers. | |
// I'm not keeping the program running in a paused state because I am | |
// running it from a CLI, so letting it close is most convenient. | |
return 0; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Problem Set 3 Question 7 solution by Dillon Morse | |
* ------------------------------------------------- | |
* Write a program that asks the user to enter a number of seconds. | |
* | |
* - If the number of seconds entered by the user is greater than or equal to | |
* 60, the program should display the number of minutes in that many seconds. | |
* - If the number of seconds entered by the user is greater than or equal to | |
* 3,600, the program should display the number of hours in that many | |
* seconds. | |
* - If the number of seconds entered by the user is greater than or equal to | |
* 86,400, the program should display the number of days in that many | |
* seconds. | |
*/ | |
#include<iostream> | |
using namespace std; | |
string get_output(int seconds) { | |
double number; | |
string unit; | |
string out; | |
if(seconds >= 86400) { | |
number = seconds / 86400.0; | |
unit = "days"; | |
} else if(seconds >= 3600) { | |
number = seconds / 3600.0; | |
unit = "hours"; | |
} else if(seconds >= 60) { | |
number = seconds / 60.0; | |
unit = "minutes"; | |
} else { | |
number = seconds; | |
unit = "seconds"; | |
} | |
// to_string is a feature introduced in C++ 11 that converts a number type to | |
// a string type. | |
out = to_string(number) + " " + unit; | |
return out; | |
} | |
int main() { | |
int seconds; | |
string out; | |
cout << "Enter a number of seconds" << '\n'; | |
cin >> seconds; | |
out = get_output(seconds); | |
cout << out << '\n'; | |
// Necessary to prevent warnings from some compilers. | |
// I'm not keeping the program running in a paused state because I am running | |
// it from a CLI, so letting it close is most convenient. | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
that is a very high level code to solve a small problem like that, lol