0% found this document useful (0 votes)
9 views33 pages

C++ Programming Fundamentals: I/O & Cases

The document outlines a lecture on programming fundamentals at Adama Science & Technology University, focusing on I/O statements, case studies, and formatting numbers in C++. It includes specific case studies such as calculating taxi fare change and evaluating pizza sizes based on area. Additionally, it provides assignments for students to apply their programming skills in practical scenarios.

Uploaded by

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

C++ Programming Fundamentals: I/O & Cases

The document outlines a lecture on programming fundamentals at Adama Science & Technology University, focusing on I/O statements, case studies, and formatting numbers in C++. It includes specific case studies such as calculating taxi fare change and evaluating pizza sizes based on area. Additionally, it provides assignments for students to apply their programming skills in practical scenarios.

Uploaded by

ramkeltesfa246
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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 Fare Change ASTU

3
Case Study: Taxi Changes ASTU

• In Addis, to travel from Bole to Kazanchi


s the tariff is 2 birr and 70 cents. Suppos
e that the taxi assistant(conductor) don’t
have any birr notes but only cents to ret
urn. So he wants the change to contain
as many 50 cents as possible then 25 ce
nts, 10 cents and 5 cents, in that order.
We want our program to calculate the c
hange, 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, therefor


e the change is 7 birr & 30 cents

Change=7.30, So cents=730

No of 50 cents=730/50=14

Remaining Change=730 % 50=30

No of 25 cents=30/25=1

Remaining Change=30 % 25=5

No of 10 cents=5/10=0

Remaining Change=5 % 10=5

No of 5 cents=5/5=1

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

– Prompt the user for input.


– Get input.
– Display the entered amount on the screen. (Echo)
– Calculate the Change in cents
– Print the Change in cents
– Compute and print the number of 50 cents.
– Calculate the remaining change.
– Compute and print the number of 25 cents.
– Calculate the remaining change.
– Compute and print the number of 10 cents.
– Calculate the remaining change.
– Print the remaining change(i.e. number of 5 cents)
7
Step 3: Coding ASTU

• To calculate the equivalent change, the p


rogram performs calculations using the v
alues of a fifty-cent, which is 50; at
wenty-five-cent, which is 25; a ten-cent,
which is 10 .
– Because these data are special and the prog
ram uses these values more than once, it m
akes sense to declare them as named const
ants.
– Using named constants also simplifies later
modification of the program
8
Step 3: Coding ASTU

1. Write a C++ program to solve the probl


em

Use the following constants in your program


Use variables where necessary
2. Modify the program to work for pay
ments made for two or more people
9
Formatting Numbers for Program Output ASTU

• The field width manipulator must be incl


uded for each value in the data stream s
ent 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 Numb Displa Comments
er y
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


fixed digits.
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.
fixed Note that the decimal point takes up one location in
setprecision(2 the field width.
)
setw(5) 142.364 |1.4e+002 Field width is ignored, and scientific notation is used
setprecision(2 | with the setprecision manipulator.
)
setw(5) 142.364 |142.36| Field width is ignored, but precision specification is
fixed used.
setprecision(2 The setprecision manipulator specifies the number of
) fractional digits.
setw(5) 142.366 |142.37| Field width is ignored, but precision specification used.
fixed The setprecision manipulator specifies the number of
setprecision(2 fractional digits. (Note the rounding of the last decimal
) digit.)
setw(5) 142 | 142| Field width is used; fixed and setprecision
fixed manipulators are irrelevant because the number is an
setprecision(2 integer that specifies the total number of significant
12
) digits (integer plus
Formatting Number for Program Output ASTU

• setiosflags manipulator: Allows additional for


matting:
– 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 ar
guments, or parameters

13
Formatting Number for Program Output ASTU

14
Formatting Number for Program Output ASTU

• Manipulators can also be set using the ostream cl


ass methods
• Separate the cout object name from the method
name with a period
Example:
[Link](2)

15
Formatting Number for Program Output ASTU

16
Cast Operator ASTU

• Cast operator: A unary operator that forces the da


ta 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 checke


d 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 n


ot always a better buy than the smaller si
ze.
– 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 determ


ined by the area of the pizza
– Most people cannot easily estimate the diff
erence in area between a ten-inch pizza an
d 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 progra


m
• 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 r
ound 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 f


unction (use five functions add (), m
ultiply (), divide (), subtract () and modul
e ().

29
General Problems ASTU

• Write a C++ program to calculate and di


splay the value of the slope of the line co
nnecting two points with the coordinate
s (3,7) and (8,12).
• After verifying the output your program
produces, modify it to determine the slo
pe of the line connecting the points (2,1
0) 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 [Link]
– The [Link] denotes placing the calculated v
alue in a field wide enough for three places
to the left of the decimal point and two pla
ces to the right of it.

31
Assignment #1 ASTU

• When a particular rubber ball is dropped


from a given height (in meters), its impa
ct speed (in meters/second) when it hits
the ground is given by this formula:

• where g is the acceleration cause


d by gravity and h is the height.
• The ball then rebounds to 2/3 the heig
ht 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 rebou
nd height of each bounce.
• Test your program by using an initial hei
ght of 2.0 meters.
• Run the program twice, and compare th
e 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