• No results found

First Step to Computer Programming With FORTRAN 90

N/A
N/A
Protected

Academic year: 2021

Share "First Step to Computer Programming With FORTRAN 90"

Copied!
14
0
0

Loading.... (view fulltext now)

Full text

(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

(2)

2

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

๐Ÿ๐Ÿ(๐ฑ๐ฑ) = ๏ฟฝ ๐ญ๐ญ๐ญ๐ญ๐ญ๐ญโˆ’๐Ÿ๐Ÿ(๐ฑ๐ฑ) + ๐ž๐ž๐ฑ๐ฑ (๐ฑ๐ฑ > 0) ๐ŸŽ๐ŸŽ (๐ฑ๐ฑ = ๐ŸŽ๐ŸŽ) ๏ฟฝ๐ฑ๐ฑ๐Ÿ๐Ÿ+ ๐Ÿ๐Ÿ (๐ฑ๐ฑ < 2) Solution: read*, x 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 read*,x,y 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 read*, choice if (choice==0) exit enddo end #################################################################################

(3)

3

Problem (3): Write a Fortran program to calculate exponential of (1) from library function (๐‘ฌ๐‘ฌ๐‘ฌ๐‘ฌ๐‘ฌ๐‘ฌ)

and from the following series up to (15):

๐ž๐ž๐ฑ๐ฑ = ๐Ÿ๐Ÿ + ๐ฑ๐ฑ +๐ฑ๐ฑ๐Ÿ๐Ÿ ๐Ÿ๐Ÿ! + ๐ฑ๐ฑ๐Ÿ‘๐Ÿ‘ ๐Ÿ‘๐Ÿ‘! + ๐ฑ๐ฑ๐Ÿ’๐Ÿ’ ๐Ÿ’๐Ÿ’! + โ‹ฏ Solution: x=1 s=1 do i=1,15 f=1 do j=1,i f=f*j enddo s=s+x**i/f enddo print*,s,exp(x) end #################################################################################

Problem (4): Write a Fortran program to calculate exponential of (1) from library function (๐‘ฌ๐‘ฌ๐‘ฌ๐‘ฌ๐‘ฌ๐‘ฌ)

and from the following series up to (15), [without using do-loops]:

๐ž๐ž๐ฑ๐ฑ = ๐Ÿ๐Ÿ + ๐ฑ๐ฑ +๐ฑ๐ฑ๐Ÿ๐Ÿ ๐Ÿ๐Ÿ! + ๐ฑ๐ฑ๐Ÿ‘๐Ÿ‘ ๐Ÿ‘๐Ÿ‘! + ๐ฑ๐ฑ๐Ÿ’๐Ÿ’ ๐Ÿ’๐Ÿ’! + โ‹ฏ Solution: x=1 b=1 n=0 s=1 10 n=n+1 if (n<=15) then b=b*x/n s=s+b goto 10 endif z=exp(x) print*,s,z end #################################################################################

Problem (5): Write a fortran program to calculate ๐’„๐’„๐’„๐’„๐’„๐’„(๐’™๐’™) from library function (๐‘ช๐‘ช๐‘ช๐‘ช๐‘ช๐‘ช) and from following series up to power (20):

๐œ๐œ๐œ๐œ๐œ๐œ( ๐’™๐’™) = ๐Ÿ๐Ÿ โˆ’

๐’™๐’™๐Ÿ๐Ÿ!๐Ÿ๐Ÿ

+

๐’™๐’™๐Ÿ’๐Ÿ’!๐Ÿ’๐Ÿ’

โˆ’

๐’™๐’™๐Ÿ”๐Ÿ”!๐Ÿ”๐Ÿ”

+ โ‹ฏ

Solution:

read*, x s=1

(4)

4 sign=-1 do i=2,20,2 f=1 do j=1,i f=f*j enddo s=s+(sign*x**i)/f sign=-1*sign enddo print*,s,cos(x) end #################################################################################

Problem (6): Write a Fortran program to calculate sin(x) from library function (๐‘ช๐‘ช๐‘บ๐‘บ๐‘บ๐‘บ) and the

following series up to power (15), [input the value of ๐’™๐’™ by degree]:

๐œ๐œ๐ฌ๐ฌ๐ญ๐ญ ๐ฑ๐ฑ =๐Ÿ๐Ÿ! โˆ’๐ฑ๐ฑ ๐ฑ๐ฑ๐Ÿ‘๐Ÿ‘! +๐Ÿ‘๐Ÿ‘ ๐ฑ๐ฑ๐Ÿ“๐Ÿ“! โˆ’ โ‹ฏ๐Ÿ“๐Ÿ“ Solution: read*, x c=(22./7)/180 x=x*c sign=1 s=0 do i=1,15,2 f=1 do j=1,i f=f*j enddo s=s+sign*x**i/f sign=-1*sign enddo print*,s,sin(x) end #################################################################################

Problem (7): Write a Fortran program to calculate the sum of series from one to (n) integer [without using do-loops]:

Solution: integer i,n read*,n sum=0 do 10 i=1,n sum=sum+i print*,i,sum

(5)

5

10 continue print*,sum end

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

Problem (8): Write afortran program to print the following series, [without using do-loops]:

( ๐Ÿ๐Ÿ , ๐Ÿ๐Ÿ , ๐Ÿ’๐Ÿ’ , ๐Ÿ–๐Ÿ– , ๐Ÿ๐Ÿ๐Ÿ”๐Ÿ” , ๐Ÿ‘๐Ÿ‘๐Ÿ๐Ÿ , ๐Ÿ”๐Ÿ”๐Ÿ’๐Ÿ’ , ๐Ÿ๐Ÿ๐Ÿ๐Ÿ๐Ÿ–๐Ÿ– ) Solution: integer n n=1 10 print*,n If (n<=100) then n=2*n goto 10 endif end #################################################################################

Problem (9): Write a Fortran program to find the real solution of the function:

๐’‡๐’‡(๐’™๐’™) = ๐’™๐’™๐Ÿ๐Ÿ+ ๐Ÿ๐Ÿ๐’™๐’™ โˆ’ ๐Ÿ๐Ÿ

Using the equation:

๐’™๐’™ =โˆ’๐’ƒ๐’ƒ ยฑ โˆš๐’ƒ๐’ƒ๐Ÿ๐Ÿ๐Ÿ’๐Ÿ’๐Ÿ๐Ÿโˆ’ ๐Ÿ’๐Ÿ’๐Ÿ’๐Ÿ’๐’„๐’„ Solution: read*, a,b,c s=b**2-4*a*c if (s<0) then print*,'ther is no solution' else x1=(-b+sqrt(S))/(2*a) x2=(-b-sqrt(s))/(2*a) print*,x1,x2 endif end #################################################################################

Problem (10): Write a Fortran program to read a two number (๐Ÿ’๐Ÿ’, ๐’ƒ๐’ƒ), and check if (๐Ÿ’๐Ÿ’) is posetive or negative or zero , and compare (๐Ÿ’๐Ÿ’) with (๐’ƒ๐’ƒ):

Solution:

read*,a,b if (a>0) then if (a>b) then

(6)

6

print*,'(a) is posetive and greater than (b)' elseif (a<b) then

print*,'(a) is posetive and smaller than (b)' else

print*,'(a) is posetive and equal to (b)' endif

elseif (a<0) then if (a>b) then

print*,'(a) is negative and greater than (b)' elseif (a<b) then

print*,'(a) is negative and smaller than (b)' else

print*,'(a) is negative and equal to (b)' endif

else

if (a>b) then

print*,'(a) is zero and greater than (b)' elseif (a<b) then

print*,'(a) is zero and smaller than (b)' else

print*,'(a) is zero and (b) is zero ' endif

endif end

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

Problem (11): Write a loop to read (๐’๐’) real numbers and print their sum after all aditions:

Solution: s=0.0 read*, n summation: do i=1,n read*,i sum=sum+i print*,sum end do summation end #################################################################################

Problem (12): Write a Fortran program to find the maximum and minimum values among ten numbers; [read the the numbers from text data file]:

Solution:

open(5,file='data.txt') read(5,*)x

(7)

7 max=x min=x do i=1,9 read(5,*)x 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 read(5,*)x 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 read(5,*) a(i) enddo do i=1,14 do j=i+1,15 if (a(i)>a( j))then x=a(i)

(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 #################################################################################

(9)

9

Problem (17): Write a Fortran program to multiple i and j , where i=1:9 , j=1:9

Solution: integer i, j do i=1,9 do j=1,9 k= i*j print*,k enddo enddo end #################################################################################

Problem (18): Write a program in Fortran 90 to calculate the value of ๐‘ช๐‘ช from the equation below: ๐œ๐œ = ๏ฟฝ ๏ฟฝ ๐ฌ๐ฌ โˆ— ๐ฃ๐ฃ ๐Ÿ“๐Ÿ“ ๐ฃ๐ฃ=๐Ÿ๐Ÿ ๐Ÿ‘๐Ÿ‘ ๐ฌ๐ฌ=๐Ÿ๐Ÿ Solution: program summation s=0.0 do i=1,3 do j=1,5 s=s+i*j enddo enddo

write(*,*)'The result value of S=', s end program summation

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

Problem (19): Write a Fortran program to sort the following numbers from biger to smaller [use

Data comand to read the numbers]:

๐ฑ๐ฑ = [๐Ÿ“๐Ÿ“ , ๐Ÿ–๐Ÿ– , โˆ’๐Ÿ๐Ÿ , ๐Ÿ๐Ÿ , ๐Ÿ”๐Ÿ” , ๐Ÿ‘๐Ÿ‘ , ๐ŸŽ๐ŸŽ , ๐Ÿ’๐Ÿ’ , ๐Ÿ—๐Ÿ— , ๐Ÿ๐Ÿ] Solution: dimension x (10) Parameter (n=10) data (x(i),i=1,n) /5,8,0,2,6,3,-2,4,9,1/ k=1 10 do i=k,n if (x(k)<x(i)) then s=x(k) x(k)=x(i) x(i)=s endif enddo k=k+1

(10)

10

If (k<=n) goto 10 print*,(x(i),i=1,n) end

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

Problem (20): Write a Fortran program to computes these two quantites from (10) numbers; [read the the numbers from text data file]:

๐ฑ๐ฑ๏ฟฝ =๐ญ๐ญ ๏ฟฝ ๐ฑ๐ฑ๐ฌ๐ฌ๐Ÿ๐Ÿ ๐ญ๐ญ ๐ฌ๐ฌ=๐Ÿ๐Ÿ ๐œ๐œ๐Ÿ๐Ÿ= ๐Ÿ๐Ÿ ๐ญ๐ญ โˆ’ ๐Ÿ๐Ÿ ๏ฟฝ(๐ฑ๐ฑ๐ฌ๐ฌ โˆ’ ๐ฑ๐ฑ๏ฟฝ) ๐ญ๐ญ ๐ฌ๐ฌ=๐Ÿ๐Ÿ Solution: dimension x(10) real::xbar=0 std=0 n=10 open(1,file='data.text') do i=1,n read(1,*) x(i) xbar=xbar+x(i) enddo xbar=xbar/n do i=1,n std=std+(x(i)-xbar)**2 enddo std=sqrt(std/(n-1)) write(*,*)'mean=',xbar write(*,*)'std=',std end #################################################################################

Problem (21): Write a Fortran program to calculate the ages average of (10) students in the first stage; [read the the numbers from text data file], where:

๐ญ๐ญ๐š๐š๐ž๐ž ๐ญ๐ญ๐š๐š๐ž๐ž๐š๐š๐ญ๐ญ๐š๐š๐ž๐ž = ๏ฟฝ ๐€๐€๐ฌ๐ฌ ๐Ÿ๐Ÿ๐ŸŽ๐ŸŽ ๐ˆ๐ˆ=๐Ÿ๐Ÿ Solution: real,dimension(10)::a open(4,file='data.txt') sum=0.0 do i=1,10 read(4,*) a(i) sum=sum+a(i) enddo

(11)

11

average = sum/10

print*,'the age average of student is =',average end

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

Problem (22): Write a Fortran program to find the average marks of class (10 students) for computer examination using dimension statement; [read the data from screan]:

Solution:

real,dimension(10)::x real::s

s=0.0 do i=1,10

print*,'supply mark of student no.',i read*, x(i) s=s+x(i) end do print*,'average s core is ', s/10 end #################################################################################

Problem (23): Write a program in Fortran 90 to find the value of ( S ) from sireas bellow:

๐‘ช๐‘ช = ๐Ÿ๐Ÿ โˆ’๐Ÿ๐Ÿ๐Ÿ๐Ÿ๐Ÿ๐Ÿ+๐Ÿ๐Ÿ๐Ÿ๐Ÿ๐Ÿ’๐Ÿ’โˆ’๐Ÿ๐Ÿ๐Ÿ๐Ÿ๐Ÿ”๐Ÿ”+๐Ÿ๐Ÿ๐Ÿ๐Ÿ๐Ÿ–๐Ÿ–โˆ’๐Ÿ๐Ÿ๐Ÿ๐Ÿ๐Ÿ๐Ÿ๐ŸŽ๐ŸŽ+๐Ÿ๐Ÿ๐Ÿ๐Ÿ๐Ÿ๐Ÿ๐Ÿ๐Ÿ Solution: s=1 j=0 do i=2,20,2 j=j+1 s=s+(-1)**j/2.**i enddo print*,'the value of S =',s end #################################################################################

Problem (24): Write a Fortran program to calculate (๐‘ฌ๐‘ฌ) from following equation:

๐‘ฌ๐‘ฌ = ๏ฟฝ๏ฟฝ(๐’๐’ + ๐Ÿ“๐Ÿ“) ๐Ÿ๐Ÿ๐ŸŽ๐ŸŽ ๐’๐’=๐Ÿ๐Ÿ ๏ฟฝ ๐Ÿ๐Ÿ/๐Ÿ๐Ÿ Solution: p=0.0 do n=1,10 p=p+(n+5)

(12)

12 enddo p=sqrt(p) print*,p end #################################################################################

Problem (25): Write a Fortran program to calculat the value of (๐‘จ๐‘จ๐‘จ๐‘จ) from the following equation, when (๐’๐’ = ๐Ÿ“๐Ÿ“) and (๐’Ž๐’Ž = ๐Ÿ•๐Ÿ•): ๐‘จ๐‘จ๐‘จ๐‘จ = ๏ฟฝ๏ฟฝ(๐’Ž๐’Ž + ๐’๐’)๐Ÿ๐Ÿ ๐Ÿ๐Ÿ๐Ÿ๐Ÿ ๐’Š๐’Š=๐Ÿ๐Ÿ ๏ฟฝ ๐Ÿ๐Ÿ/๐Ÿ๐Ÿ Solution: Al=0.0 n=5 m=7 do i=1,12 Al=Al+(m+n)**2 enddo Al=sqrt(Al) print*, Al end #################################################################################

Problem (26): Write a program in Fortran 90 to calculate the resultant matrix ๐‘ช๐‘ช(๐‘บ๐‘บ, ๐‘ฑ๐‘ฑ) from [๐‘ช๐‘ช(๐‘บ๐‘บ, ๐‘ฑ๐‘ฑ) = ๐‘จ๐‘จ(๐‘บ๐‘บ, ๐‘ฑ๐‘ฑ) + ๐‘ฉ๐‘ฉ(๐‘บ๐‘บ, ๐‘ฑ๐‘ฑ)] when: Solution: integer,dimension(3,3):: a,b,c do j=1,3 do i=1,3 read(*,*)a(i,j),b(i,j) enddo enddo do j=1,3 do i=1,3 c(i,j)=a(i,j)+b(i,j) enddo enddo do i=1,3 write(*,*)(c(i,j),j=1,3) enddo

๏ฃบ

๏ฃบ

๏ฃบ

๏ฃป

๏ฃน

๏ฃฏ

๏ฃฏ

๏ฃฏ

๏ฃฐ

๏ฃฎ

=

๏ฃบ

๏ฃบ

๏ฃบ

๏ฃป

๏ฃน

๏ฃฏ

๏ฃฏ

๏ฃฏ

๏ฃฐ

๏ฃฎ

=

90

60

30

80

50

20

70

40

10

)

,

(

,

9

6

3

8

5

2

7

4

1

)

,

(

I

J

B

I

J

A

(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 read(*,*)(N(i,j),j=1,4) 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 read(*,*)K do J=1,2 do I=1,2 read(*,*)A(I,J) 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

๏ฃบ

๏ฃป

๏ฃน

๏ฃฏ

๏ฃฐ

๏ฃฎ

ร—

=

3

6

5

4

K

B

(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

read(*,*) (A(i,j),j=1,2) enddo

write(*,*)'Input the matrix B(2,3)' do i=1,2 read(*,*) (B(i,j),j=1,3) 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 #################################################################################

References

Related documents