Download 24497 BIL106E Introduction to Scientific and Engineering

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

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

Document related concepts
no text concepts found
Transcript
24497 BIL106E Introduction to Scientific and Engineering Programming
2012-2013 Spring Semester Midterm Exam 1
05.04.2013
PROBLEM 1: (10p) Find the value of each of the following expressions
(a) 2/3/4
(b) 2/(3/4)
(c) 2/3/4.0
(d) 2/3.0/4
(e) 2**3**2
(f) 4**3/2
(g) 4**(3/2)
(h) 4**(3/2.0)
(i) 4.0**(3/2)
(j) "abcde"(3:5)//"-"// "99"
SOLUTION 1:
(a) 0
(b) division by zero
(c) 0.0
(d) 0.166666
(e) 512
(f) 32
(g) 4
(h) 8.0
(i) 4.0
(j) cde-99
PROBLEM 2: (10p) What will be the output of the following program?
program prn
integer :: i, j
do i = 5, 1, -1
print "(1x, 5i1)", (/(i-j+1, j=1, i)/)
end do
end program prn
SOLUTION 2:
◊54321
◊4321
◊321
◊21
◊1
PROBLEM 3: (10p) Consider the array numb = (/1, 2, 3, 4, 5, 6, 7, 8, 9, 0/). Print the array as
(a) a single row of numbers
(b) a single column of numbers
(c) two rows of five numbers
(d) five rows of two numbers
(e) a single row leaving one space between consecutive numbers
Your answers should be single print statements.
SOLUTION 3:
(a) print *, numb
(b) print "(i1)", numb
(c) print "(5(1x, i1))", numb
(d) print "(2(1x, i1))", numb
(e) print "(1x,10(i1, 1x))", numb
PROBLEM 4: (15p)Write a function the receives an integer number n as an argument and returns a
string value
"negative" – if n is a negative number
"zero"
– if n is equal to zero
"positive" – if n is a positive number.
SOLUTION 4:
function signum(n) result(ans)
integer, intent(in) :: n
character(len = 8) :: ans
select case(n)
case(:-1)
ans = "negative"
case(0)
ans = "zero"
case(1:)
ans = "positive"
end select
end function signum
PROBLEM 5: (15p) Write a subroutine that interchanges the first characters of two strings.
SOLUTION 5:
subroutine swapletter(a, b)
character(len = *), intent(inout) :: a, b
character(len = 1) :: temp
temp = a(1:1)
a(1:1)=b(1:1)
b(1:1)=temp
end subroutine swapletter
PROBLEM 6: (20p) Consider the figure below. The square OABC has
a sidelength of 1 cm. The coordinates of the point P are xP and yP,
respectively. Write a program that reads xP and yP and prints the output
message
y
C(0, 1)
B(1, 1)
P(xP, yP)
"inside"- when the point P is inside or on the edge of the square
"outside" - when the point P is outside the square.
x
O
A(1, 0)
SOLUTION 6:
program prob6
real :: xp, yp
print *, "Enter the x and y coordinates of the point P"
read *, xp, yp
if (xp<0.or.xp>1.or.yp<0.or.yp>1) then
print *, "outside"
else
print *, "inside"
end if
end program prob6
PROBLEM 7: (20p) The velocity of a particle P is defined by v P (t )  t 3  6t 2  11t  6 m / s .
Write a program that finds the instants in the interval t  0, 5s at which the velocity of the particle
is equal to zero. Do this by dividing the time interval into subintervals of 0.05 s and checking
whether the sign of the velocity function at an instant t i  t 0  i  t changes.
SOLUTION 7:
program prob7
integer :: i, n
real :: x1, x2, fx1, fx2
n = 5.0 / 0.05
do i = 0, n
x1 = i * 0.05
x2 = x1 + 0.05
fx1 = x1 ** 3 - 6.0 * x1 ** 2 + 11.0 * x1 - 6.0
fx2 = x2 ** 3 - 6.0 * x2 ** 2 + 11.0 * x2 - 6.0
if (fx1*fx2<0) then
print *, x1
end if
end do
end program prob7
Related documents