PL1 Lec4
PL1 Lec4
PL1 Lec4
Ghada Khoriba
[email protected]
Reference
Main reference book:
• Problem Solving with C++, 10th Edition, Global Edition Walter Savitch
Chapters 1-9.
• On https://pearson.turingscraft.com/
CourseID (a.k.a. "Section Access Code"): SMSDEFINS-7678-0
2
Course
Contents
01. Introduction 03. Functions
› PROGRAMMING AND PROBLEM- › Predefined / programmer defined
SOLVING › Scope and local variables
› High-Level Languages vs low-level › Call by reference
language
› Introduction to C++ 04. Arrays
› Declaration
02. C++ Basics › With loops / functions
› Search / Sort
› Variables and assignments / Data types
and expressions
› I/O 05. Pointers and Dynamic Arrays
› Branching Mechanisms & Boolean
06. Strings and vectors
expressions / Loops
07. I/O streams, using files
3
Recap
4
Self-assessment Quiz
int number = 2;
int valueProduced = 2 * (number++);
cout << valueProduced << endl; 4
cout << number << endl; 3
int number = 2;
int valueProduced = 2 * (++number);
6
cout << valueProduced << endl; 3
cout << number << endl;
5
Self-assessment Quiz
Q1- What is the output of the following (when embedded in a complete
program)?
int n = 1;
do 123
cout << n << " ";
while (++n <= 3);
int n = 1;
do
cout << n << " ";
while (n++ <= 3) 1234
6
Self-assessment Quiz
for (double sample = 2; sample > 0; sample -= 0.5)
cout << sample << " ";
2 1.5 1. 0.5
}
7
Write C++ program to find Armstrong numbers between 1 to n
9
Benefits of Top Down Design
11
e scaled by modulus and addition. For example, to simulate rolling
predefined functions, example
ided die we could use the following:
int die = (rand() % 6) + 1;
The number of seconds elapsed since January 1, 1970 is known as Unix time.
12
DISPLAY 4.3 A Function Definition
PROGRAMMER-
13 cout << "Enter the number of items purchased: ";
14 cin >> number;
15 cout << "Enter the price per item $";
16 cin >> price; function call
DEFINED
17
18 bill = totalCost(number, price);
19
FUNCTIONS
20 cout.setf(ios::fixed);
21 cout.setf(ios::showpoint);
22 cout.precision(2);
23 cout << number << " items at "
24 << "$" << price << " each.\n"
25 << "Final bill, including tax, is $" << bill
26 << endl;
27
28 return 0; function heading
29 }
30
31 double totalCost(int numberPar, double pricePar)
32 {
33 const double TAX_RATE = 0.05; //5% sales tax
34 double subtotal; function function
35 body definition
36 subtotal = pricePar * numberPar;
37 return (subtotal + subtotal * TAX_RATE);
38 } 13
Function Definition
n Provides the same information as the declaration
n Describes how the function does its task
function header
n Example:
function body
or
n expression is a variable containing the
calculated value
n Example:
return subtotal + subtotal * TAX_RATE;
int main()
{
double price, bill;
int number; 1. Before the function is called, values of
cout << "Enter the number of items purchased: "; the variables number and price are set
cin >> number; to 2 and 10.10, by cin statements (as
cout << "Enter the price per item $"; you can see the Sample Dialogue in
cin >> price; Display 4.3)
bill = totalCost (number, price); 2. The function call executes and the value
2 10.10 of number (which is 2) plugged in for
cout.setf (ios::fixed); numberPar and value of price (which
cout.setf (ios::showpoint); is 10.10) plugged in for pricePar.
cout.precision(2);
cout << number << " items at "
<< "$" << price << " each.\n"
21.21 << "Final bill, including tax, is $" << bill
<< endl;
return 0;
} 2 10.10
double totalCost (int numberPar, double pricePar)
3. The body of the function executes
{
with numberPar set to 2 and
const double TAX_RATE = 0.05; //5% sales tax
pricePar set to 10.10, producing the
double subtotal;
value 20.20 in subtotal.
subtotal = pricePar * numberPar;
4. When the return statement is executed,
return (subtotal + subtotal * TAX_RATE);
the value of the expression after return is
}
evaluated and returned by the function. In
21.21 this case, (subtotal + subtotal *
TAX_RATE) is (20.20 + 20.20*0.05)
or 21.21.
5. The value 21.21 is returned to where the function was invoked. The result is that
totalCost (number, price) is replaced by the return value of 21.21. The
value of bill (on the left-hand side of the equal sign) is set equal to 21.21 when 17
the statement bill = totalCost (number, price); finally ends.
Function Declaration
Type_Returned Function_Name(Parameter_List);
Function_Declaration_Comment
Function Definition
14. Write a function definition for a function isRootOf that takes two arguments
of type int and returns a bool value. The function returns true if the first
argument is the square root of the second; otherwise, it returns false.
4.4 bool
PROCEDURAL
isRootOf(intABSTRACTION
rootnum, int number) {
return (number == rootnum * rootnum); }
The cause is hidden, but the result is well known.
OVID, Metamorphoses IV