Sample 2

advertisement
ITU Faculty of Aeronautics and Astronautics
2014-2015 Spring Semester
21546 BIL106E Introduction to Scientific & Engineering Computing (Fortran)
Sample Problems & Solutions 2
PROBLEM 1: Pencils of cylindrical shape are piled into a
rectangular box as is shown in the figure below. The height of
the box is ℎ and the width of the box is 𝑏 = 14𝑅, where 𝑅 is
the radius of the pencils. Thus, starting at the bottom of the
box seven pencils fit precisely in the box when placed
adjacently in a row. Then six pencils are placed on top of the
first row, then again a row of seven pencils, etc. until the box
is filled. The pencils do not stick out of the box, i.e. the height
of the pile of pencils does not exceed the height of the box.
Write a program that reads ℎ and 𝑅 from the keyboard and
finds the maximum number of pencils that will fit into the
box.
h
R
b =14R
Solution 1:
program m2p1
real :: h, R, hp, dh
integer :: nr, np
print *, "enter h & R:"
read *, h, R
hp = 0
nr = 0
dh = sqrt(3.0) * R
if (.not.(h<2*R)) then
nr = 1
np = 7
hp = 2*R
Do
if(hp+dh<=h) then
hp = hp + dh
nr = nr + 1
np = np + (1-(-1)**nr)/2*7+(1+(-1)**nr)/2*6
else
exit
end if
end do
end if
print *, np
end program m2p1
PROBLEM 2: The grades of the students of a programming course are stored in a file named "grades.dat"
(the file contains solely numbers). The number of students attending the course is 𝑛. Write a program, that
-
reads the grades from this file and assigns these values to an array,
finds the lowest grade,
finds the highest grade,
and calculates the average grade, leaving out the lowest and highest grade.
Solution 2:
program m2p2
integer :: i, grades(10), ming, maxg, summ
real :: ave
open (10, file = "grade.txt")
do i = 1, 10
read(10, '(i2)') grades(i)
end do
maxg = grades(1) ; ming = maxg
do i = 2, 10
if (grades(i).lt.ming) then
ming = grades(i)
end if
if (grades(i).gt.maxg) then
maxg = grades(i)
end if
end do
summ = 0
do i = 1, 10
summ = summ + grades(i)
end do
ave = real(summ - maxg - ming)/10
print *, ming, maxg, ave
end program m2p2
PROBLEM 3: What will be displayed when the following program is run?
program p3
integer
:: x = 123
real
:: y = -4.56
character(len=4 ) :: z = "7890"
print '(i2,tr1,f5.2,2x,a4)', x, y, z
print '(i3,f4.2,a3)', x, y, z
print '(i4,f5.2,tl4,a2)', x, y, z
end program p3
Solution 3:
**_-4.56__7890
123****789
_123-7856
PROBLEM 4: Write a subroutine that changes the elements of a square-matrix into the element of its
transpose. The subroutine must have a single argument.
Solution 4:
subroutine tp(a)
integer, dimension (:,:), intent(inout) :: a
integer :: i, j, n, temp
n = size(a, 1)
do i = 2, n
do j = 1, i-1
temp = a(i, j)
a(i,j)=a(j,i)
a(j,i)=temp
end do
end do
end subroutine tp
PROBLEM 5: Consider the module matlib containing the recursive function 𝑓(𝑛) and the driver program
that tests this function.
module matlib
public :: f
contains
recursive function f(n) result(t)
integer, intent(in) :: n
real :: t
select case(n)
case(2:)
t = sqrt(2-f(n-1))
case(1)
t = sqrt(2.0)
end select
end function f
end module matlib
program p4
use matlib
print *, f(1)
print *, f(2)
print *, f(3)
print *, f(4)
print *, f(5)
end program p4
(a) What will be displayed when the program is run?
(b) Write the recurrence relation, the function in the module is based on. What is the seed value of this
recurrence relation?
(c) To which value, do you think, the function will converge when the argument passed to the function
goes to infinity (𝑛 → ∞)? Does this limit value depend on the seed value?
Solution 5:
(a)
1.414214
7.653669E-01
1.111140
9.427935E-01
1.028206
(b) recurrence relation: 𝑓𝑛 = √2 − 𝑓𝑛−1
Seed value : 𝑓1 = √2
(c) lim 𝑓𝑛 = 1
𝑛→∞
For −2 ≤ 𝑓1 ≤ +2 the limit will be 1. Values beyond the boundaries of this interval are not valid.
Download