3. Structures In order to explain how a structure works, let\'s look at an examp
ID: 3704630 • Letter: 3
Question
3. Structures In order to explain how a structure works, let's look at an example. A loan usually has several components in it. For simplicity, we assume for now that no name or personal identification number is involved. Instead, we use the loan number to identify a loan. The components of a loan in our example are: a loan ID, assuming an integer, .the loan amount, which for now we assume is a float, .the interest rate, another float, and the term of the loan, which represents the integer of months until the loan is paid in full. One thing everyone is concerned about in a loan is the monthly payment. A program that computes the monthly payment for a loan would be very useful. One way to compute the monthly payment is: (rate + 1)term (rate + 1)term1 payment amount* rate Note that your bank may use a different formula to compute loan payment, but this is a good example. As you may have noticed in the description of the loan components, it seems that we could have a new variable type, let's say Loan, which includes all of the four components in it. Thus, we wish to define a new type of variable that can keep four variables of various types within one. A struct allows to do this verv easiExplanation / Answer
#include <iostream>
#include <cmath>
using namespace std;
struct Loan
{
int id, term;
float rate, amount;
};
float payment(Loan l1);
int main() {
Loan loan1;
float monthly_payment;
cout<<"Enter the ID of this loan:";
cin>>loan1.id;
cout<<"Enter the amount of this loan:";
cin>>loan1.amount;
cout<<"Enter the annual interest rate of this loan in % (e.g., 5.4):";
cin>>loan1.rate;
cout<<"Enter the term (number of months, length of loan):";
cin>>loan1.term;
monthly_payment = payment(loan1);
cout<<"The monthly payment for loan " << loan1.id << " is: "<<monthly_payment;
return 0;
}
float payment(Loan l1)
{
l1.rate = l1.rate/1200;
return l1.amount * l1.rate * (pow((l1.rate+1), l1.term))/(pow((l1.rate+1), l1.term)-1);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.