Data Types and Arithmetic Operations

Download Report

Transcript Data Types and Arithmetic Operations

ICS 101 – LAB 2
Arithmetic Operations
I Putu Danu Raharja
raharja@ kfupm.edu.sa
Information & Computer Science Department
CCSE - King Fahd University of Petroleum & Minerals
1
Data Type
 What are data types? What are the differences among
them?


Range of values
Result of operation
 Data types in Fortran 77
 Integer
 Real
 Logical
 Character
 We can use explicit or implicit definition
 Explicit definition is recommended
Integer
 What is it? Examples?
 No decimal point
 32 bit Integer (-2147483648 to +2147483647)
 Implicit definition starts with: I, J, K, L, M, N
 Remember: Explicit definition is recommend-ed.
Real
 Real numbers (Examples?)
 With decimal point or scientific notation



3.1415
-9.8
0.76E-16
 Implicit definition starts with any characters other
than I – N.
 Remember (again): Explicit definition is
recommended.
Character
 To store characters
 Text/String: chain of characters
CHARACTER SPEED*20
CHARACTER*7 FIRST, MIDDLE*3, LAST
CHARACTER TEST
Simple Output Statement
 PRINT statement shows a text, values of
variables or results of operations to the output
device such as screen.
 A text must be enclosed by quotes
 Single or double quotes can be used but be sure
to end the text with the same type of quote.
Exercise-1
 Write a program to display the following text:
In this course, you will be introduced to the basic
concepts of computing and computer
programming. It's an exciting experience.
Simple Input Statement
 To read an input value from terminal into a
variable.
 Each unformatted READ* statement starts
reading from a new line.
 The data values must agree in type with the
variables.
 The data values can be separated by blanks or
comma.
Exercise-2
 Write a program that asks your name, your student ID,
and your age and then displays the inputs as follows:
Your Name:
Muhammad Ali
Your student ID:
223344
How old are you?
18
Muhammad Ali (223344) is 18 years old.
Arithmetic Operations
 See page 13
 Operator & operand
 Binary & unary operators
 Basic operators:





Exponentiation: **
Multiplication: *
Division: /
Addition: +
Substraction: -
Precedence
1. Parentheses
2. Exponentiation
3. Multiplication & Division
4. Addition & Substraction
What is the result of the following expresions:
2+3*4
(2 + 3) * 4
3 * 2 ** 3
(3 * 2) ** 3
4 ** 0.5
1/3
1/3.0
3 / 5 * 10
Associativity
 What are these: Commutative, Associative,
Distributive?
 How is it to evaluate operators with same
precedence?
 Exponentiation: right to left
 Others: left to right
What is the result of the following expressions:
4–2+5
2*3/4
2/3*4
2 ** 3 ** 2
Notes
 We can not write two operators consecutively such
as: 2 * -3
 It must be written as 2 * (-3)
 We cannot raise a negative number to a real
exponent in Fortran 77




Why?
xy = ey ln x is used for real exponent
(-2) ** 0.5 ?
(-1) ** (1.0 / 3) ?
 Multiplication is “cheaper” than exponentiation
Integer, Real, or Mixed Operation?
 Integer operands: integer result

1/3 → 0
 Real operands: real result

1.0/3.0 → 0.33333...
 Integer & real operands: real results

1.0/3 → 0.333333...
Notes
 Remember (again): readability
 Parentheses are very useful


Watch for incomplete ones
Is it complete?: 1 * (a / (b – (c * 5)))
 Keep each expression readable
 However, in Major-I usually you will be asked to
used least number of parentheses
Assignment
 Assignment: assigning a value to a variable
 Assignment ≠ Mathematical equation

X = 5 means we assign the value of 5 into X; it
doesn't say that X equals to 5
Exercises-3


Write a program to take as input the radius of a circle;
compute and display its area and circumference.
Write a program that computes the speed of sound A
(ft/s) in air of a given temperature T (°F). Use the
formula
A  1086

5T  2297
2457
Write a program to take as input the length of each
side of a triangle; compute and display its area.