Introduction to Programming with Fortran ---------------------------------------- Practical Exercise 3 -------------------- Question 1 ---------- 1.1 Evaluate the following when A = 6, B = 2 and C = 3. A, B and C are all integer variables. A/B*C A*B/C 3*A**B B+A/C A/B+C 1.2 Evaluate the above expressions when A = 3, B = 5 and C = 4. 1.3 Write a program to check that your Fortran compiler gives the results you have given. Question 2 ---------- 2.1 What value does the real variable A take in the following statements when I = 5, J = 4 and K = 3 (I, J and K are INTEGER)? A = I*J + 1/K A = I/J + 1/K A = J/I + 1/K 2.2 What value does the INTEGER I take in the following statements when A = 5.0, B = 4.0 and C = 3.0 (A, B and C are REAL)? I = A*B + 1/C I = A/B + 1/C I = B/A + 1/C Question 3 ---------- 3.1 On a particular computer the range for reals is 10^-38 to 10^+38. a) What value does the expression (A/B) have when A = 1.0, B = 0.0? b) What value does the expression (5.3E+25 * 6.4E+28) have? c) What value does the expression (5.3E-50 * 6.4E-35) have? Question 4 ---------- 4.1 This question is about named constants. a) Why are named constants useful? b) What is the difference between REAL, PARAMETER :: pi = 22.0/3.0 and REAL :: pi = 22.0/3.0 c) Is the following program fragment allowed? INTEGER, PARAMETER :: ZERO = 0 ZERO = 1 d) Is the following program fragment allowed? INTEGER :: ONE = 1 ONE = 0 Question 5 ---------- 5.1 Which of the following are incorrect declarations and why? If you think a declaration may be correct in a given situation (but not everywhere) then say what the situation would be. a) ReAl :: x b) CHARACTER :: name c) CHARACTER(LEN=10) :: name d) REAL :: var-1 e) INTEGER :: 1a f) INTEGRAL :: loji g) CHARACTER(LEN=5) :: town = "Glasgow" h) CHARACTER(LEN=*), PARAMETER :: city = "Glasgow" i) INTEGER :: pi = +22/7 j) CHARACTER(LEN=*), PARAMETER :: "Bognor" k) REAL, PARAMETER :: pye = 22.0/7.0 l) REAL, PARAMETER :: two_pie = pye*2 m) REAL :: a = 1., b = 2 n) CHARACTER(LEN=6) :: you_know = 'y'know" o) CHARACTER(LEN=6) :: you_know = "y'know" p) INTEGER :: ia ib ic id q) REAL :: poie = 4.*atan(1.) Question 6 ---------- 6.1 Declare the following objects: a) feet an integer variable b) miles a real variable c) Town a character variable of up to 20 letters d) home_town a constant with value set to LIVERPOOL e) sin_half a constant with value set to sin(0.5) = 0.47942554 Question 7 ---------- 7.1 When IMPLICIT NONE is used at the start of a program all variable names beginning with I, J, K, L, M, N are assumed to be INTEGER. Is this true or false? 7.2 Add parentheses to the following expression to indicate the order of evaluation: -a*b-c/d**e/f+g**h+1-j/k Question 8 ---------- 8.1 Write a program that reads in a person's name and date of birth (in the form `dd.mm.yy') and prints out a sentence such as: dd.mm.yy is the birthday of Joe Bloggs 8.2 Modify the program to print the sentence in the form: Joe Bloggs was born on dd.mm.yy Question 9 ---------- 9.1 Write a simple program to read in a radius and calculate the area of the corresponding circle and volume of the corresponding sphere. Demonstrate its correctness by calculating the area and volume using radii of 2, 5 and 10. The area of a circle is pi times the radius squared, and pi may be taken as 3.14159. The volume of a sphere is 4/3 pi times the radius cubed.