Midterm Exam Solution

advertisement
Faculty of Aeronautics and Astronautics
2014-2015 Spring Term
14.04.2015
Midterm Exam - Solution
Problem 1: (10 p) What will be the result of the expressions below?
(a)
(b)
(c)
(d)
(e)
1–2+3
1 – 2 + 3.0
4/5*6
4*6/5
4 / 5.0 * 6
(f)
(g)
(h)
(i)
(j)
2 ** 3 ** 4
(2 ** 3) ** 4
2 ** (3 ** 4)
log10(10.0)
log(10)
(f)
(g)
(h)
(i)
(j)
281=2417851639229258349412352
4096
281=2417851639229258349412352
1.0
2.303
Solution 1:
(a)
(b)
(c)
(d)
(e)
2
2.0
0
4
4.8
Problem 2: (10 p) What will be the output of the following program? (use the underscore _ character for
blanks)
program problem2
integer :: i, total = 0
character (2) :: s1
character (1) :: s2, s3
character (4) :: s
do i = 1, 9
s1 = "(i"
s2 = char(i+48)
s3 = ")"
s = s1//s2//s3
total = total + i
write(*, s) total
end do
end program problem2
ASCII Code
46
47
48
49
50
51
52
53
54
55
56
57
Problem 3: (10 p) What will be the output of
the following program? (use the underscore
_ character for blanks)
program problem3
character(len = 3) :: t = "abc"
integer :: a = 123
real :: x = 3.1e-1
print "(f8.1, tl4, i2, tr2, a1)", x, a, t
end program problem3
Character
.
/
0
1
2
3
4
5
6
7
8
9
Solution 3:
**.3a
Solution 2:
1
3
6
10
15
21
28
36
45
Problem 4: (10 p) Write a program that reads the date in the form dd-mm-2015 and returns which day this
date is if the month entered is April (4th month). If the month entered is not April then the program must
ask for a new date. (Today’s date is 14.04.2015, Tuesday.)
Solution 4:
program problem4
integer :: mday, day, d1, d2
character (10) :: date
print *, "Enter a date that is in April in the form ddmm-2015:"
read *, date
do
if (date(4:5)=="04") then
d1 = ichar(date(1:1)) - 48
d2 = ichar(date(2:2)) - 48
day = 10 * d1 + d2
mday = modulo(day-14, 7)
select case(mday)
case(0)
print *, "Today is Tuesday"
case(1,-6)
print *, "Today is Wednesday"
case(2,-5)
print *, "Today is Thursday"
!(continued)
case(3,-4)
print *, "Today is Friday"
case(4,-3)
print *, "Today is Saturday"
case(5,-2)
print *, "Today is Sunday"
case(6,-1)
print *, "Today is Monday"
end select
exit
else
print *, "Enter new date:"
read *, date
end if
end do
end program problem4
Problem 5: (10 p) Write a function that receives a complex number (𝑧 = 𝑎 + 𝑏𝑖) as an argument and
returns the complex conjugate of this complex number (𝑧̅ = 𝑎 − 𝑏𝑖).
Solution 5:
function conjugate(z) result(zc)
complex, intent(in) :: z
complex :: zc
zc = cmplx(real(z), -imag(z))
end function conjugate
Alternative solution:
function conjugate(z) result(zc)
complex, intent(in) :: z
complex :: zc
zc = abs(z)**2 / z
end function conjugate
Problem 6: (10 p) Write a subroutine that calculates the sum of the elements of a matrix 𝐴𝑚×𝑛 . Do not use
intrinsic functions.
Solution 6:
subroutine matsum(a, s)
integer, dimension(:,:), intent(in) :: a
integer, intent(out) :: s
integer :: i, j
s=0
!(continued)
do i = 1, size(a, 1)
do j = 1, size(a,2)
s = s + a(i, j)
end do
end do
end subroutine matsum
Problem 7: (10 p) Write a function that finds the length of a character expression which is passed to the
function. The intrinsic functions given below might be helpful.
LEN (string) : Returns the length of a string.
ADJUSTL (string): Adjusts a character string to the left by removing all leading blanks and inserting an
equal number of trailing blanks.
TRIM (string): Removes trailing blanks from a character string.
Solution 7:
function stringlength(c) result(d)
character (len=*), intent(in) :: c
integer :: d
d = len(trim(adjustl(c)))
end function stringlength
Problem 8: (30 p) Cube root: Write a function to compute the cube root of a real argument by Newton’s
method. Starting with Q=1.0, evaluate the expression
(2.0 * Q + R / (Q * Q) ) / 3.0
where R is a given real argument. If this expression has almost the same value as Q, return this value as
the cube root of R. Otherwise, assign this expression value as the next value of Q, and repeat the
calculation. (Problem: What is “almost the same value?”)
Solution 8:
Q = 1.0
do
x =(2.0*Q+R/(Q*Q))/3.0
Q=x
if
F
|x-Q|<epsilon
T
cube_root = q
function cube(x) result(c)
real, intent(in) :: x
real :: c
real :: q = 1.0, eps = 0.0000001, dif, x1
x1 = x
do
dif = abs((2.0 * q + x1 / (q*q)) /3.0 - q)
if (dif<eps) then
c=q
exit
else
q = (2.0 * q + x1 / (q*q)) /3.0
end if
end do
end function cube
Download