CS1001 Lecture 8 - PowerPoint PPT Presentation

About This Presentation
Title:

CS1001 Lecture 8

Description:

is used to form the complement (or opposite) of a condition. E.g.: Even ... Used to form Complements of a logical expression. Theorem: Complement of Expr1 .AND. ... – PowerPoint PPT presentation

Number of Views:28
Avg rating:3.0/5.0
Slides: 15
Provided by: plum1
Learn more at: http://web.cs.wpi.edu
Category:

less

Transcript and Presenter's Notes

Title: CS1001 Lecture 8


1
CS1001 Lecture 8
  • Working with LOGICAL Data Type
  • Examples
  • QA Project

2
LOGICAL Variables
  • LOGICAL type statement
  • LOGICAL list
  • e.g., LOGICAL RootExists, Error
  • Assignment statement
  • variable logical-expression or logical-constant
    (.TRUE., .FALSE.
  • e.g., Error .TRUE.
  • Error iTemp lt -100 .OR. iTemp gt 1000

3
LOGICAL Variables
  • I/O
  • Print , error, RootExists, .TRUE., .FALSE.
  • Suppose error is .TRUE. And RootExists is
    .FALSE., then out put is T F T F
  • LOGICAL A,B,C
  • Read , A,B,C
  • Input could be .T., .FALSE., T
  • or T, .F,

4
  • From yesterday
  • IF (cScale / F .AND. cScale / C) THEN
  • bError .TRUE.
  • ELSE IF (iTemp lt -100 .OR. iTemp gt 1000) THEN
  • bError .TRUE.
  • ELSE
  • bError .FALSE.
  • ENDIF
  • END IF

5
Operations with LOGICALs
  • LOGICAL OutRange, CorrectLetter
  • CorrectLetter (cScale F) .OR. (cScale
    C)
  • OutRange iTemp lt -100 .OR. iTemp gt 1000
  • Integer N
  • LOGICAL Even, Odd
  • Even (N-N/2N) .EQ. 0
  • Odd (N-N/2N) / 0

6
Problem 1 (Chap 3, 14)
Program Sides_of_Triangle ! Check if 3 lengths
can be the sides of a triangle, of an !
equilateral triangle, of an isosceles triangle,
of a scalene ! triangle. IMPLICIT NONE REAL
side1, side2,side3 LOGICAL Triangle,
Equilateral, Isosceles, Scalene ! Read three
sides and make determination PRINT , Enter 3
lengths READ , side1, side2, side3
7
Problem 1 (Contd)
Triangle (side1side2 gt side3) .AND.
(side1side3 gtside2) (side2side3 gt
side1) Equilateral Triangle .AND. (side1
side2) .AND. (side3 side2) Isosceles
Triangle .AND. ((side1 side2) .OR (side2
side3) . OR. (side1 side 3)) Scalene
Triangle .AND. .NOT. Isosceles Print , Triangle
is , Triangle Print , Equilateral is,
Equilateral Print , Isosceles is,
Isosceles Print , Scalene is , Scalene END
PROGRAM Sides_of_Triangle
8
.NOT. Operator
  • The logical operator .NOT. is used to form the
    complement (or opposite) of a condition.
  • E.g. Even (N-N/2N) .EQ. 0 Odd .NOT.
    Even
  • E.g. (Age .GT. 25) .AND. (Status .EQ. single)
  • .NOT. ( (Age .GT. 25) .AND. (Status .EQ.
    single))

9
DeMorgans Theorem
  • Used to form Complements of a logical expression
  • Theorem
  • Complement of Expr1 .AND. Expr2 is Comp1 .OR.
    Comp2
  • Complement of Expr1 .OR. Expr2 is Comp1 .AND.
    Comp2
  • where Comp1 is Complement of Expr1 (.NOT.
    Expr1) and
  • Comp2 is Complement of Expr2 (.NOT.
    Expr2)
  • E.g. .NOT. ( (Age .GT. 25) .AND. (Status .EQ.
    single)) may be written as using DeMorgans
    Law
  • .NOT. (Age .GT.25) .OR. .NOT. (Status .EQ.
    single)
  • Then simplifying to (Age .LE. 25) .OR. (Status
    .NE. single)
  • E.g. .NOT. ((A gt 5) .OR. (C lt (AB))) may be
    written as
  • using DeMorgans Law .NOT. (Agt5) .AND. .NOT.
    Clt(AB)
  • simplifying to (Alt5) .AND. (C gt(AB))

10
Program Pointers 1
  • With Multialternative selection IF-ELSE IF can
    be more efficient than a sequence of IFs.

IF (Score gt 90) Grade A IF ((Score gt 80)
.AND. (Score lt 90) Grade B IF ((Score gt 70)
.AND. (Score lt 80) Grade C IF ((Score gt 60)
.AND. (Score lt 70) Grade D IF ((Score lt 60)
Grade F IF (Score gt 90) THEN Grade A
ELSE IF (Score gt 80) Grade B ELSE IF
(Score gt 70) Grade C ELSE IF (Score gt 60)
Grade D ELSE Grade F
11
Program Pointer 2
  • Because most real values are not stored exactly,
    real qualities that are algebraically equal may
    yield a false logical construct when compare with
    .
  • E.g. algebraically x(1.0/x) 1.0
  • but, x(1.0/x) 1.0 is usually .FALSE.
  • See example Figure 3.3 Pages 135-136.
  • Use IF(ABS(real_values1 - real_values2) lt
    Tolerance) THEN .
  • Tolerance is some small positive real value e.g.,
    1E-10

12
Example Program
  • Page 183 6
  • Gas Company charges gas used bas on the following
    table
  • Gas Used Rate
  • First 70 Cubic meters 5 minimum
  • Next 100 Cubic meters 0.05 per cubic meter
  • Next 230 cubic meters 0.025 per cubic meter
  • Above 400 cubic meter 0.015 per cubic meter

13
If gas used is 60 cubic meters, then the charge
is 5.00. If gas used is greater than 70 cubic
meters for example, 130 cubic meters, then the
charge (130-70)(rate2) Minimum rate
60 0.05 5.00 8.00. If the gas used
is 250 cubic meters, then the charge
(250-170) (rate3) (170-70)(rate2) minimum
cost If the gas used is 500 cubic meters, then
the charge (500-400)(rate4)
(400-170)(rate3)
(170-70)(rate2)minimum cost
14
Program Design
Ask the user for two 4-digit numbers, i.e.,
meter reading for the previous month and the
current meter reading. Calculate the gas used.
Handle the special case Calculate charges (how
to do this?) Check and do for the higher usage
first. Print out charges
Write a Comment
User Comments (0)
About PowerShow.com