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

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick Strings #include using namespace std; int main () { string word; cout
Advertisements

Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 102 Computer Programming II (Lab:
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
1 Objectives Understand streamed input and output.
Data Management and File Organization
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
// Functions that take no arguments #include using namespace std; void function1(); void function2( void ); int main() { function1(); function2(); return.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
Overview Order of presentation different than published handouts Run a program on ccc Finish Arithmetic operations Data types integer char floating point.
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
Computer Science 1620 Programming & Problem Solving.
1 9/25/06CS150 Introduction to Computer Science 1 Nested Ifs, Logical Operators, exit() Page 194.
1 CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
C++ is Fun – Part Eight at Turbine/Warner Bros.! Russell Hanson.
1 Session-9 CSIT 121 Spring 2006 Lab Demo (first 20 minutes) The correct way to do the previous lab sheet Function calls Calling void functions Calling.
1 11/8/06CS150 Introduction to Computer Science 1 More Functions! page 343 November 8, 2006.
1 10/30/06CS150 Introduction to Computer Science 1 Functions Chapter 3, page 313.
Software Engineering 1 (Chap. 1) Object-Centered Design.
PRINCIPLES OF PROGRAMMING Revision. A Computer  A useful tool for solving a great variety of problems.  To make a computer do anything (i.e. solve.
CS1201: Programming Language 2 Recursion By: Nouf Almunyif.
Chapter 5 Functions For All Subtasks. Void functions Do not return a value. Keyword void is used as the return type in the function prototype to show.
Module 4: Structures ITEI222 Advanced Programming.
Lecture 15: Control Flow (cont). 2 Lecture Contents: t Design and implementation of programs illustrating linear algorithms, branch algorithms and loop.
Data Structures and Debugging Dr. Nancy Warter-Perez June 18, 2003.
Previously Repetition Structures While, Do-While, For.
 Introduction to Computer Science COMP 51 – Fall 2012 – Section 2 Structures.
First steps Jordi Cortadella Department of Computer Science.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 102 Computer Programming II (Lab:
Control Structures (B) Topics to cover here: Sequencing in C++ language.
C++ Programming, Namiq Sultan1 Chapter 3 Expressions and Interactivity Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin.
1 CS161 Introduction to Computer Science Topic #4.
Structures - Part II aggregate operations arrays of type struct nested structures compared to classes.
Chapter 3 More Flow of Control Goals: To analyze the use of Boolean expressions To analyze the use of Boolean expressions To introduce the notion of enumerated.
Objective: Students will be able to: Declare and use variables Input integers.
1 8/31/05CS150 Introduction to Computer Science 1 Hello World!
1 09/27/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Simple Functions Writing Reuseable Formulas. Problem Using OCD, design and implement a program that computes the area and circumference of an Australian.
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
April 11, 2005 More about Functions. 1.Is the following a function call or a function header? calcTotal(); 2.Is the following a function call or a function.
12/14/2016CS150 Introduction to Computer Science 1 Announcements  Website is up!   All lecture slides, assignments,
Introduction to Database Systems
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Selection Control Structures 2 (L09) * Nested if Statements * The if-else Chain * Exercise: if, if-else, nested if, and if-else chain Selection Control.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
Topic 4 Data Structures Program Development and Design Using C++, Third Edition.
Infinite for Loop If you omit the test condition, the value is assumed to be TRUE so the loop will continue indefinitely unless you provide some other.
Basic concepts of C++ Presented by Prof. Satyajit De
Structured Data.
MT262A Review.
Dept. of Computer & Information Sciences
Arrays Part-1 Armen Keshishian.
C++ Arrays.
Screen output // Definition and use of variables
CS150 Introduction to Computer Science 1
הרצאה 03 אבני היסוד של תוכנית ב- C
Starting Out with C++: From Control Structures through Objects
CS150 Introduction to Computer Science 1
Lab 1 Introduction to C++.
CS150 Introduction to Computer Science 1
Let’s all Repeat Together
Life is Full of Alternatives
Let’s all Repeat Together
Arithmetic Operations
Life is Full of Alternatives
Life is Full of Alternatives
Presentation transcript:

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