1.1 A/B*C = 9 A*B/C = 4 3*A**B = 108 B+A/C = 4 A/B+C = 6 1.2 A/B*C = 0 A*B/C = 3 3*A**B = 729 B+A/C = 5 A/B+C = 4 1.3 program PROGRAM Trivial INTEGER :: A, B, C A = 6 B = 2 C = 3 PRINT *, A/B*C, A*B/C, 3*A**B, B+A/C, A/B+C A = 3 B = 5 C = 4 PRINT *, A/B*C, A*B/C, 3*A**B, B+A/C, A/B+C END PROGRAM Trivial 1.3 output 9 4 108 4 6 0 3 729 5 4 2.1 20.0 1.0 0.0 2.2 20 1 1 3.1 Division by zero. Overflow. Underflow. Programs containing such expressions may crash, may deliver a special value or may otherwise misbehave. In general, underflow will cause the result to be quietly replaced by zero, but that is not required by the Fortran standard. 4.1 a) Why are named constants useful? Answer: They reduce mistyping errors, they makes it clearer which constant is being used and they make it easier to modify the program later. b) What is the difference between REAL, PARAMETER :: pi = 22.0/3.0 and REAL :: pi = 22.0/3.0 Answer: The first defines a named constant pi with value 22.0/3.0, the second declares a variable named pi initialised to 22.0/3.0. c) Is the following program fragment allowed? INTEGER, PARAMETER :: ZERO = 0 ZERO = 1 Answer: No, because ZERO is a constant and cannot be changed. d) Is the following program fragment allowed? INTEGER :: ONE = 1 ONE = 0 Answer: Yes, since ONE is a variable. It can have any value. 6.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 Correct, but quirky. b) CHARACTER :: name Correct. c) CHARACTER(LEN=10) :: name Correct. d) REAL :: var-1 INCORRECT - you can't declare an expression. e) INTEGER :: 1a INCORRECT - names must start with a letter. f) INTEGRAL :: loji INCORRECT - there is no statement 'INTEGRAL'. g) CHARACTER(LEN=5) :: town = "Glasgow" Correct, but town will contain "Glasg". h) CHARACTER(LEN=*), PARAMETER :: city = "Glasgow" Correct. i) INTEGER :: pi = +22/7 Correct, but not useful - the value of pi will be 3. j) CHARACTER(LEN=*), PARAMETER :: "Bognor" INCORRECT - no constant name was given. k) REAL, PARAMETER :: pye = 22.0/7.0 Correct. l) REAL, PARAMETER :: two_pie = pye*2 Correct, provided pye is defined previously as a PARAMETER. m) REAL :: a = 1., b = 2 Correct, but it is not good practice. n) CHARACTER(LEN=6) :: you_know = 'y'know" INCORRECT - that is a mangled character constant. o) CHARACTER(LEN=6) :: you_know = "y'know" Correct. p) INTEGER :: ia ib ic id INCORRECT - you can't separate lists by spaces. q) REAL :: poie = 4.*atan(1.) Either - atan is not allowed in initialisation in Fortran 95, but is in Fortran 2003. Declare the following objects: a) feet an integer variable INTEGER :: feet b) miles a real variable REAL :: miles c) Town a character variable of up to 20 letters CHARACTER(LEN=20) :: Town d) home_town a constant with value set to LIVERPOOL CHARACTER(LEN=*) :: home_town = "LIVERPOOL" e) sin_half a constant with value set to sin(0.5) = 0.47942554 REAL, PARAMETER :: sin_half = 0.47942554 or, in Fortran 2003: REAL, PARAMETER :: sin_half = sin(0.5) 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? False. IMPLICIT NONE forces all variables to be declared in type statements. 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 Answer: ( ( ( (-(a*b)) - (c/(d**e))/f ) + (g**h) ) + 1 ) - (j/k) 3 2 1 x x y y 1 2 3 8.1 program PROGRAM sample IMPLICIT NONE CHARACTER(LEN=40) :: name CHARACTER(LEN=20) :: date PRINT *, 'Type name' READ *, name PRINT *, 'Type date' READ *, date PRINT *, trim(date) // ' is the birthday of ' // name END PROGRAM sample 8.2 program PROGRAM sample IMPLICIT NONE CHARACTER(LEN=40) :: name CHARACTER(LEN=20) :: date PRINT *, 'Type name' READ *, name PRINT *, 'Type date' READ *, date PRINT *, trim(name) // ' was born on ' // date END PROGRAM sample 9.1 program PROGRAM Area IMPLICIT NONE REAL :: radius REAL, PARAMETER :: pi = 3.14159 PRINT *, "Type in the radius" READ *, radius PRINT *, "Area of circle with radius ", & radius, " is ", pi*radius**2 PRINT *, "Volume of sphere with radius ", & radius, " is ", (4.0/3.0)*3.14159*radius**3 END PROGRAM Area 9.1 answers Type in the radius 2 Area of circle with radius 2.0000000 is 12.5663605 Volume of sphere with radius 2.0000000 is 33.5102959 Type in the radius 5 Area of circle with radius 5.0000000 is 78.5397491 Volume of sphere with radius 5.0000000 is 5.2359839E+02 Type in the radius 10 Area of circle with radius 10.0000000 is 3.1415900E+02 Volume of sphere with radius 10.0000000 is 4.1887871E+03