CSE 1062 Fundamentals of Programming Lecture #8: Spring 2017

Download as pdf or txt
Download as pdf or txt
You are on page 1of 33

ASTU

CSE 1062 Fundamentals of Programming

Lecture #8

Spring 2017

Computer Science & Engineering Program


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

• Branching and Looping Practice


– Loop Programming Techniques
– Practice if statements
– Assignment #2: Heat Transfer
– General Math and Science Problems
– Drawing Patterns with Nested For Loops
– Recursion vs. Iteration
– Solving Trigonometry using Infinite/Truncated Series

2
Loop Programming Techniques ASTU

• These techniques are suitable for pretest


loops (for and while):
– Interactive input within a loop
• Includes a cin statement within a while or for
loop
– Selection within a loop
• Using a for or while loop to cycle through a set
of values to select those values that meet some
criteria

3
Interactive Input within a Loop ASTU

4
Selection in a Loop ASTU

5
Evaluating Functions of One Variable ASTU

6
Loop Programming Techniques ASTU

• Interactive loop control


– Variable is used to control the loop
repetitions
– Provides more flexibility at run-time
• Random numbers and simulation
– Pseudorandom generator used for
simulators
– C++ functions: rand(); srand()

7
Interactive Loop Control ASTU

8
Random Number and Simulation ASTU

• Example #1

9
Random Number and Simulation ASTU

Example #2 : The Algorithm


Initialize a heads count to 0
Initialize a tails count to 0
For 1000 times
Generate a random number between 0 and 1
If the random number is greater than 0.5
consider it a head and add 1 to the heads count
Else
consider it a tail and add 1 to the tails count
End If
End For
Calculate the percentage of heads as the number of heads ÷ 1000 × 100%
Calculate the percentage of tails as the number of tails ÷ 1000 × 100%
Print the percentage of heads and tails calculated

10
Random Number and Simulation ASTU

• Example #2: Source Code

11
Random Number and Simulation ASTU

• Example #2: Source Code

12
Practice if statements ASTU

Write appropriate if statements for the


following conditions
– If the temperature is above 100 degrees, display
the message “above the boiling point of
water”; else, display the message “below the
boiling point of water.”
– If the number is positive, add the number to
the variable positivesum ; else, add the number
to the variable negativesum .
– If the difference between volts1 and volts2 is
less than 0.001, set the variable approx to 0;
else, calculate approx as the quantity (volts1 -
volts2) / 2.0.

13
General Math and Science Problems ASTU

• A student’s letter grade is calculated


according to the following schedule:
– Using this information, write a C++ program
that accepts a student’s numerical grade,
converts the numerical grade to an
equivalent letter grade, and displays the
letter grade.
Numerical Grade Letter Grade
Greater than or equal to 90 A
Less than 90 but greater than or equal to 80 B
Less than 80 but greater than or equal to 70 C
Less than 70 but greater than or equal to 60 D
Less than 60 F
14
General Math and Science Problems ASTU

• Modify the previous program


– Use switch statement
– Based on the obtained letter grade of a
student display comment

Letter Grade Comment


A Excellent
B Very Good
C Good
D Poor
F Fail

15
General Math and Science Problems ASTU

• Write a C++ program that accepts the


angle of the line as user input
– determine and display
the correct quadrant for
the input data.
Angle from the Positive X-Axis Quadrant

Between 0 and 90 degrees I


Between 90 and 180 degrees II
Less than 180 but greater than or equal to 270 degrees III
Less than 270 but greater than or equal to 360 degrees IV

– Note : If the angle is exactly 0, 90, 180, or 270


degrees, the corresponding line doesn’t reside in
any quadrant but lies on an axis. 16
Assignment #2: Heat Transfer ASTU

• The transfer of heat by the movement


(currents) of a gas or liquid is referred to as
heat convection.
• The heat transferred per unit area of a
substance is given by this formula
𝑞 = ℎ𝐴(𝑇𝑠 −𝑇𝑎 )
– q is the heat transfer rate (watts or Joules/sec).
– h is the convective heat transfer coefficient
– 𝐵𝑇𝑈/ℎ𝑟 − 𝑓𝑡℉ 𝑜𝑟 𝑤𝑎𝑡𝑡𝑠/𝑚2 ℃)
– A is the surface area (ft 2 or m2).
– Ts is the surface temperature (°F or °C).
– Ta is the ambient temperature (°F or °C).
-Heat Transfer Notes
http://www.freestudy.co.uk/heat%20transfer/

17
Assignment #2: Heat Transfer ASTU

• The program should


– accept a substance’s surface area,
– a substance’s surface temperature
– the ambient air temperature as inputs and
– displays the heat transfer rate through air
• Users should have three choices for
entering the surface area:
– A rectangular area
– An elliptical area
– Other

18
Assignment #2: Heat Transfer ASTU

• If the user selects 1


• the program should ask the user to enter the
surface’s length and width,
• and the program calculates surface area as length
times width.
• If the user selects 2,
• the program should ask the user to enter the
surface’s major and minor axis,
• and the program calculates the surface area as
π(major axis)(minor axis).
• If the user selects 3 (Other),
• the program should ask the user to enter the
surface area.

19
Assignment #2: Heat Transfer ASTU

• The heat transfer rate should then be


calculated and displayed, using the
convective heat transfer coefficient of
8.7 watts/m2 °C, which should be defined as
the symbolic constant AIRCONV.
• As a test case
• determine the heat transfer rate away from a chip in
a computer’s console.
• The chip has a surface temperature of 44°C, and the
ambient temperature maintained by the console’s fan
is 40°C.
• The rectangular chip has a length of 2 cm
and a width of 2 cm.
20
Drawing Patterns with Nested For Loops ASTU

• Write a program that uses for statements


to print the following patterns separately,
one below the other. Use for loops to
generate the patterns.
– All asterisks(*) should be printed by a single
statement of the form cout << '*';
• (this causes the asterisks to print side by side).
[Hint: The last two patterns require that each line
begin with an appropriate number of blanks.

21
Drawing Patterns with Nested For Loops ASTU

• Write a program that create the


following number patterns.

22
Recursion vs. Iteration(Loops) ASTU

• Recursion is defining something in


terms of itself. It sounds a bit odd, but
very natural in fact.
• Common Examples are
– Factorial of a Number and
– Fibonacci Numbers

23
Recursion vs. Iteration(Loops) ASTU

In mathematics, the factorial function is defined as


follows:

f(n) = 1 if n = 1 base case


n x f(n - 1) if n > 1 recursive case
f(n)

f(n) = n X (n-1) X (n-2) X……….X 2 X 1


(n – 1)!
f(n – 1)

24
Factorial: Using Recursion ASTU

25
Factorial: Using Recursion ASTU

Fact(5) 120

5* Fact(4) 24

4* Fact(3) 6

3* Fact(2) 2

2* Fact(1) 1
Factorial: Using Iteration ASTU

27
Fibonacci Numbers ASTU

Fibonacci numbers
n f(n)
1 if n = 0 0 1
1 if n = 1 base case 1 1
f(n)
f(n – 1) + f(n - 2) recursive case 2 2
3 3
4 5
5 8
6 13
…… ……

28
Fibonacci Numbers ASTU

Fib(5) 8

Fib(4) 5 Fib(3) 3

Fib(3) 3 Fib(2) 2 Fib(2) 2 1

2 Fib(2) 1 1 1 1 1

1 1
Fibonacci Numbers ASTU

• Write a c++ program to calculates the


nth Fibonacci number
A. Recursively
B. Iteratively

30
Solving Trigonometry using Series ASTU

• A trigonometric function can be


represented by an infinite series.
• For example, the infinite series of sin x
is
𝑥3 𝑥5 𝑥7 𝑥 2𝑛−1
sin 𝑥 = 𝑥 − + + = σ∞
𝑛=1 (−1) 𝑛−1
3! 5! 7! 2𝑛−1 !
• Where x is in unit of radian -∞<x< ∞

31
General Math and Science Problems ASTU

• Since a computer cannot sum up the


infinite number of terms in the series
• the infinite series is truncated after a finite number of
terms.
• The truncated series, therefore, can only calculate the
function to the precision of the floating point on the
computer.
• The truncated infinite series for sin
𝑁
2𝑛−1
𝑛−1
𝑥
𝑠𝑖𝑛𝑥 = ෍ (−1)
2𝑛 − 1 !
𝑛=1
• Where N is the number of terms to be
retained in the series.
32
Solving Trigonometry using Series ASTU

• Write a c++ program that:


– Reads in a value of x in degrees and then
calculates the sine of x using the sin built in
function .
– Next calculate the sine of x using above
truncated infinite series to the prescribed
accuracy (N)
• which should be input by the user.
– Output the values of the sine of x calculated
using both intrinsic function and the
truncated series, and the number of terms
of the truncated series required.

33

You might also like