Download Sample Problems 2 Problem 1: What will be displayed by the

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Sample Problems 2
Problem 1: What will be displayed by the following F-program? You must show all the
leading blanks in your answer. If a value cannot be displayed with a specific format then
asterisks in the right amount are displayed.
program problem_1
real :: x, y, z
character(len = 12) :: s
x = 3.14519
y = -275.3027
z = 12.9999
s = " spaces " !(three leading blanks and three trailing blanks)
print "(f10.3, f10.3, f10.3, a12)", x, y, z, s
print "(f8.2, f10.2, f5.1, a5)", x, y, z, s
print "(f3.0, f5.3, f5.3, a5)", x, y, z, s(3:7)
end program problem_1
Solution 1:
_ _ _ _ _ 3.145_ _ -275.303_ _ _ _ 13.000_ _ _spaces_ _ _
_ _ _ _3.15_ _ _-275.30_ 13.0_ _ _sp
_ 3.********** _spac
Problem 2: An array arr has ten three-digit integer elements. Purely by changing the
output format, print the numbers as
(a) a single line of numbers.
(b) five rows of two numbers;
(c) three rows of three numbers and one line of one number;
(d) a single line leaving two blanks between the numbers;
(e) a single column leaving two blank lines between the numbers.
Your answers should be single print statements. Don’t use other statements including doloops.
Solution 2:
program problem_2
integer, dimension(10) :: arr
integer :: i
arr = (/(100+i, i = 1, 10)/)
print "(10(i3, tr1))", arr
print "(i3, tr1, i3)", arr
print "(3(i3, tr1))", arr
print "(i3, tr2, i3)", arr
print "(i3//)", arr
end program problem_2
Problem 3: Write a function that translates the weekday names in Turkish into English.
Thus, if “pazartesi” is passed to the function the result should be “monday”, if “sali” is
passed to the function it should return “tuesday”, etc. When a wrong value, i.e. other than
the weekday names, is passed to the function the return value should be “error”.
Solution 3:
function wd(tday) result(eday)
character(len=*), intent(in) :: tday
character(len=10) :: eday
if (tday == "pazartesi") then
eday = "monday"
else if (tday == "sali") then
eday = "tuesday"
else if (tday == "carsamba") then
eday = "wednesday"
else if (tday == "persembe") then
eday = "thursday"
else if (tday == "cuma") then
eday = "friday"
else if (tday == "cumartesi") then
eday = "saturday"
else if (tday == "pazar") then
eday = "sunday"
else
eday = "error"
end if
end function wd
Problem 4: Write a recursive function that calculates n!. Write also a main program that
uses this function in calculating the permutation P(5, 2) = 5!/(5-2)!.
Solution 4:
module prob4
public :: fact
contains
recursive function fact(n) result(s)
integer, intent(in) :: n
integer :: s
select case(n)
case(0:1)
s=1
case(2:)
s = n * fact(n-1)
case default
s=0
end select
end function fact
end module prob4
program problem_4
use prob4
print *, "P(5, 2) =", fact(5)/fact(3)
end program problem_4
Problem 5: Personal data of the employees of company X are stored in a file named
“phonenum.txt” as is given below:
Name
Surname
------- -----------John
Doe
Ashley Long
Mike
Young
10 characters
…………
…………
…………
Phone number
----------------2343434
9871233
3178256
12 characters
The file contains the data of 100 employees. Write a program that reads the data from this
file and then stores the personal data in reversed order in a new file called “reversed.txt”.
Solution 5:
program files
character(len = 12), dimension(100) :: name, sname, tnum
character(len = 12) :: temp
integer:: i, j, n
open (unit = 10, file = "phonenum.txt", action = "read", status = "old",
position="rewind")
n = 100
do i = 1, n
read (unit = 10, fmt="(a10,a12,a7)") name(i), sname(i), tnum(i)
end do
open (unit = 11, file = "reversed.txt", action = "readwrite", status = "replace",
position="rewind")
do i = 1, n
write(unit = 11, fmt="(a10,a12,a7)") name(101-i), sname(101-i), tnum(101-i)
end do
end program files
Problem 6:Write a function that multiplies the two double arrays A(m, n) and B(n, k) and
assigns the result to the output array C.
Solution 6:
module mproblem6
public :: multpl
contains
subroutine multpl(a, b, c)
real, dimension(:, :), intent(in) :: a, b
real, dimension(:,:), intent(out) :: c
integer :: i, j, l, k, m, n
real :: summ
m = size(a,1)
n = size(a,2)
k = size(b,2)
do i = 1, m
do j = 1, k
summ = 0
do l = 1, n
summ = summ + a(i, l) * b(l, j)
end do
c(i, j) = summ
end do
end do
end subroutine multpl
end module mproblem6
Problem 7: The definite integral of a function f (x) can be evaluated using the rectangle
method as is given below:
b
n
a
i 1
 f ( x)dx   f (a  (i  1) x) x .
where Δx = (b-a)/n. This computation is the spirit of the definition of the Riemann
integral and the limit of this approximation as n   is defined and equal to the integral
of f on (a, b) if this Riemann integral is defined. In this perspective,
(a) write a module that contains the two functions:
f1 ( x)  x 2  ln( x  1)
f 2 ( x)  x cos( x)  e x
(b) write another module that contains a subroutine having the following arguments:
input arguments: f(x) (function as argument)
a
(lower bound of the integration interval (a, b) )
b
(upper bound of the integration interval (a, b) )
n
(number of steps that the integration interval is divided into)
output argument: ires (numerical result of the summation)
(c) write a main program that finds the definite integral of the functions f1 and f 2 in the
interval (1, 2).
Solution 7:
(a)
module prob7a
public :: f1, f2
contains
function f1(x) result(res)
real, intent(in) :: x
real :: res
res = x ** 2 - log (x+1.0)
end function f1
function f2(x) result(res)
real, intent(in) :: x
real :: res
res = x * cos (x) + exp (x)
end function f2
end module prob7a
(b) module prob7b
public :: integ
contains
subroutine integ(f, a, b, n, ires)
interface
function f(x) result(res)
real, intent(in) :: x
real :: res
end function f
end interface
real, intent(in) :: a, b
integer, intent(in) :: n
real, intent(out) :: ires
real :: summ, dx
integer :: i
dx = (b - a) / n
summ = 0
do i = 1, n
summ = summ + f(a + (i - 1) * dx) * dx
end do
ires = summ
end subroutine integ
end module prob7b
(c)
program problem_7
use prob7a
use prob7b
real :: a, b
integer :: n
real :: idef
a = 1.0
b = 2.0
n = 100
call integ(f1, a, b, n, idef)
print *, "definite integral of function f1(x) = x^2 - ln(x+1) on interval (1,2):", idef
call integ(f2, a, b, n, idef)
print *, "definite integral of function f1(x) = x cos(x) + exp(x) on interval (1,2):",
idef
end program problem_7
Related documents