0% found this document useful (2 votes)
197 views

CSE 1062 Fundamentals of Programming Lecture #6: Spring 2017

The points (2,3) and (2,4) will result in a divide by zero error since the x-coordinates are the same. To avoid this, the program should check if the x-coordinates are equal before calculating the slope.

Uploaded by

Alem Mezgebo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (2 votes)
197 views

CSE 1062 Fundamentals of Programming Lecture #6: Spring 2017

The points (2,3) and (2,4) will result in a divide by zero error since the x-coordinates are the same. To avoid this, the program should check if the x-coordinates are equal before calculating the slope.

Uploaded by

Alem Mezgebo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

ASTU

CSE 1062 Fundamentals of Programming

Lecture #6

Spring 2017

Computer Science & Engineering Program


The School of EE & Computing
Adama Science & Technology University
ASTU

• I/O statements and functions Practice


– Case Study: Taxi Fare Change
– Formatting Number for Program Output
– Casting Operator
– Case Study: Buying Pizza
– General Problems(Maths and Physics)
– Assignment I: Rubber Ball

2
Case Study: Taxi Fare Change ASTU

3
Case Study: Taxi Changes ASTU

• In Addis, to travel from Bole to Kazanchis


the tariff is 2 birr and 70 cents. Suppose
that the taxi assistant(conductor) don’t
have any birr notes but only cents to
return. So he wants the change to
contain as many 50 cents as possible
then 25 cents, 10 cents and 5 cents, in
that order. We want our program to
calculate the change, given the amount
paid.

4
Step 1: Analyze the Problem ASTU

• Input: Amount Paid


• Output: Equivalent Change in 50 cents,
25 cents, 10 cents, and 5 cents.

5
Step 2: Develop a Solution ASTU

• Suppose amount paid is 10 birr,


therefore the change is 7 birr & 30 cents
1. Change=7.30, So cents=730
2. No of 50 cents=730/50=14
3. Remaining Change=730 % 50=30
4. No of 25 cents=30/25=1
5. Remaining Change=30 % 25=5
6. No of 10 cents=5/10=0
7. Remaining Change=5 % 10=5
8. No of 5 cents=5/5=1

6
Step 2: Develop a Solution(Detailed Algorithm) ASTU

1. Prompt the user for input.


2. Get input.
3. Display the entered amount on the screen. (Echo)
4. Calculate the Change in cents
5. Print the Change in cents
6. Compute and print the number of 50 cents.
7. Calculate the remaining change.
8. Compute and print the number of 25 cents.
9. Calculate the remaining change.
10. Compute and print the number of 10 cents.
11. Calculate the remaining change.
12. Print the remaining change(i.e. number of 5 cents)

7
Step 3: Coding ASTU

• To calculate the equivalent change, the


program performs calculations using the
values of a fifty-cent, which is 50;
a twenty-five-cent, which is 25; a ten-
cent, which is 10 .
– Because these data are special and the
program uses these values more than once,
it makes sense to declare them as named
constants.
– Using named constants also simplifies later
modification of the program

8
Step 3: Coding ASTU

1. Write a C++ program to solve the


problem
• Use the following constants in your program

• Use variables where necessary


2. Modify the program to work for
payments made for two or more people
9
Formatting Numbers for Program Output ASTU

• The field width manipulator must be


included for each value in the data
stream sent to cout
• Other manipulators remain in effect until
they are changed
• iomanip header file must be included to
use manipulators requiring arguments

10
Formatting Number for Program Output ASTU

• Test the format manipulators using a


C++ program
Manipulators Number Display Comments

setw(2) 3 | 3| Number fits in the field.

setw(2) 43 |43| Number fits in the field.

setw(2) 143 |143| Field width is ignored.

setw(2) 2.3 |2.3| Field width is ignored.

setw(5) 2.366 | 2.37| Field width of five with two decimal digits.
fixed
setprecision(2)

11
Formatting Number for Program Output ASTU

• Test the format manipulators using a C++ program


cout << "|" << setw(10) << fixed
<< setprecision(3) << 25.67 << "|";
Manipulators Number Display Comments
setw(5) 42.3 |42.30| Number fits in the field with the specified precision. Note
fixed that the decimal point takes up one location in the field wid
setprecision(2) th.
setw(5) 142.364 |1.4e+002| Field width is ignored, and scientific notation is used with
setprecision(2) the setprecision manipulator.
setw(5) 142.364 |142.36| Field width is ignored, but precision specification is used.
fixed The setprecision manipulator specifies the number of
setprecision(2) fractional digits.
setw(5) 142.366 |142.37| Field width is ignored, but precision specification used. The
fixed setprecision manipulator specifies the number of fractional
setprecision(2) digits. (Note the rounding of the last decimal digit.)
setw(5) 142 | 142| Field width is used; fixed and setprecision manipulators
fixed are irrelevant because the number is an integer that specifie
setprecision(2) s the total number of significant digits (integer plus
fractional digits).
12
Formatting Number for Program Output ASTU

• setiosflags manipulator: Allows additional


formatting:
– Right or left justification
– Fixed display with 6 decimal places
– Scientific notation with exponential display
– Display of a leading + sign
• Parameterized manipulator: One which requires
arguments, or parameters

13
Formatting Number for Program Output ASTU

14
Formatting Number for Program Output ASTU

• Manipulators can also be set using the ostream


class methods
• Separate the cout object name from the method
name with a period
Example:
cout.precision(2)

15
Formatting Number for Program Output ASTU

16
Cast Operator ASTU

• Cast operator: A unary operator that forces the


data to the desired data type
• Compile-time cast
– Syntax: dataType (expression)
– Example: int(a+b)

17
Cast Operator ASTU

• Run-time cast: The requested conversion is


checked at run time and applied if valid
– Syntax:
staticCast<data-type> (expression)
– Example:
staticCast<int>(a*b)

18
Case Study: Buying Pizza ASTU

• The large “economy” size of an item is


not always a better buy than the smaller
size.
– This is particularly true when buying pizzas.
– Pizza sizes are given as the diameter of the
pizza in inches.

19
Case Study: Buying Pizza ASTU

• However, the quantity of pizza is


determined by the area of the pizza
– Most people cannot easily estimate the
difference in area between a ten-inch pizza
and a twelve-inch pizza
– So cannot easily determine which size is the
best buy—that is, which size has the lowest
price per square inch.

20
Case Study: Buying Pizza ASTU

• Build, Run and Test the following


program
• Understand the logic

21
Case Study: Buying Pizza ASTU

22
Case Study: Buying Pizza ASTU

23
Case Study: Buying Pizza ASTU

24
Case Study: Buying Pizza ASTU

• Suppose that restaurants offer both


round pizzas and rectangular pizzas.

• The code is on the next slides


• Note that the function name unitPrice
has been overloaded so that it applies
to both round and rectangular pizzas.
• Build, Run and Test the Program
25
Case Study: Buying Pizza ASTU

26
Case Study: Buying Pizza ASTU

27
Case Study: Buying Pizza ASTU

28
General Problems ASTU

• Develop a calculator program using


function (use five functions add (),
multiply (), divide (), subtract () and
module ().

29
General Problems ASTU

• Write a C++ program to calculate and


display the value of the slope of the line
connecting two points with the
coordinates (3,7) and (8,12).
• After verifying the output your program
produces, modify it to determine the
slope of the line connecting the points
(2,10) and (12,6).

30
General Problems ASTU

• What will happen if you use the points


(2,3) and (2,4)
• Change its output to this:
– The value of the slope is xxx.xx
– The xxx.xx denotes placing the calculated
value in a field wide enough for three places
to the left of the decimal point and two
places to the right of it.

31
Assignment #1 ASTU

• When a particular rubber ball is dropped


from a given height (in meters), its
impact speed (in meters/second) when it
hits the ground is given by this formula:
𝑠𝑝𝑒𝑒𝑑 = 2𝑔ℎ
• where g is the acceleration
caused by gravity and h is the
height.
• The ball then rebounds to 2/3 the
height from which it last fell.

32
Assignment #1 … ASTU

• Write, test, and run a C++ program that


calculates and displays the impact speed
of the first three bounces and the
rebound height of each bounce.
• Test your program by using an initial
height of 2.0 meters.
• Run the program twice, and compare the
results for dropping the ball on Earth
(g = 9.81 m/s2) and on the moon
(g = 1.67 m/s2).
33

You might also like