chapter 3 applications & logical constructs. 2 application 1 temperature conversion: write a...
Post on 22-Dec-2015
229 views
TRANSCRIPT
2
Application 1
Temperature Conversion: Write a program that will convert a Celsius temperature to the corresponding Fahrenheit temperature.
SOLUTION: Assume that the temperature converting formula is given by
F = (9/5) C + 32INPUT: A temperature in degrees CelsiusOUTPUT: A temperature in degrees Fahrenheit Programs T_conv_1.f and T_conv_2.f BASIC STEPS FOR ALGORITHM
STEP1 --> Enter CSTEP2 --> Calculate FSTEP3 --> Display F
3
Application 2
Area of a Triangle: Write a program to read values for the three sides a, b, c of a triangle and then calculate its perimeter and its area. These should be displayed together with the values of a, b, c using appropriate labels. (for the area, you might use Hero’s formula for the area of a triangle:
where s is one-half the perimeter.)
SOLUTION: See the source code area_tri.f for the solution
))()(( csbsassarea
4
Selective Execution
Three basic methods of control are used in designing algorithms and
programs:
1) Sequence: Each step are performed exactly once and steps are performed in a strictly sequential manner.
2) Selection: One of a number of alternative actions is selected and executed
3) Repetition: One or more steps are performed repeatedly.
5
Logical Expressions
Logical expressions may be either simple or compound. Simple logicalexpressions are logical constants or variables or relational expressionsof the form.
expression1 relational-operator expression2
where both expression1 and expression2 are numeric or character (or
logical) expressions, and the relational-operator may be any of thefollowing:
6
Simple Logical Expressions
Symbol Meaning< or .LT. Is less than
> or .GT. Is greater than
== or .EQ. Is equal to
<= or .LE. Is less than or equal to
>= or .GE. Is greater than or equal to
/= or .NE. Is not equal to
7
Simple Logical Expressions
Examples: x < 6.7 number == 1999ASCII uses codes in the range 0 through 255 (i.e. A=65,
B=66, …Z=90).
(See complete table of ASCII characters in Appendix D of Ellis’s book)
”A” < ”G” is true logical expression ”cat” < ”dog” true ”June” < ”July” true
8
Compound Logical Expressions
They are formed by combining logical expressions by using the logical
operators .NOT. (negation)
.AND. (conjunction).OR. (disjunction).EQV. (equivalence)
.NEQV. (nonequivalence)
all possible values are displayed in truth tables (next page)
9
Compound Logical Expressions
p q p.AND.q p.OR.q p.EQV.q p.NEQV.q
.TRUE. .TRUE. .TRUE. .TRUE. .TRUE. .FALSE.
.TRUE. .FALSE. .FALSE. .TRUE. .FALSE. .TRUE.
.FALSE. .TRUE. .FALSE. .TRUE. .FALSE. .TRUE.
.FALSE. .FALSE. .FALSE. .FALSE. .TRUE. .FALSE.
10
IF Constructs
A simple IF construct is as follows
if (logical-expression) then statement-sequence end if
EXAMPLE: if (X >= 0) then Y=X * X Z=sqrt(X) end if
11
Logical IF Statement
Fortran also provides a simpler IF construct called Logical IF statement
if (logical-expression) statement
if (1.5 <= X .AND. X<= 2.5) print *, X
12
General form of the IF construct
if (logical-expression) then statement-sequence1
else statement-sequence2
end if
If the logical expression is true, statement-sequence1 is executed and
statement-sequence2 is bypassed.
13
Applications
(Problem #1) Solve the quadratic equation
using an IF construct.
SOLUTION: Read : The coefficients A, B, and COutput : The roots of the equation
Check the source code Quad_eq_1.f !
02 CBxAx
14
Applications
(Problem #2) Rank two numbers Two real data values, X and Y are given. Write a
program that finds the ranking between two entered numbers.
Use If construct for the solution in your program.
Check the source code rank_two.f !
15
IF-ELSE IF Constructs It is possible to use the IF construct to design selection
structures that contain more than two alternatives.
if (logical-expression1) then
statement-sequence1
else if (logical-expression2) then
statement-sequence2
else if (logical-expression3) then
statement-sequence3...
else statement-sequencen
end if
16
IF-ELSE IF Constructs
When an if-else if construct is executed, the logical expressions are evaluated to determine the first expression that is true; the associated sequence of statements is executed, and execution then continues with the next statement following the next construct. If none of the logical expressions is true, the statement sequence associated with the else statement is executed, and execution then continues with the statement following the construct.
This if construct thus implements an n-way selection structure in which exactly one of statement-sequence1, statement-sequence2, statement-sequence3,…, statement-sequencen is executed.
17
Named Constructs
if and if-else if constructs may be named by attaching a label at the
beginning and end of the construct
name: if (logical-expression) thenend if name
For example: update: if (x > largest) then largest=x position=N end if update
18
Named Constructs
Naming prevents the possible mixing within the nested if’s. The name used in an if or if-else if construct may also be attached following the keyboard then in any else if part of the construct and following the keyboard else in an else part.
For example:
EmpType: if (EmployeeType == ”S”) then ! Salaried employee print *,”Enter employee’s annual salary” read *, Salary pay = salary / 52 else EmpType ! Hourly employee print *,”Enter hours worked and hourly rate:” read *, HoursWorked, HourlyRate Overtime: if (HoursWorked > 40.0) then
19
Named Constructs
pay = 40.0 * HourlyRate & + Multiplier * HourlyRate*(HoursWorked-40.0) else Overtime pay=HoursWorked*HourlyRate end if Overtimeend if EmpType
20
The CASE Construct
The case construct is not as general as the if-else if construct. But itis useful for implementing some selection structures. The form of acase construct is as follows:
select case (selector) case (label-list1)
statement-sequence1
case (label-list2)
statement-sequence2...
case (label-listn)
statement-sequencen
end select
21
The CASE Construct
Where the selector is an integer, character, or logical expression; each of the (label-list1) is a list of one or more possible values of the selector, enclosed in parenthesis. The values in this list may have any of the forms
valuevalue1 : value2
value1 :
: value2
to denote a single value, the range of values from value1 through value2, the set of all values greater than or equal to value1, or the set of all values less than or equal to value2, respectively. When a case construct is executed, the selector is evaluated; if this value is in label-list1 , statement-sequence1 is executed, and execution continues with the statement following the end select statement.
22
Named CASE Construct
If the value is not in any of the lists of values, the sequence of statements associated with default is executed.
Named case constructs
A name may be attached to a case construct:
name: select case (selector)...
end select name
23
Named CASE Construct
For example:
Class: select case (ClassCode) case (1) print *, ”Freshman” case (2) print *, ”Sophomore” case (3) print *, ”Junior” case (4) print *, ”Senior” case (5) print *, ”Graduate” default print *, ”Illegal class code”,Class code end select Class
24
Exercises & Reading (Ellis’s Book)
Study examples 5.1, 5.3, 5.4 and 5.5 on pages 112, 118, 124 and 126 respectively.
Read summary on page 131.
Study Chapter 5: Pages between 103 and 131.
25
The LOGICAL data type
Two logical constants in Fortran are .TRUE. and .FALSE. A logical variable is declared using a logical type statement logical :: listwhere list is a list of variables being typed as logical. An
assignment statement of the form logical-variable = logical-expressioncan be used to assign a value to a logical variableFor example: logical :: RootExists, End_of_Data End_of_Data=.true. RootExists=Discriminant >= 0are valid assignment statements and assigns .TRUE. to
RootExists if Discriminant is nonnegative and assigns .FALSE. otherwise.