300 likes | 413 Views
WEEK 3. DATA HANDLING. There are two fundamental types of numbers in both mathematics and programming , which may have either a fractional part or not. There are, in F, known as integers and real numbers. There is vital importance between them. Fundamental types of numbers.
E N D
WEEK 3 DATA HANDLING
There are two fundamental types of numbers in both mathematics and programming, which may have either a fractional part or not. • There are, in F, known as integers and real numbers. • There is vital importance between them
Fundamental types of numbers • A whole number and is stored in the computer’s memory without any decimal part. • positive/negative/zero • It’s limits vary from one computer to another.
Integers • Examples: 1965 1234 98765432 0 -1 -567 • Typical range on a 32-bit computer -2 x 109 to +2 x 109
Reals +/-xxx.yyyyy xxxinteger part yyyyy fractional part 0.12 -15.16 • A better representation: • Sign:+/- • Mantissa:a fraction between 0.1and 1.0 • Exponent:x 10e • Typical range on a 32-bit computer -1038 to 1038
Variables • It is usual for people to associate a name or phrase with a piece of information. • For example, the phrase "today" has an associated numeric value which varies day by day. • This is similar to the concept of a program variable; a program variable is some object (named by the programmer) which uniquely identifies a piece of data stored in memory.
real and integer variables • Variable declaration: type :: name Type specifies the data type for which memory space is to be reserved Name is chosen by programmer with which to refer to the variable that has been declared.
It is possible to be more than one variable of the same type being declared type :: name1, name2, name3, … • integer :: number1, number2, & number3, .. In a similar way, real variables are declared as, • real :: div1, div2, …
Arithmetic expressions • The arguments of these functions are numbers, arithmetic functions or arithmetic expressions. • An arithmetic expression is one which is evaluated by performing a sequence of arithmetic operations to obtain a numeric value, which replaces the expression.
Assignment • name = expression • replace the content of the variable name with the result of the expression
An operand of an arithmetic expression may be: a numeric constant a numeric variable, which may be preceded by a unary + or -. an arithmetic expression in parentheses, i.e. (arithmetic_expression) N + 1 2.2 * K + 33.9 time // 1000 3.14159 K (A+B)*(C+D) -1.0/X + Y/Z**2 2.0 * 3.14159*RADIUS
OperatorOperation + Addition or unary + -Subtraction or unary – *Multiplication /Division ** Exponentiation
Aritmetik operator priorities First ** Exponentiation Second * or /Multiplication/Division Third + or -Addition/Subtraction Evaluation will proceed ** Exponentiation From right to left * or /Multiplication/Division From left to rigt + or -Addition/SubtractionFrom left to rigt
Aritmetik operator priorities 1 6 5 2 X=Y+Z-K/L+M*K**N/J-G*H 4 3 7 9 8
Aritmetik operator priorities a=b+c*d/e-f**g/h+i*j+k • Calculate f**g and save it in temp_1 • Calculate c*d and save it in temp_2 • Calculate temp_2/e and save it in temp_3 • Calculate temp_1/h and save it in temp_4 • Calculate i*j and save it in temp_5 • Calculate b+temp_3 and save it in temp_6 • Calculate temp_6-temp_4 and save it in temp_7 • Calculate temp_7+temp_5 and save it in temp_8 • Calculate temp_8+k and store it in a.
List-directed input and output • read *, var_1, var_2, … • only variables! • print *, item_1, item_2, … • variables, constants, expressions, … • Value separators: • Comma (,) 8,6,5 • Space 8 6 5 • Slash (/) 8/65 • End-of-line 8 6 5
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
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 9s= + - * / ( ) , . ' : ! " % & ; < > ? $ (S represents the space or blank, character)
Character data • 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!"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 constantsThe constants, which are no need to be altered, are called named constand. They are used by the parameter attribute in a declaration statement. • type, parameter :: name1=constant_expression1, … • real, parameter :: pi=3.1415926, pi_by_2 = pi/2.0 integer, parameter :: max_lines = 200
! Name : ! Tel : ! Address : ! Purpose : ! Date : ! 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
! Name : ! Address : !! Date : ! 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
!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
program exercise_1 ! A program to convert a Celsius temperature to Fahrenheit ! Variable declarations real :: temp_c, temp_f ! Ask for Celsius temperature print *, "What is the Celsius temperature ?" read *, temp_c ! Convert it to Fahrenheit temp_f=9*temp_c/5 + 32 ! print both temperatures print *,"C= ",temp_c," F=",temp_F end program exercise_1
program exercise_2 ! Variable declarations real :: a,b,p,q,r integer :: x,y,z a = 2.5 b = 4.0 p = a + b x = a + b q = a * b y = a * b r = p / q z = x / y print *, p,q,r print *, x,y,z end program exercise_2
program exercise_3 ! Variable declarations real :: x, y1, y2, y3 character(len=3):: ch_1,ch_2 character(len=8) ::ch_3 ! Ask for x value print *, "Enter the value of x =>" read *, x ! Calculate the polinomials with the character assignments ch_1="x-1" y1 = x - 1.0 ch_2="x+1" y2 = x + 1.0 ch_3="x*x+x-2" y3 = x**2 + x - 2.0 ! print results print *,ch_1,y1 print *,ch_2,y2 print *,ch_3,y3 end program exercise_3
program exercise_4 real, parameter :: apple=675, butter=75, sugar=150 real, parameter :: breadcrumbs=100, cream=150 integer, parameter :: m=4 ! the recipe prepared for 4 people character(len=3) , parameter :: blank=" " character(len=11), parameter :: apple_p="g of apples" character(len=11), parameter :: butter_p="g of butter" character(len=10), parameter :: sugar_p="g of sugar" character(len=17), parameter :: breadcrumbs_p="g of breadcrumbs" character(len=11), parameter :: cream_p="ml of cream" integer :: n real :: apple_1, butter_1, sugar_1 real :: breadcrumbs_1, cream_1 ! Ask for number of people print *, "Enter the number of people ""n"" in integer" read *, n ! Calculate the recipe ! first divide all ingredients by recipe scale which is 4 (4 people) ! then multiply them by numöber of people apple_1 = n * (apple/m) butter_1 = n * (butter/m) sugar_1 = n * (sugar/m) breadcrumbs_1 = n * (breadcrumbs/m) cream_1 = n * (cream/m) ! print results print *,"Danish cake for",n," people" print *,apple_1 , blank, apple_p print *,butter_1 , blank, butter_p print *,sugar_1 , blank, sugar_p print *,breadcrumbs_1 , blank, breadcrumbs_p print *,cream_1 , blank, cream_p end program exercise_4
program exercise_5 ! parameter declaration real, parameter :: l=1.6e-19, pi=3.1415927, eps=8.86e-12 real, parameter :: four_pi=4.0*pi ! variable declaration integer :: z real :: phi_r, r , temp_1, temp_2 ! Ask for the distance r print *, "Enter the distance ""r"" in meters" read *, r ! Ask for the charge z print *, "Enter the charge ""z"" " read *, z ! first calculate the nominator and store it in temporary variable named temp_1 ! second calculate the dnominator and store it in temporary variable named temp_2 ! Calculate the coulomb potential phi_r temp_1 = z * l temp_2 = four_pi * eps * r phi_r = temp_1 / temp_2 ! print results print *,"the Coulomb potential at the distance r=",r, c " meters with a charge of",z," is equal to", phi_r end program exercise_5