Title First Step to Computer Programming With FORTRAN 90 Types School Work Essays & Theses 236.3ย KB 14
##### Document Text Contents
Page 1

Kurdistan Iraqi Region
Ministry of Higher Education
University of Sulaimani
College of Science
Physics Department

Dr. Dlear R. Saber
Dr. Omed Gh. Abdullah
Mr. Ary A. Abdulrahman
Mr. Shaho Osman

2009 - 2010

First step to

Computer Programming

With FORTRAN 90
First stage

Page 2

2

Problem (1): Write a Fortran program to calculate the value of function ๐๐(๐๐); where x= [-5, 0, 5]:

๐๐(๐ฑ๐ฑ) = ๏ฟฝ
๐ญ๐ญ๐ญ๐ญ๐ญ๐ญโ๐๐(๐ฑ๐ฑ) + ๐๐๐ฑ๐ฑ (๐ฑ๐ฑ > 0)
๐๐ (๐ฑ๐ฑ = ๐๐)
๏ฟฝ๐ฑ๐ฑ๐๐ + ๐๐ (๐ฑ๐ฑ < 2)

๏ฟฝ

Solution:
if(x>0) then
y=Atan(x) +exp(x)
print*, x, y
elseif(x<0) then
y=sqrt(x**2+2)
print*, x, y
else
y=0
print*, x, y
endif
end

#################################################################################

Problem (2): Write a program in Fortran 90 using coditional control statement to find the value of

(๐๐,๐พ๐พ) according to value of ๐๐,๐๐ from the equations below, if(๐๐ = ๐๐) and (๐๐ = ๐๐):

๏ฟฝ ๐ณ๐ณ =
|๐ฑ๐ฑ| + (๐ฑ๐ฑ + ๐ฒ๐ฒ)

๐ฐ๐ฐ = ๐ณ๐ณ|๐ฑ๐ฑ โ ๐ฒ๐ฒ|
๏ฟฝ ๐ฑ๐ฑ โฅ ๐๐

๏ฟฝ๐ณ๐ณ =
(๐ฑ๐ฑ + ๐ฒ๐ฒ)๐๐

๐ฑ๐ฑ (๐ฑ๐ฑ + ๐ณ๐ณ)
๐ฐ๐ฐ = ๐ฑ๐ฑ๐๐ + ๐๐๐ณ๐ณ

๏ฟฝ ๐ฑ๐ฑ < 0

Solution:
do
if(x==0.or.x>0) goto 50
z=(x+y)**3/(x+z)
w=x**2+2*z
goto 70
50 z=abs (x)+(x+y)
w=z/abs(x+z)
70 print*, z,w
if (choice==0) exit
enddo
end

#################################################################################

Page 7

7

max=x
min=x
do i=1,9
if (x>max) max=x
if (x<min) min=x
enddo
write(*,*) 'maximum no.=',max
write(*,*) 'minimum no.=',min
end

#################################################################################

Problem ( 13): Write a Fortran program to find the number of odd and even integers among (15)

numbers; [read the the numbers from text data file]:

Solution:
open(5,file='data.txt')
odd=0
even=0
do i=1,15
if (x==(int(x/2)*2)) then
even=even+1
else
odd=odd+1
endif
enddo
write(*,*)'number of odd=',odd
write(*,*)'number of even=',even
end

#################################################################################

Problem ( 14): Write a Fortran program to sort (15) numbers, from minimum to maximum; [read

the the numbers from text data file]:

Solution:
dimension a(15)
open (5,file='data.txt')
do i=1,15
enddo
do i=1,14
do j=i+1,15
if (a(i)>a( j))then
x=a(i)

Page 8

8

a(i)=a(j)
a(j)=x
endif
enddo
enddo
do i=1,15
write(*,*) a(i)
enddo
end

#################################################################################

Problem (15): Write a Fortran program to computes the square rootes of 1, 1.5, 2, 2.5, 3, ......, 10

by Newtonโs method:

๐บ๐บ๐ต๐ต๐ต๐ต ๐๐ =
๐๐
๐๐
๏ฟฝ๐๐ +

๐๐
๐๐
๏ฟฝ

Solution:
do b=1,10,0.5
x=b
do
xn=0.5*(x+b/x)
if (abs (x-xn)<0.00001) exit
x=xn
enddo
print*,'sqrt(',b,')=',x
enddo
end

#################################################################################

Problem (16): Write a Foretran program to show how to write a counting loop with real numbers.

variable ๐ฑ๐ฑ receves values โ๐๐.๐๐ ,โ๐๐.๐๐๐๐ ,โ๐๐.๐๐ ,โ๐๐.๐๐๐๐ ,๐๐. ,๐๐.๐๐๐๐ ,๐๐.๐๐ ,๐๐.๐๐๐๐ ๐๐๐๐๐๐ ๐๐.๐๐;
[without using do-loops]

Solution:
real , parameter :: lower = -1.0
real , parameter :: upper = 1.0
real , parameter :: step = 0.25
real :: x=lower
do
if (x>upper) exit
print*, x
x=x+ step
enddo
end

#################################################################################

Page 13

13

end

#################################################################################

Problem (27): Write a program in Fortran 90 to find the transpose of the following matrix:

๐บ๐บ = ๏ฟฝ
๐๐ ๐๐ ๐๐ ๐๐๐๐
๐๐ ๐๐ ๐๐ ๐๐๐๐
๐๐ ๐๐ ๐๐ ๐๐๐๐

๏ฟฝ

Solution:
program transpose_array
integer,dimension(3,4):: N
integer,dimension(4,3):: M
do i=1,3
enddo
do i=1,3
do j=1,4
m(j,i) = N(i,j)
enddo
enddo
do i=1,4
write(*,*)(M(i,j),j=1,3)
enddo
end Program transpose_array

#################################################################################

Problem (28): Write a program in Fortran 90 to find the value of matrix ๐ฉ๐ฉ when ๐ฒ๐ฒ = ๐๐:

Solution:
program B_array
integer,dimension(2,2)::A,B
do J=1,2
do I=1,2
B(I,J)=K*A(I,J)
enddo
enddo
print*,'The result of matrix'
do I=1,2
write(*,*)(B(I,J),J=1,2)
enddo
end Program B_array

๏ฃบ
๏ฃป

๏ฃน
๏ฃฏ
๏ฃฐ

๏ฃฎ
ร=

36
54

KB

Page 14

14

#################################################################################

Problem (29): Write a Fortran program to find the result of multiplication of two matrix

๐ช๐ช(๐๐,๐๐) = ๐จ๐จ(๐๐,๐๐) โ ๐ฉ๐ฉ(๐๐,๐๐), where:

๐จ๐จ(๐๐,๐๐) = ๏ฟฝ
๐๐ โ๐๐
๐๐ ๐๐
๐๐ ๐๐

๏ฟฝ

๐ฉ๐ฉ(๐๐,๐๐) = ๏ฟฝโ๐๐ ๐๐ ๐๐
๐๐ ๐๐ ๐๐

๏ฟฝ

Solution:
integer,dimension(3,2):: A
integer,dimension(2,3):: B
integer,dimension(3,3):: C
write(*,*)'Input the matrix A(3,2)'
do i=1,3
enddo
write(*,*)'Input the matrix B(2,3)'
do i=1,2
enddo
do i=1,3
do j=1,3
C(i,j)=0
do k= 1,2
C(i,j)=C(i,j)+A(i,k)*B(k,j)
enddo
enddo
enddo
do I=1,3
print*, (c(i,j),j=1,3)
enddo
end
#################################################################################