Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 102 Computer Programming II (Lab: Structures) Exercise Write, compile, and run the following program: #include<iostream> #include<cmath> using namespace std; struct Loan { int ID; double amount; double rate; int term; }; // Loan is called structure tag // assume an unique integer between 1111-9999 // $ amount of the loan // annual interest rate // number of months, length of the loan double payment(Loan loan); Lab: Structures continue 1 Exercise int main( ) { Loan loan1; double 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 %): "; cin >> loan1.rate; cout << "Enter the term (number of months, length of the loan): "; cin >> loan1.term; monthly_payment = payment(loan1); continue Lab: Structures 2 Exercise cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); cout << "The monthly payment for loan " << loan1.ID << " is: " << monthly_payment << endl; return 0; } double payment(Loan loan) { loan.rate = loan.rate/1200; // To convert % yearly rate to monthly fraction return loan.amount*loan.rate* (pow((loan.rate+1),loan.term)/(pow((loan.rate+1), loan.term)-1)); } Lab: Structures 3 Exercise Modify the previous program such that it asks users to enter 2 different loans this time and uses a function called initialize_loan to initialize each loan struct. The program should also compute and display the payment for each individual loan and the total monthly payment. Lab: Structures 4 Solution #include<iostream> #include<cmath> using namespace std; struct Loan { int ID; double amount; double rate; int term; }; // Loan is called structure tag // assume an unique integer between 1111-9999 // $ amount of the loan // annual interest rate // number of months, length of the loan double payment(Loan loan); void initialize_loan (Loan& loan); // forgetting the & is a common mistake continue Lab: Structures 5 Solution int main( ) { Loan loan1, loan2; double monthly_payment; cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); initialize_loan(loan1); monthly_payment = payment(loan1); cout << "The monthly payment for loan " << loan1.ID << " is: " << monthly_payment << endl; initialize_loan(loan2); monthly_payment = payment(loan2); cout << "The monthly payment for loan " << loan2.ID << " is: " << monthly_payment << endl; return 0; continue } Lab: Structures 6 Solution double payment(Loan loan) { loan.rate = loan.rate/1200; // To convert % yearly rate to monthly fraction return loan.amount*loan.rate*( pow( (loan.rate+1), loan.term)/ (pow( (loan.rate+1), loan.term) - 1) ); } void initialize_loan (Loan& loan) { cout << "Enter the ID of this loan: "; cin >> loan.ID; cout << "Enter the amount of this loan: "; cin >> loan.amount; cout << "Enter the annual interest rate of this loan (in %): "; cin >> loan.rate; cout << "Enter the term (number of months, length of the loan): "; cin >> loan.term; } Lab: Structures 7 Exercise Write a definition of a structure type for records consisting of a person's wage rate, accrued vacation (which is some whole number of days), and status (which is either hourly or salaried). Represent the status as one of the two char values 'H' and 'S'. Call the type EmployeeRecord. Test your structure. Lab: Structures 8 Solution #include<iostream> using namespace std; struct EmployeeRecord { double wage_rate; int vacation; char status; }; continue Lab: Structures 9 Solution int main() { EmployeeRecord employee1; cout << "Enter the wage rate: "; cin >> employee1.wage_rate; cout << "Enter the vacation: "; cin >> employee1.vacation; cout << "Enter the status: "; cin >> employee1.status; //status should be checked (H or S) cout << "\nYou entered the following information:" << "\nWage_rate: " << employee1.wage_rate << "\nVacation: " << employee1.vacation << "\nStatus: " << employee1.status << endl << endl; return 0; } Lab: Structures 10