$\require{cancel}$

An Example

• We will be covering all of this later, but I would like to work through an example.
• I need to borrow some money from my mom. She would like to set up a regular loan. As part of this she would like a program that calculates the interest due. Can you help write this program?
• What are the problem solving steps we discussed?
• Understand the problem
• Develop a solution
• Imiplement the solution
• Use/Maintenance
• Do we understand the problem?
• What questions might we need answered?
• Let's assume the following conditions:
• A simple interest loan.
• Interest rate given as a yearly amount.
• Time given in years
• Principal given in dollars.
• Can we work through an example?
• I need the simple interest formula, i = prt
• A loan of $100 for 1 year at 5% interest would require a payment of$100 principal and $5 interest. • Work through it. • Principal:$100
Time : 1 year
Rate: 5% or 0.05
i = prt
i = 100 * 1 * 0.05 = 5.
payment = principal + interest
= 100 + 5
= $105  • Can I develop an algorithm? • I maintain that this is hard since we don't know how to program. • Let's write instructions for a person to do this. • We will use terms like ask, compute using, tell.... • A first pass • calculate the interest for the loan.  • This is probably too high level. • It really doesn't say anything just restates the problem. • A second pass • Get input Compute result Print output • This is probably useless as well. • This probably solves 70% of all problems. • It really lacks detail. • Try again • Get principal Get time Get rate output principal, rate, time compute interest = principal * rate * time payment = principal + interest output interest and payment  • Given these instructions can you run the test case? • This looks good to me. • one last try • #include <iostream> #include <iomanip> using namespace std; int main() { float principal{0}, interest{0}, rate{0}, time{0}, payment{0}; cout << fixed << setprecision(2); cout << "Enter the principal: "; cin >> principal; cout << endl; cout << "Enter the interest rate: "; cin >> rate; cout << endl; cout << "Enter the time: "; cin >> time; cout << endl; interest = principal * rate * time; payment = interest + principal; cout << "Principal:$" << principal << endl;
cout << "Rate: " << rate  << "%" << endl;
cout << "Time: " << time  << " years" << endl;
cout << "Interest: $" << interest << endl; cout << "Payment:$" << payment << endl;

return 0;
} 
• Can you read this?
• This is code, which IS an algorithm, but not useful for our purposeses here.
• Write the program