(1) State whether each of the following program names is
valid or not. If not, state why the name is invalid.
(a) Junk
(b) 3rd
(c) Who_are_you?
(d) Time_to_intercept
(e) Cubic Equation
Answer:
(a) Valid Integer Variable
(b) Invalid
(c) Program Name Must Begin With a Letter
(d) Valid Character Constant
(2) Which of the following expressions are legal in FORTRAN?
If an expression is legal, evaluate it.
(a) 2.∗∗ 3/3 ∗∗ 2
(b) 2 ∗ 6 + 6 ∗∗ 2/2
(c) 2 ∗ (−10.) ∗∗ −3.
(d) 2/ (−10.) ∗∗ 3.
(e) 23/4/8
Answer:
(a) 2.**3/3**2 Legal Expression
Evaluate exponents first: 8. /9
Evaluate divisions: 0.8889 (Real Result)
(b) 2*6 + 6**2 / 2 Legal expression
Evaluate exponents first: 2*6 + 36 /2
Evaluate multiplications and divisions: 12 + 18
Evaluate addition: 30 (Integer Result)
(c) 2* (-10)**-3 Illegal Expression (two adjacent operators)
(d) 2 / (-10.)**3 Legal Expression
Evaluate exponents first: 2 / (-1000.)
Evaluate divisions: -0.002 (Real Result)
(e) 23 / (4 / 8) Illegal Expression (division by zero)
(3) Evaluate each of the following expressions
(a) 13/5 ∗ 6
(b) 2 + 5 ∗ 2 − 5
(c) (2 + 5) ∗ 2 − 5
(d) 3 ∗∗ 3 ∗∗ 2
(e) 3 ∗∗ (3 ∗∗ 2)
Answer
(a) 13/5*6
Answer 13/5*6=>2.6*6=15.6
(b) 2+5*2-5
Answer 2+5*2-5=>2+10-5=>12-5=7
(c) (2+5)*2-5
Answer (2+5)*2-5=>7*2-5=>14-5=9
(d) 3**3**2
Answer: All exponentiations are performed first;
consecutive exponentiations are performed from right to
left
3**3**2=>3*9=>19683
(e) 3**(3**2)
Answer 3**(3**2) is the same as 3**3**2
=3**3**2=>3**9=>19683
(4) Write the FORTRAN statements required to calculate y
(t) from the equation
For a user-supplied value of t.
Answer
!Prompt the users for the values t
Read t
!Echo the input coefficient
IF(t>=0.) THEN
Fun=(-3)*t**2+5
ELSEIF(t<0)THEN
Fun=3*t**2+1.25
END IF
!Write out y(t)
!Turn the algorithm in to FORTRAN statements
!The final FORTRAN COD is shown below
FUNCTION Lagrange(x, y, n, x)
Sum = 0
DO i = 0, 1, 2
Product f(xi)
DO j = 0, 1, 2
IF i /= j THEN
Product = Product*(x-xj)/(xi-xj)
ENDIF
END DO
sum = sum + product
END DO
Lagrange = sum
END Lagrange
PROGRAM funt
! Purpose:
! This Program solves the function y(t) for a user specified t,
! Where y(t) is defined as:
! Y (t) = |(-3)*t**2 + 5 t >= 0
! | 3*t**2 + 5 t < 0
! Recorded of revisions
Implicit none
!Data dictionary: declare variable types, definitions, & units
REAL :: t ! Independent variable
REAL :: fun ! Resulting function
! Prompt the user for the values t
WRITE (*) 'Enter the coefficient t: '
READ (*) t
! Calculate the function y(t) based upon the signs of t.
IF ( t >= 0. ) THEN
Fun = (-3)*t**2 + 5
ELSE IF ( t < 0. ) THEN
Fun = 3*t**2 + 5
END IF
! Write the value of the function.
WRITE (*) 'The value of the function is: ', fun
END PROGRAM funt
(5) Suppose that you are writing a program that reads in a
numerical grade and assigns a letter
Grade to it according to the following table:
95 < Grade A
86 < Grade ≤ 95 B
76 < Grade ≤ 86 C
66 < Grade ≤ 76 D
0 < Grade ≤ 66 F
Write a FORTRAN program that assigns letter grades as
described above.
Answer
Program grades
Implicit none
Real:: test1, test2, test2
Integer:: testave
! Display initial header
Write (*,*) "Grade Assignment Program"
Write (*,*)
Write (*,*) "Enter test 1, test 2 and test 3 values“
Read (*,*) test1, test2, test3
! Calculate the average and convert to integer
testave = nint ((test1 + test2 + test3)/3.0)
! Determine grade A >= 90, B= 80 – 89, C= 70 – 79, D = 60 –
69, F <= 59
Select case (testave)
Case (90:)
Write (*,*) "Grade is: A"
Case (80:89)
Write (*,*) "Grade is: B"
Case (70:79)
Write (*,*) "Grade is: C"
Case (60:69)
Write (*,*) "Grade is: D"
Case (:59)
Write (*,*) "Grade is: F"
End select
End program grades
(6) Write a FORTRAN program to evaluate the equation y(x) =
x2 – 3x + 2 for all values of x between −1 and 3, in steps of
0.1.
Answer
PROGRAM funx
! Purpose:
! This Program solves the function y(x) for a user specified x,
! Where y(x) is defined as:
! Y(x) = x**2 - 3*x + 2 -1 < x < 3
IMPLICIT NONE
! Data dictionary: declare variable types, definitions, & units
REAL :: x ! Independent variable
REAL :: fun ! Resulting function
! Prompt the user for the values x
WRITE (*) 'Enter the coefficient x:'
READ (*) x
! Calculate the function y(x).
IF ( -1 < x < 3 ) THEN
Fun = x**2 - 3*x + 2
END IF
! Write the value of the function.
WRITE (*) 'The value of the function is: ', fun
END PROGRAM funx