1 / 11

Riyadh Philanthropic Society For Science Prince Sultan College For Woman

Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 102 Computer Programming II (Lab: Structures). continue. Exercise Write, compile, and run the following program: #include<iostream> #include<cmath> using namespace std;

urbain
Download Presentation

Riyadh Philanthropic Society For Science Prince Sultan College For Woman

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 102 Computer Programming II (Lab: Structures)

  2. continue • Exercise • Write, compile, and run the following program: • #include<iostream> • #include<cmath> • using namespace std; • struct Loan // Loan is called structure tag • { • int ID; // assume an unique integer between 1111-9999 • double amount; // $ amount of the loan • double rate; // annual interest rate • int term; // number of months, length of the loan • }; • double payment(Loan loan); Lab: Structures 1

  3. continue • 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); Lab: Structures 2

  4. 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

  5. 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

  6. continue • Solution • #include<iostream> • #include<cmath> • using namespace std; • struct Loan // Loan is called structure tag • { • int ID; // assume an unique integer between 1111-9999 • 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 Lab: Structures 5

  7. continue • 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; • } Lab: Structures 6

  8. 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

  9. 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

  10. continue • Solution • #include<iostream> • using namespace std; • struct EmployeeRecord • { • double wage_rate; • int vacation; • char status; • }; Lab: Structures 9

  11. 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

More Related