Sample 1

advertisement
ITU Faculty of Aeronautics and Astronautics
2014-2015 Spring Semester
21546 BIL106E Introduction to Scientific & Engineering Computing
Sample Problems & Solutions 1
Problem 1: Find the result of the following mathematical expressions.
(a)
(b)
(c)
(d)
(e)
COS(0.0)
12 + SIN(0.0)
ABS(LOG(0.1))
7**SIN(0.0)
(0.5)**(1.0/SIN(0.0))
(f)
(g)
(h)
(i)
(j)
4.0**(8/(5*2))
2.0*EXP(1.0)
"ABC"(1:1)// "DEF"(2:3)// "XY"
1/9**(-1.0)
INT(COS(0.0)**SIN(0.0))
(f)
(g)
(h)
(i)
(j)
1.0
5.43
AEFXY
9.0
1
Solution 1:
(a)
(b)
(c)
(d)
(e)
1.0
12.0
2.30
1.0
Division by zero
Problem 2: What will be displayed by the program given
below?
program prob2
integer :: i, j
do i = 1, 20, 3
do j = 11, 1, -2
if(j==i) then
print *, "j=i", i+j
else if (j>i) then
print *, j-i
end if
end do
end do
end program prob2
Solution 2:
10
8
6
4
2
j=i
2
7
5
3
1
4
2
j=i
14
1
Problem 3: Write a program that reads an integer number and then finds whether it is an even number or
odd number. The result should be displayed.
Solution 3: (alternative solutions)
program p3a
integer :: n
print *, "enter an integer number:"
read *, n
if (n / 2 * 2 == n) then
print *, "n is even"
else
print *, "n is odd"
end if
end program p3a
program p3b
integer :: n
real :: x
print *, "enter an integer number:"
read *, n
x=n
if (x/2 == n/2) then
print *, "n is even"
else
print *, "n is odd"
end if
end program p3b
program p3c
integer :: n
print *, "enter an integer number:"
read *, n
if ((-1)**n < 0) then
print *, "n is odd"
else
print *, "n is even"
end if
end program p3c
program p3d
integer :: n
print *, "enter an integer number:"
read *, n
if (modulo(n,2)==0) then
print *, "n is even"
else
print *, "n is odd"
end if
end program p3d
program p3e
integer :: n
character(11) :: w
character(1) :: c
print *, "enter an integer:"
read *, n
write(w, '(i11)') n
c = w(11:11)
select case(c)
case("0", "2", "4", "6", "8")
print *, "n is even number"
case default
print *, "n is odd number"
end select
end program p3e
Problem 4: Write a program that reads three integer numbers from the keyboard and checks whether all
three number are equal. If the numbers are not equal the program should ask for a new set of integer
numbers until the numbers entered are equal.
Solution 4: (alternative solutions)
program p4
integer :: x, y, z
print *, "enter three integer that are all different:"
read *, x, y, z
do
if (x==y.or.x==z.or.y==z) then
print *, "the number are not different"
print *, "reenter three integers:"
read *, x, y, z
else
exit
end if
end do
end program p4
program p4
integer :: x, y, z
print *, "enter three integer that are all different:"
read *, x, y, z
do
if (x/=y.and.x/=z.and.y/=z) then
exit
else
print *, "the number are not different"
print *, "reenter three integers:"
read *, x, y, z
end if
end do
end program p4
Problem 5: Write a program that puts the letters in the array str=(/"p"," r", "a", "x", "b"/) in alphabetical
order. Also define a character variable of length five. Equate the letters of this variable to the elements of
the array str.
Solution 5:
program p5
character(len=1), dimension(5) :: str=(/"p","r", "a", "x", "b"/)
character(len=5) :: text
character(len=1) :: temp
integer :: i, j
do i = 1, 4
do j = i+1, 5
if (str(j)<str(i)) then
temp = str(i)
str(i)=str(j)
str(j)=temp
end if
end do
end do
do i = 1, 5
text(i:i) = str(i)
end do
print *, str
print *, text
end program p5
Problem 6: The coefficient of lift of an airfoil is defined as
𝑐𝑙 = 2𝜋𝛼
where 𝜋 is the mathematical quantity pi and 𝛼 is the angle of attack in radians. Write a program that
calculates and displays the lift coefficient in the interval −1 𝑟𝑎𝑑 ≤ 𝛼 ≤ 1 𝑟𝑎𝑑 with increments of 0.1 rad
for 𝛼.
Solution 6:
program p6
real, parameter :: pi=3.141592
real :: alpha, cl
print *, "alpha (rad)
cl"
print *, "-----------------"
do i = 0, 20
alpha = -1.0 + i * 0.1
cl = 2.0 * pi * alpha
print *, alpha, cl
end do
end program p6
Problem 7: Consider the electrical circuit given in the figure below. The current in the circuit will be
nonzero if both switches are closed. By defining two logical variables A and B write a program that finds
whether the circuit is closed depending on the values of A and B.
A
B
V
Solution 7:
program p7
logical :: a, b
a = .true.
b = .true.
if (a.and.b) then
print *, "circuit closed"
else
print *, "circuit open"
end if
end program p7
Download