0% found this document useful (0 votes)
8 views

Lecture 8

The document discusses techniques for loop programming and solving problems using loops and series. It provides examples of coding problems involving loops, conditional statements, math problems, and calculating trigonometric functions using infinite series. Solutions and explanations are given for various problems involving loops, conditionals, heat transfer calculations and more.

Uploaded by

DANIEL WELDAY
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Lecture 8

The document discusses techniques for loop programming and solving problems using loops and series. It provides examples of coding problems involving loops, conditional statements, math problems, and calculating trigonometric functions using infinite series. Solutions and explanations are given for various problems involving loops, conditionals, heat transfer calculations and more.

Uploaded by

DANIEL WELDAY
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

ASTU

CSE 1062 Fundamentals of Programming

Lecture #8

Spring 2016

Computer Science & Engineering Program


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

• Branching and Looping Practice


– Loop Programming Techniques
– Assignment #2: Heat Transfer
– General Math and Science Problems
– Practice Loops
– Solving Trigonometry using Infinite/Truncated Series
– General Math and Science Problems

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 statement and switch 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.
– 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.
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
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
– )
– A is the surface area (ft2 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
Practice Loops ASTU

• Write a program that create the


following number patterns.

21
General Math and Science Problems ASTU

• To Be Done

22
ASTU

• To Be Done

23
Solving Trigonometry using Series ASTU

• A trigonometric function can be


represented by an infinite series.
• For example, the infinite series of sin x
is
++
• Where x is in unit of radian -<x<

24
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

• Where N is the number of terms to be


retained in the series.
25
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.
26

You might also like