0% found this document useful (0 votes)
19 views27 pages

Lecture6 (Mar 27 2016)

The document discusses a lecture on C++ programming fundamentals. It covers input/output statements and functions practice with examples, formatting numbers for output, casting operators, buying pizza and slope calculation problems, and an assignment on modeling rubber ball bounces.

Uploaded by

DANIEL WELDAY
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views27 pages

Lecture6 (Mar 27 2016)

The document discusses a lecture on C++ programming fundamentals. It covers input/output statements and functions practice with examples, formatting numbers for output, casting operators, buying pizza and slope calculation problems, and an assignment on modeling rubber ball bounces.

Uploaded by

DANIEL WELDAY
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

ASTU

CSE 1062 Fundamentals of Programming

Lecture #6

Spring 2016

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

Thus write a c++ program that that calculates the


change, given the amount paid.

3
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

4
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)
5
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

6
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)

7
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
setprecision(2) width.
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 speci-
setprecision(2) fies the total number of significant digits (integer plus
fractional digits).
8
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

9
Formatting Number for Program Output ASTU

10
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)

11
Formatting Number for Program Output ASTU

12
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)

13
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)

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

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

16
Case Study: Buying Pizza ASTU

• Build, Run and Test the following


program
• Understand the logic

17
Case Study: Buying Pizza ASTU

18
Case Study: Buying Pizza ASTU

19
Case Study: Buying Pizza ASTU

20
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
21
Case Study: Buying Pizza ASTU

22
Case Study: Buying Pizza ASTU

23
Case Study: Buying Pizza ASTU

24
General Problems ASTU

• Develop a calculator program using


function (use five functions add (),
multiply (), divide (), subtract () and
module ().
• Write a c++ program that accepts
hydrogen then your program should:
– Calculate and print phlevel
– Should print message “Acid” if phLevel
below 7; “Neutral” if phLevel is equal 7 and
“Base” otherwise

25
General Problems ASTU

• Write a C++ program that accepts two


coordinates and calculates and displays
the slope of the line connecting the two
points. [hint ]

26
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:

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

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).
27

You might also like