Sample Problems 1 Problem 1: Find the value of each of the

advertisement
Sample Problems 1
Problem 1: Find the value of each of the following expressions.
(a) 9 – 5 – 3
(b) 2 + 3 / 5
(c) 2 + 3 ** 2
(d) 25.0 ** 1 / 2
(e) (2 + 3 ** 2) / (8 – 2 + 1)
(f) sqrt(6.0+3.0)
(g) (2.0 + 3**2) / (8 – 2 + 1)
(h) –3.0 ** 2
(i) abs(1 – 2 – 3)
(j) int(5.0 + 4.0 / 3.0)
(k) "abc" // "deed"(:2)
(l) exp(1.5 – 1)
Solution 1:
(a) 1
(b) 2
(c) 11
(d) 5.0
(e) 1
(f) 3.0
(g) 1.571428
(h) –9.0
(i) 4
(j) 6
(k) abcde
(l) 1.648721
Problem 2: Write the programming expressions for the following assignments:
g
cd f 2
(a) a  b 

 i  j  k (formula representation)
e
h
a2
 4  j (formula representation)
(b) a 
be
a.c  d
(c) a 
(formula representation)
4.e
g2
(d) a 
f
2
(formula representation)
gf
ad
(e) a 
(formula representation)
ad
2
Solution 2:
(a) a = b + c * d / e – f ** (g / 2) / h + i *j + k
(b) a = a ** 2 / (b * e) + 4 * j
(c) a = (a * c – d) / (4 * e)
(d) a = f ** (g / 2) / g ** f
(e) a = (a + d) /(a – d)
Problem 3: Write a program a program to read values for the three sides a, b, and c of a triangle
and then calculate its perimeter and its area. These should be displayed together with the values of
a, b, and c using appropriate labels. (For the area, you might use Hero’s formula for the area of the
triangle:
area  s(s  a)( s  b)( s  c)
where s is one-half perimeter.)
Solution 3:
program midterm_prob3
real :: a, b, c, s
real :: area
print *, "enter the side lengths of a triangle:(a, b, c)"
read *, a, b, c
s = (a + b + c) / 2.0
area = sqrt(s * (s-a) * (s-b) * (s-c))
print *, "The area of the triangle having side lengths"
print *, "a = ", a, "b = ", b, "c = ", c
print *, "is"
print *, "Area = ", area
end program midterm_prob3
Problem 4 Write a program that reads values for the coefficients A, B, C, D, E, and F of the
equations
Ax  By  C
Dx  Ey  F
of two straight lines. Then determine whether the lines are parallel (their slopes are equal) or the
lines intersect. If they intersect, determine whether the lines are perpendicular (the product of their
slopes is equal to –1).
program midterm_prob4
real :: A, B, C, D, E, F
real :: m1, m2, eps
eps = 1.0e-3
print *, " Ax + By = C"
print *, " Dx + Ey = F"
print *, "enter the coefficient of the above given equations:"
print *, "A = "
read *, A
print *, "B = "
read *, B
print *, "C = "
read *, C
print *, "D = "
read *, D
print *, "E = "
read *, E
print *, "F = "
read *, F
m1 = - A / B
m2 = - D / E
if (abs(m1-m2)<eps) then
print *, "the two lines are parallel"
else
if (abs(m1*m2+1.0)<eps) then
print *, "the two lines are perpendicular"
else
print *, "the two lines are nor parallel neither perpendicular"
end if
end if
end program midterm_prob4
Problem 5: Write a function which, when supplied with the coordinates of two points (x1, y1) and
(x2, y2) calculates the distance between the points. The distance between the two points is given by
the formula
d
x2  x1 2   y2  y1 2 .
Solution 5:
function distance(x1, y1, x2, y2) result(dist)
real, intent(in) :: x1, y1, x2, y2
real :: dist
dist = sqrt((x2-x1) ** 2 + (y2-y1) ** 2)
end function distance
Problem 6: Write a subroutine that swaps the values of the elements of the two arrays arr1 and
arr2 both having the same number of elements.
Solution 6:
subroutine swap(arr1, arr2)
real, dimension(:), intent(inout) :: arr1, arr2
real, dimension(size(arr1)) :: temp
temp = arr1
arr1 = arr2
arr2 = temp
end subroutine swap
Problem 7:(10p) Write a program which will read up to 20 integers and print them out in the
reverse order to that in which they were typed.
Solution 7:
program reverse_order
use algebra
integer, dimension(20) :: a
integer :: n, i
print *, "enter the number of integer you want to type in:"
read *, n
do i=1, n
print *, "enter integer no.", i, ":"
read *, a(i)
end do
print *, a(n:1:-1)
end program reverse_order
Problem 8: The sequence Fn of Fibonacci numbers is defined by the recurrence relation
Fn  Fn1  Fn2
with seed values F0  1 and F1  1. Write a recursive function that calculates the value of the n.th
element of the Fibonacci series.
Solution 8:
recursive function fibonacci(n) result(s)
integer, intent(in) :: n
integer :: s
select case(n)
case(0:1)
s=1
case(2:)
s = fibonacci(n-1)+fibonacci(n-2)
case default
s=0
end select
end function fibonacci
Download