160 likes | 177 Views
Fundamental data types List-directed input/output Week 3. Fundamental types of numbers. Integers Whole numbers (positive/negative/zero) Examples: 1952 3456787890123 0 -2334567 Typical range on a 32-bit computer -2 x 10 9 to +2 x 10 9. Fundamental types of numbers. Reals
E N D
Fundamental data types List-directed input/output Week 3
Fundamental types of numbers • Integers • Whole numbers (positive/negative/zero) • Examples: 1952 3456787890123 0 -2334567 • Typical range on a 32-bit computer -2 x 109 to +2 x 109
Fundamental types of numbers • Reals +/-xxx.yyyyy xxxinteger part yyyyy fractional part • A better representation: • Sign: +/- • Mantissa: a fraction between 0.1 and 1.0 • Exponent: x 10e -0.923456 x 10-6 or -0.923456e-6
real and integer variables • Variable declaration: type :: name type :: name1, name2, … • integer :: a, b, c • real :: x, y, z
Arithmetic expressions • A combination of variables, constants, operators, parentheses… (4*Pi*Radius* *2 + 1.2098)/2.3
Assignment name= expression replace the content of the variablename with the result of the expression
List-directed input and output • read *, var_1, var_2, … • only variables! • print *, item_1, item_2, … • variables, constants, expressions, … • Value separators: • Comma (,) • Space • Slash (/) • End-of-line
List-directed input and output • Two consecutive commas: • a null value is read • the value of the variable is not set to zero, simply its value is not changed! • If the terminating character is a slash then no more dataitems are read; processing of the input statement is ended • Example
Character data • A B C D E F G H I J K L M N O P Q R S T U W X Y Za b c d e f g h i j k l m n o p q r s t u w x y z0 1 2 3 4 5 6 7 8 9lj = + - * / ( ) , . ' : ! " % & ; < > ? $ • Declaration: character (len=length) :: name1, name2, … character (len=6) :: a, b, c character (len=10*3):: a character (len=10) :: b • Assignmenta = "What a lovely afternoon!" a will have the value of "What a lovely afternoon!ljljljljljlj"b = ab will have the value of "What a lov"
Character data • Concatenation: • Operator // • Example: character (len=10) a, b, c a = "James" b = "Bond" c = trim(a)//""//trim(b) c will have the value of "James Bond"
Named constants • type, parameter :: name1=constant_expression1, … real, parameter :: pi=3.1415926, pi_by_2 = pi/2.0 integer, parameter :: max_lines = 200
Example ! Name : Dursun Zafer Seker ! Tel : +90 (212) 285 3755 (office) ! Address : ITU, Faculty of Civil Engg. 80626 Maslak, Istanbul ! Purpose : Converts Celsius to Fahrenheit ! Date : March 14, 2001 ! Comments:..... ! program Cel_Fah real :: CEL, FAH print *, "Please Enter Celsius Temperature" read *, CEL FAH = 9.0*CEL/5.0+32.0 print*,"Celsius = ",CEL," Fahrenheit = ", FAH end program Cel_Fah
Example ! Name : Dursun Zafer Seker ! Address : ITU, Faculty of Civil Engg. 80626 Maslak, Istanbul !! Date : March 14, 2001 ! Comments:..... ! program Sin_Cos_Tan real :: angle,S,C,T,RAD real, parameter :: PI = 3.1415926 print *, "Please Enter Value of Angle in degrees" read *, angle RAD = angle/(180.0/PI) S = sin(RAD) C = cos(RAD) T = tan(RAD) print*,"angle = ",angle," Sinx = ",S," Cosx = ",C," Tanx = ",T end program Sin_Cos_Tan
Example program list_directed_input_example!integersinteger::int_1, int_2, int_3real::real_1, real_2, real_3!initial valuesint_1=-1int_2=-2int_3=-3real_1=-1.0real_2=-2.0real_3=-3.0!read dataread*, int_1, real_1, int_2, real_2,int_3, real_3!print new valuesprint*, int_1, real_1, int_2, real_2,int_3, real_3end program list_directed_input_example
Example !this program is calculates area of a rectangle program area_calculation use rec real::a,b,al print *, "enter two edges of the rectangle" read *, a,b al=area (a,b) print *, "a=",a print*,"b=",b print *, "area_of_rectangle=",al endprogram area_calculation