Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 102 Computer Programming II (Lab: Structures)
Lab: Structures1 Exercise Write, compile, and run the following program: #include using namespace std; struct Loan// Loan is called structure tag { int ID;// assume an unique integer between double amount;// $ amount of the loan double rate; // annual interest rate int term;// number of months, length of the loan }; double payment(Loan loan); continue
Lab: Structures2 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: Structures3 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: Structures4 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: Structures5 Solution #include using namespace std; struct Loan// Loan is called structure tag { int ID;// assume an unique integer between double amount;// $ amount of the loan double rate; // annual interest rate int term;// number of months, length of the loan }; double payment(Loan loan); void initialize_loan (Loan& loan); // forgetting the & is a common mistake continue
Lab: Structures6 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: Structures7 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: Structures8 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 Solution #include using namespace std; struct EmployeeRecord { double wage_rate; int vacation; char status; }; 9 continue
Lab: Structures 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; } 10