41
Wednesday, September 24, 2008 Fortran for bce-I/II HOW TO START FORTRAN First of all copy the fortran file on the C:\> drive and follow the instructions as below To open the file and creat First of all go on ‘start menu’ and then click on ‘run’ and type ‘cmd or command’ and press enter. Type ‘cd\fortran’ and press enter Then type ‘edit filename.for’ and press enter Then make the program and follow instructions below to run this To run the program: Go ‘start menu’ click ‘run’ and type ‘cmd’ and press enter Then type’ cd\fortran’ and press enter Then type’ for1 filename.for’ and press enter Then type ‘pas2;’ and press enter Then type ‘link filename.for’ and press enter Then type ‘filename.exe or filename;’ and press enter BUT FORCE2 IS COMMONLU USED INSTEAD OF THIS FORTRAN First of all setup the force2 on your computer and run it Type the program code and save it Then run it. Set-I 1

Fortran code for ioe studient

Embed Size (px)

Citation preview

Page 1: Fortran  code for ioe studient

Wednesday, September 24, 2008 Fortran for bce-I/II

HOW TO START FORTRAN

First of all copy the fortran file on the C:\> drive and follow the instructions as below

To open the file and creat

First of all go on ‘start menu’ and then click on ‘run’ and type ‘cmd or command’ and press enter.

Type ‘cd\fortran’ and press enter

Then type ‘edit filename.for’ and press enter

Then make the program and follow instructions below to run this

To run the program:

Go ‘start menu’ click ‘run’ and type ‘cmd’ and press enter

Then type’ cd\fortran’ and press enter

Then type’ for1 filename.for’ and press enter

Then type ‘pas2;’ and press enter

Then type ‘link filename.for’ and press enter

Then type ‘filename.exe or filename;’ and press enter

BUT FORCE2 IS COMMONLU USED INSTEAD OF THIS FORTRAN

First of all setup the force2 on your computer and run it

Type the program code and save it

Then run it.

Set-IQ. No. 1.1 Write a programme to input the temperature in celcius and convert into farenheight.Use formula:

* This is a program to convert temperature from celcius to farenheight

write(*,*)'Input temperature in celcius'

read(*,*)c

f=9./5*c+32

write(*,*)'The equivalent temperature in Farenheight is',f

end

1

Page 2: Fortran  code for ioe studient

Wednesday, September 24, 2008 Fortran for bce-I/II

Q. No. 1.2. Wrie a programme to calculate the area of triangle when three sides are given.We Have a, b, c are theree sides of triangle then:a < b + cb < c + ac < a + bS = (a + b + c) / 2Area ( A ) = sqrt ( s ( s - a ) ( s - b ) ( s - c ) )

* This is a programme to calculate area of Tiangle using three sides

1 write(*,*)'Enter first side of triangle ie. a'

read(*,*)a

write(*,*)'Enter second side of triangle ie. b'

read(*,*)b

write(*,*)'Enter third side of triangle ie. c'

read(*,*)c

x=b+c

y=c+a

z=a+b

s=0.5*(a+b+c)

If(x.gt.a.and.y.gt.b.and.z.gt.c) then

area=sqrt(s*(s-a)*(s-b)*(s-c))

write(*,*)'The area of triangle is:',area

else

write(*,*)'......................'

write(*,*)'An error has been occured'

write(*,*)'These values of a,b,c doesnot form triangle'

write(*,*)'Plz check the sides and try again'

write(*,*)

write(*,*)'Again'

goto 1

endif

end

Q. No. 1.3.Write a programme to solve simultaneous equations of two unknowns of the form:

a1x+b1y=c1

2

Page 3: Fortran  code for ioe studient

Wednesday, September 24, 2008 Fortran for bce-I/II

a2x+b2y=c2

* Program to solve sumultaneus equations

write(*,*)’enter the values of a1,b1,c1,a2,b2,c2’

read(*,*) a1,b1,c1,a2,b2,c2

d1=c1*b2-b1*c2

d2=a1*c2-c1*a2

d=a1*b2-b1*a2

x1=d1/d

x2=d2/d

write(*,*)’x1=’,x1,’x2=’,x2

end

Q. No. 1.4.Write a programme to calculate the total base area, curve surface area, total surface area and total bolume of acylinder by giving the value of radius and height.Use formulaBase surface area BSA = 2πr2

Curve Surface Area CSA = 2πrhTotal Surface Area TSA = BSA + CSATotal Volume TV = πr2h.

* This is a programme to display all calculation of a cylinder

write(*,*)'Enter the radius'

read(*,*)r

write(*,*)'Enter the height'

read(*,*)h

bsa=2*3.1415*r**2

csa=2*3.1415*r*h

tsa=bsa+csa

tv=3.1415*r**2*h

write(*,*)'The total Base surface area is ',bsa

write(*,*)'The total Curve surface area is',csa

write(*,*)'The total Surface area is',tsa

write(*,*)'The total Volume is',tv

end

3

Page 4: Fortran  code for ioe studient

Wednesday, September 24, 2008 Fortran for bce-I/II

Set-IIQ. No. 2.1 Wrie a programme to find maturity value (V)

*program to find the maturity value (v)

write(*,*)’enter the values of p,r and y’

read(*,*) p,r,y

v=p*(1+r/100)**y

write(*,*) ‘the value of v is ‘,v

stop

end

Q. No. 2.2 Wrie a programme to input the two sides of a triangle and the angle (in degree) invcluded by them and calculate the third side.Use formula:Remaining Third Side C = Sqrt (a2 + b2- 2ab*Cos X)

* This is a programme to calculate third side of triangle by inputing two

* sides and angle between them

write(*,*)'The first side of triangle is '

read(*,*)a

write(*,*)'The second side of triangle is '

read(*,*)b

write(*,*)'The angle in degree betn 1st side and 2nd side is '

read(*,*)x

c=sqrt(a**2+b**2-2*a*b*cos(3.1415*x/180))

write(*,*)'The Third of triangle is c=',c

end

Q. No. 2.3.Wrie a programme tocalculate the length of parabola when height (h) and base length (2b) is given:Use Formula:L = Sqrt [4h2 + b2] + b2/2h * Ln [(2h + sqrt[4h2 + b2) / b]

* This is a programme to calculate the length of parabola height and base

* length is given

real l

4

Page 5: Fortran  code for ioe studient

Wednesday, September 24, 2008 Fortran for bce-I/II

write(*,*)'The semi-base length of parabola is:'

read(*,*)b

write(*,*)'The height of parabola is:'

read(*,*)h

x=sqrt(4*h**2+b**2)

l=x+b**2/2./h*log((2*h+x)/b)

write(*,*)'The length of parabola is l=',l

end

Set-IIIQ. No. 3.1. Wrie a programme to solve a quadrantic equation using arithmatic IFEvaluation of Discreminant:Quadratic Equation: ax2 + bx + c = 0Discreminant d2 = b2 - 4ac

* This is a programme to solve quadratic equation

write(*,*)'Enter the coefficient of second degree of x ie. a'

read(*,*)a

write(*,*)'Enter the coefficient of first degree of x ie. b'

read(*,*)b

write(*,*)'Enter the constant ie. c'

read(*,*)c

d=b*b-4*a*c

If(d-0) 1,2,3

2 x=b**2/2./a

write(*,*)'The values of x coincide & equal to x=',x,x

goto 100

3 x1=(-b+sqrt(d))/2./a

x2=(-b-sqrt(d))/2./a

write(*,*)'The values are real and distinct and are x=',x1,x2

goto 100

1 x1=-b/2./a

5

Page 6: Fortran  code for ioe studient

Wednesday, September 24, 2008 Fortran for bce-I/II

x2=sqrt(abs(d))/2./a

x3=-x2

write(*,*)'The values of x are immmaginary'

write(*,*)'The first value is',x1,'+',x2,'i'

write(*,*)'The second value is',x1,'-',x2,'i'

100 stop

end

Q. No. 3.2. Wrie a programme to generate a multiplication table of a given number.

* This is a programme to generate the multiplication table

integer a,c

write(*,*)'Enter the number whose multiplication table is needed'

read(*,*)a

write(*,*)'Enter the no. upoto which table is to be calutlated'

read(*,*)j

do 10 i=1,j,1

c=a*i

write(*,1)a,i,c

1 format(1x,i4' x'i3' ='i5)

10 continue

End

Q. No. 3.3. Wrie a programme to find the factorial of a given number.

* This is a programme to calculate the factorial of a given number

write(*,*)'Enter the number'

read(*,*)n

j=1

do 10 i=1,n,1

j=i*j

10 continue

write(*,*)n,'!=',j

end

Q. No. 3.4. Write a programme to input the year and display wheather it is leap year or not.

* This is a programme to check fo leap year

6

Page 7: Fortran  code for ioe studient

Wednesday, September 24, 2008 Fortran for bce-I/II

integer year

write(*,*)'Input the year'

read(*,*)year

If(mod(year,400).eq.0) goto 1

If(mod(year,100).eq.0) goto 2

If(mod(year,4).eq.0) goto 1

write(*,*)'This is not leap year',year

goto 3

1 write(*,*)'This is a leap year',year

goto 3

2 write(*,*)'This is a leap year',year

3 stop

end

Set-IVQ. No. 4.1. Wrie a programme to Check wheather a given number is prime or not.

* This is a programme to check wheather given number is prime or not

write(*,*)'Enter the number'

read(*,*)n

do 10 i=2,n/2

if(mod(n,i).eq.0) goto 1

10 continue

write(*,*)'It is a prime number'

goto 2

1 write(*,*)'It is not a prime number'

2 end

Q. No. 4.2. Wrie a programme to compute the reverse of a given integer number.

* This is a programme to get the reverse digit

Write(*,*)'Enter the Number'

7

Page 8: Fortran  code for ioe studient

Wednesday, September 24, 2008 Fortran for bce-I/II

read(*,*)n

k=n

m=0

10 m=m*10+mod(k,10)

k=k/10

If(k.gt.0) goto 10

write(*,*)'The reverse digit of',n,' is',m

end

Q. No. 4.3. Wrie a programme to chek wheather the given number is perfect number or not.

* This is a program to check wheather given number is perfect or not

Write(*,*)'Enter the number that u wanna to check for a perfect'

write(*,*)'number'

read(*,*)n

nsum=0

do 10 i=1,n/2,1

10 if(mod(n,i).eq.0) nsum=nsum+i

if(nsum.ne.n) goto 1

write(*,*)n,' is a perfect number'

goto 2

1 write(*,*)n,' is not perfect number'

2 stop

End

Q. No. 4.4. Wrie a programme to check wheather given number is Armsturng Number or not.

* This is a programme to check either given number is armstrung or not

Write(*,*)'Enter the number to check for Armstrung number'

8

Page 9: Fortran  code for ioe studient

Wednesday, September 24, 2008 Fortran for bce-I/II

read(*,*)n

k=n

m=0

33 m=m+1

k=k/10

if(k.gt.0) goto 33

k=n

j=0

100 j=(mod(k,10))**m +j

k=k/10

if(k.gt.0) goto 100

if (m.eq.1)then

write(*,*)n,' is NOT an Armstrung Number'

stop

endif

11 if(j.eq.n) then

write(*,*)n,' is an Armstrung number'

else

write(*,*)n,' is NOT an Armstrung Number'

endif

end

Q. No. 4.4.1. Wrie a programme to generate first N Armstrung numbers.

9

Page 10: Fortran  code for ioe studient

Wednesday, September 24, 2008 Fortran for bce-I/II

* This is a programme to generate first N armstrung numbers

Integer a,b

write(*,*)'How much first Armtrung number u wanna to see?'

read(*,*)n

kount=1

b=10

write(*,5)n

5 format(1x,'The first',i4,' Armstrung numbers are:-')

1 m=0

a=b

2 m=m+1

a=a/10

if(a.gt.0) goto 2

j=0

a=b

3 j=(mod(a,10))**m+j

a=a/10

if(a.gt.0) goto 3

a=b

if(j.eq.a) then

write(*,*)a

kount=kount+1

10

Page 11: Fortran  code for ioe studient

Wednesday, September 24, 2008 Fortran for bce-I/II

endif

b=b+1

if (kount.le.n) goto 1

end

Q. No.4.4.2 . Wrie a programme to generate all Armstrung Nos. within specified range.

* This is a programme to list out all armstrung numbers within range

integer a,b

Write(*,*)'Enter the lower range'

read(*,*)a

write(*,*)'Enter the upper range'

read(*,*)b

if(b.lt.153) goto 44

write(*,*)'The armstrung numbers within the range are'

if (a.le.9) a=10

do 50 i=a,b

k=i

m=0

33 m=m+1

k=k/10

if(k.gt.0) goto 33

sum=0

k=i

11

Page 12: Fortran  code for ioe studient

Wednesday, September 24, 2008 Fortran for bce-I/II

100 if(k.le.0) goto 60

j=(mod(k,10))**m

sum=sum+j

k=k/10

goto 100

60 if (sum.eq.i) write(*,*)i

50 continue

goto 45

44 write(*,*)'There doesnot exist any Armstrung number'

45 end

Set-VQ. No. 5.1. Write a programme to calculate the mothly electricity bill amount on the basis of following criteria:Minimum Charge (Upto 20 Unite) = Rs. 80.0021 to 250 units = Rs. 7.30 per unit.250 unit or above = Rs. 10.5 per unit.

* This is a programme to calculate mothly bill of electricity

Integer R

write(*,*)'Input the meter reading in unit'

read(*,*)r

If(r.le.20)goto 1

If(r.gt.20.and.t.le.250)goto 2

If(r.gt.250)goto 3

1 b=80

write(*,*)'You have to pay Rs.',b

12

Page 13: Fortran  code for ioe studient

Wednesday, September 24, 2008 Fortran for bce-I/II

goto 10

2 b=80+(r-20)*7.30

write(*,*)'You have to pay Rs.',b

goto 10

3 b=80+230*7.30+(r-250)*10.50

write(*,*)'You have to pay Rs.',b

10 stop

end

Q. No. 5.2. Wrie a programme to find sum upto N terms of series:Sin X = x - x3/3! + x5/5! - x7/7! + ............

*Program to find the value of sinx

write(*,*)'enter series limiting no.n and value for x in degree'

read(*,*)n,x

x=3.1416*x/180

sum=x

term=x

do 5 i=2,n

term=term*(-1)*x**2/((2*i-1)*(2*i-2))

sum=sum+term

5 continue

write(*,*)sum

end

Q. No. 5.3. Wrie a programme to find sum upto N terms of series:ex = 1 + x/1! + x2/2! + x3/3! + ............

* This is a programme to evaluate exponentials

Write(*,*)'Enter the value of X'

read(*,*)x

write(*,*)'Enter the number up to u want to to sum up the series'

read(*,*)n

sum=1

term=1

do 1 i=2,n,1

term=term*x/(i-1)

13

Page 14: Fortran  code for ioe studient

Wednesday, September 24, 2008 Fortran for bce-I/II

1 sum=sum+term

write(*,*)'The value of e^',x,' is =',sum

end

Q. No. 5.3. Wrie a programme to find sum upto N terms of series:

Π = 4* (1-1/3+1/5-1/7+ …………………<!--[if !supportLineBreakNewLine]--><!--[endif]-->

*To find the value of PI

Write(*,*) 'enter no. od terms n'

Read(*,*) n

Term=1

Sum=0

sign=-1

Do 5 i=1,n

Sum=sum+term

Term=sign/(2*i+1)

sign=-1*sign

5 continue

Write(*,*) 'the value of PI is :',sum*4

End

Set-VIQ. No. 6.1. Wrie a programme to compute Mean, Variance and Standard Deviation from N data inputs.Mean = ∑x/NVariance = (N(∑x2) - (∑x)2)/N(N-1)SD = Sqrt (Variance)

* This is a programme to calculate the mean, vaiance, and SD

write(*,*)'Input the number of data ie. N'

read(*,*)n

sumx=0

sumxsq=0

14

Page 15: Fortran  code for ioe studient

Wednesday, September 24, 2008 Fortran for bce-I/II

i=1

2 if(i.gt.n)goto 1

write(*,*)'Entr the values of x'

read(*,*)x

sumx=sumx+x

sumxsq=sumxsq+x**2

i=i+1

goto 2

1 xmean=(sumx)/n

var=(n*(sumxsq)-(sumx)**2)/(n*(n-1))

SD=sqrt(var)

write(*,*)'The mean value =',xmean

write(*,*)'The variance =',var

write(*,*)'The Standard Deviation =',sd

end

Q. No. 6.2. Wrie a programme to generate all prime numbers within a given range.

* This is a progrmme to generate all prime numbers within range

integer a,b

write(*,*)'Enter the lower range'

read(*,*)a

write(*,*)'Enter the upper range'

read(*,*)b

if(a.gt.b) then

k=a

a=b

b=k

end if

if(a.le.1) a=2

if(b.le.1) b=50

write(*,*)'The prime numbers within range are'

do 5 i=a,b

do 6 j=2,i/2

6 if( mod(i,j).eq.0)goto 5

15

Page 16: Fortran  code for ioe studient

Wednesday, September 24, 2008 Fortran for bce-I/II

write(*,*)i

5 continue

End

Q. No. 6.2.1. Wrie a programme to generate first n prime numbers.

* This is a pogramme to generate first n prime numbers

integer count

write(*,*)'Enter the number hou many u wanna to see'

read(*,*)n

write(*,*)'The first',n,' prime numbers are'

count=1

m=2

22 If(count.gt.n)goto 100

do 1 i=2,m/2

if(mod(m,i).eq.0)goto 8

1 continue

write(*,*)m

count=count+1

8 m=m+1

goto 22

100 stop

End

Q. No. 6.3. Wrie a programme to compute natural logarithm table of values from 1.0 ,1.1,1.2- - - - - - - - -to 9.8,9.9 in the following format

0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9

1 - - - - - - - - - - - - - - - - -- --

2 -- -- -- -- -- -- -- -- -- --

3 -- -- -- -- -- -- -- -- -- --

4 -- -- -- --- -- -- -- -- -- --

5 -- -- -- -- -- -- -- -- -- --

6 -- -- -- -- -- -- -- -- -- --

7 -- -- -- -- -- -- -- -- -- --

8 -- -- -- -- -- -- -- -- -- --

9 -- -- -- -- -- -- -- -- -- --

16

Page 17: Fortran  code for ioe studient

Wednesday, September 24, 2008 Fortran for bce-I/II

* program to display log table

write(*,*) 'log table is:'

write(*,1) (a/10,a=0,9)

do 10 i=1,9

10 write(*,2) i,(log(i+a/10),a=0,9)

2 format(1x,i2,10(2x,f5.3))

1 format(/,/,1x,10(5x,f2.1))

end

Set-VII (Matrix arithmetic)

Q. No. 7.1 . Wrie a programme to compute the inner-product of 2 arrays with same numer of elements.

Inner Product = ∑[a(i) * b(i)] where i = 1 to n

* This is a programme to compute inner products

real a(100),b(100)

write(*,*)'Enter the No. of terns'

read(*,*)n

write(*,*)'Enter the elements of first array'

17

Page 18: Fortran  code for ioe studient

Wednesday, September 24, 2008 Fortran for bce-I/II

do 1 i=1,n

1 read(*,*) a(i)

write(*,*)'Enter the elemnts of second array'

read(*,*) (b(i),i=1,n)

sum=0

do 2 i=1,n

sum=sum+a(i)*b(i)

2 continue

write(*,*)'The inner product is:',sum

end

Q. No. 7.2 . Wrie a programme to read an array of numbers with N elements and sort the elemnts in decending order.

* Display all elements of array in descending order

real a(100),b(100)

write(*,*)'Enter the no. of elements'

read(*,*)n

write(*,*)'Enter the elements'

read(*,*)(a(i),i=1,n)

write(*,*)'The number in descending order are'

do 10 i=1,n

do 20 j=i+1,n

if(a(i).le.a(j)) then

temp=a(i)

18

Page 19: Fortran  code for ioe studient

Wednesday, September 24, 2008 Fortran for bce-I/II

a(i)=a(j)

a(j)=temp

endif

20 continue

write(*,*)a(i)

10 continue

end

Q. No. 7.3. Wrie a programme to compute Karlpearson's Correlation coefficient from N numbers of data pairs:

Karipearson's Correlation Coeff.

r = ?[(x-xmean)(y-ymean)] ÷ [Sqrt {?(x - xmean)2 (y - ymean)2}]

* This is a prgramme to calculae the Karlpearson corelation coeff.

Dimension x(100),y(100)

write(*,*)'Enter the number of elements of array ie.frequency'

read(*,*)n

write(*,*)'Enter the values of x'

read(*,*) (x(i),i=1,n)

write(*,*)'Enter the values of y'

read(*,*) (y(i),i=1,n)

xsum=0

ysum=0

xysum=0

xsq=0

19

Page 20: Fortran  code for ioe studient

Wednesday, September 24, 2008 Fortran for bce-I/II

ysq=0

do 1 i=1,n

xsum=xsum+x(i)

1 ysum=ysum+y(i)

xmean=xsum/n

ymean=ysum/n

do 2 i=1,n

xdiff=x(i)-xmean

ydiff=y(i)-ymean

xysum=xysum+xdiff*ydiff

xsq=xsq+xdiff**2

2 ysq=ysq+ydiff**2

r=xysum/sqrt(xsq*ysq)

write(*,*)'The Karlpersons corelation coeff. is r=',r

end

Set-VIII (Matrix computation)

Q. No. 8.1. Wrie a programme to compute D=AB+CT,where A,B,C and D are all matrices of valid order and CT is the transpose of C.

* This is a programme to compute D=AB+CT

real A(10,10),B(10,10),C(10,10),D(10,10)

goto 200

20

Page 21: Fortran  code for ioe studient

Wednesday, September 24, 2008 Fortran for bce-I/II

100 write(*,*)'Such order of matrix can not be multiplied'

write(*,*)'Again'

200 write(*,*)'Enter the order of matrix A'

read(*,*)m,n

write(*,*)'Enter the order of matrix B'

read(*,*)p,q

write(*,*)'Enter the order of matrix C'

read(*,*)r,s

if (n.ne.p.or.m.ne.s.or.q.ne.r) goto 100

write(*,*)'Enter the elements of matrix A:'

read(*,*) (( A(i,j),j=1,n),i=1,m)

write(*,*)'Enter the elements of matrix B:'

read(*,*) ((B(i,j),j=1,p),i=1,q)

write(*,*)'Enter the elements of matrix B:'

read(*,*) ((C(i,j),j=1,r),i=1,s)

write(*,*)'the value of AB+CT is:'

do 3 i=1,m

do 3 j=1,q

D(i,j)=0

do 3 K=1,n

3 D(i,j)=d(i,j)+a(i,k)*b(k,j)

do 4 i=1,s

do 4 j=1,r

4 C(i,j)=C(j,i)

do 5 i=1,m

do 5 j=1,q

5 D(i,j)=D(i,j)+C(i,j)

do 6 i=1,m

6 write(*,*)(D(I,j),j=1,q)

end

Q. NO. 8.2. Wrie a programme to check or matrix wheather it is symmetric or not.

*to check or symmetric matrix

real A(10,10)

21

Page 22: Fortran  code for ioe studient

Wednesday, September 24, 2008 Fortran for bce-I/II

2 write(*,*)'enter the order of matrix A'

read(*,*)m,n

write(*,*)'enter the elements'

read(*,*)((A(i,j),j=1,m),j=i,n)

if (m.ne.n) goto 2

do 1 i=1,m

do 1 j=1,n

if(A(i,j).ne.a(j,i)) then

write(*,*)'the matrix is not symmetric'

end if

stop

1 continue

write(*,*)'the matrix is symmetric'

stop

end

Set-IX (Data file) # a data file defined as old status in the programme should exist in the defined location .

# a data file defined as new satus in the programme shouldnot exist in the defined location.

Q-9.1 Wrie a programme to calculate the standard deviation of data stores in a data file.

* Compute Standard Deviation by reading data from a file

open(1,file='data.txt',status='old')

n=0

sumx=0

sumxsq=0

22

Page 23: Fortran  code for ioe studient

Wednesday, September 24, 2008 Fortran for bce-I/II

1 read(1,*,end=100)x

n=n+1

sumx=sumx+x

sumxsq=sumxsq+x**2

goto 1

100 var=(N*sumxsq-sumx**2)/((n-1)*n)

sd=sqrt(var)

write(*,*)'The value of Standard Deviation is',sd

endfile 1

end

Q. No. 9.2. Wrie a programme to read the dimensions and elements of two matrices from a data file and multiply them and store the result in another data file.

* This is a programme to add data from external input

integer a(10,10),b(10,10),c(10,10),ma,na,mb,nb

goto 200

100 write(*,*)'Such order of matrix can not be multiplied'

write(*,*)'Again'

200 open(1,file='101.txt',status='old')

open(2,file='456.txt',status='new')

read(1,*)ma,na

do 9 i=1,ma

9 read(1,*,end=11)(a(i,j),j=1,na)

read(1,*)mb,nb

do 32 i=1,mb

32 read(1,*,end=11)(b(i,j),j=1,nb)

if (ma.ne.nb) goto 100

do 1 i=1,ma

do 1 j=1,nb

23

Page 24: Fortran  code for ioe studient

Wednesday, September 24, 2008 Fortran for bce-I/II

c(i,j)=0

do 1 k=1,na

c(i,j)=c(i,j)+a(i,k)*b(k,j)

1 continue

write(*,*)

write(*,*)'See the answer in 102.txt'

write(*,*)

write(2,*)'The resultant matrix A x B ='

write(*,*)

do 2 i=1,ma

2 write(2,*) (c(i,j),j=1,nb)

endfile 1

endfile 2

goto 99

11 write(*,*) 'Insufficient data in data file'

99 stop

End

Q. No. 9.3. Wrie a programme to read the marks of n students in 5 subjcts an do the following:

i> Compute total marks, percentage, minimum and maimum marks obtained by each students.

ii> Compute average, minimum and maximum marks obtained in each subject.

iii> Display the result in Mark-Sheet format.

--------------------------------------------------------------------------------

* This is Demo-Version of Marksheet

real marks(10,5),rper(10),rtot(10),rmax(10),rmin(10)

real ctot(5),cavg(5),cmax(5),cmin(5)

10 write(*,*)'How many sutdents (max=10)?'

24

Page 25: Fortran  code for ioe studient

Wednesday, September 24, 2008 Fortran for bce-I/II

read(*,*)n

if (n.gt.10) goto 10

write(*,*)'Now enter the marks in 5 subjects each:'

do 20 i=1,n

write(*,*)'students #',i

20 read(*,*) (marks(i,j),j=1,5)

c processing for row total, per, min, max ........

do 30 i=1,n

rtot(i)=0

rmin(i)=marks(i,1)

rmax(i)=marks(i,1)

do 40 j=1,5

rtot(i)=rtot(i)+marks(i,j)

if (marks(i,j).lt.rmin(i)) rmin(i)=marks(i,j)

if (marks(i,j).gt.rmax(i)) rmax(i)=marks(i,j)

40 continue

rper(i)=rtot(i)/5

30 continue

c processing for column total, avg, min, max .......

do 50 j=1,5

ctot(j)=0

cmax(j)=marks(1,j)

25

Page 26: Fortran  code for ioe studient

Wednesday, September 24, 2008 Fortran for bce-I/II

cmin(j)=marks(1,j)

do 60 i=1,n

ctot(j)=ctot(j)+marks(i,j)

if (marks(i,j).gt.cmax(j)) cmax(j)=marks(i,j)

if (marks(i,j).lt.cmin(j)) cmin(j)=marks(i,j)

60 continue

cavg(j)=ctot(j)/n

50 continue

Write(*,1)'RN','Math','Engg','Sci','Nep','His','Tot','Per',

$'Min','Max'

do 80 i=1,n

80 write(*,2)i,(marks(i,j),j=1,5),rtot(i),rper(i),rmin(i),rmax(i)

write(*,*)

write(*,3)'avg',(cavg(j),j=1,5)

write(*,3)'min',(cmin(j),j=1,5)

write(*,3)'max',(cmax(j),j=1,5)

1 format(////////////////////,2x,a3,9(2x,a6))

2 format(2x,i3,9(2x,f6.2))

3 format(2x,a3,5(2x,f6.2))

end

26

Page 27: Fortran  code for ioe studient

Wednesday, September 24, 2008 Fortran for bce-I/II

Set-X (function program and subroutine)

Q-10.1 Wrie a programme to find the weighted mean for n number of values stored in a array an weight stored in another array using a function subprogram.

* This is function subprogram

function sum(x,n)

real x(100),sum

sum=0

do 10 i=1,n

10 sum=sum+x(i)

end

* Main Program

real x(100), w(100), y(100)

write(*,*)'Enter Size'

read(*,*)n

write(*,*)'Enter NUMBER and WEIGHTAGE'

do 10 i=1,n

read(*,*) x(i),w(i)

10 y(i)=x(i)*w(i)

rmean=sum(y,n)/sum(w,n)

write(*,1)'Mean is ',rmean

27

Page 28: Fortran  code for ioe studient

Wednesday, September 24, 2008 Fortran for bce-I/II

1 format (1x,a7,3x,f10.2)

end

Q-10.2 Wrie function subprogramme to pass arry as arguments and hence display their mean.

*function subprogrmme to pass array as arguements and displey mean of them

function arsum(A,n)

real A(n)

sum=0

do 101 i=1,n

101 sum=sum+A(i)

arsum=sum

end

*main programme

real x(10)

write(*,*)'no. of elements'

read(*,*)m

write(*,*)'enter elements'

read(*,*)(x(i),i=1,m)

xmean=arsum(x,m)/m

write(*,*)xmean

end

Q-10.3 Wrie a subroutine programme to read in main programme the value of radius and height of a cylinder and compue base area, curve surface area total area, display the result in main progrmme.

28

Page 29: Fortran  code for ioe studient

Wednesday, September 24, 2008 Fortran for bce-I/II

*subroutine programme

subroutine cycalc(r,h)

real pi,ba,ca,voll,r,h

pi=22.0/7

ba=pi*r*r

ca=2*pi*r*h

ta=2*ba+ca

vol1=ba*h

write(*,*)'base area',ba

write(*,*)'curve area=',ca

write(*,*)'total area=',ta

write(*,*)'volume of the cylinder is =',voll

return

end

* main programme

real r1,h1

101 write(*,*)'enter the radius and height of the cylinder'

read(*,*)r1,h1

call cycalc(r1,h1)

write(*,*)'for more cylinder press 1 else 0'

read(*,*)i

if(i.eq.1) goto 101

29

Page 30: Fortran  code for ioe studient

Wednesday, September 24, 2008 Fortran for bce-I/II

end

Q-10.4. Wrie a subroutines each to:

i> Rad the valuesof a matrix.

ii> Write the values of a matrix.

iii> Multiply two matxrices.

iv> Add two matrices.

v> Find the difference of two matrixes.

vi> Transpose a matrix into another matrix.

Use these subroutines to compute the following

F = (A + B) * (C-D')

where A and B are of same order (say m x n) C and D are of same order n x p and p x n respectiveley (say)

* This is a subroutine programme

subroutine input(m,n,mm,nn,a)

integer p

real a(mm,nn)

do 10 i=1,m

10 read(*,*)(a(i,j),j=1,n)

end

subroutine matmul(m,n,p,mm,nn,a,b,c)

integer p

real a(mm,nn),b(mm,nn),c(mm,nn)

do 30 i=1,m

30

Page 31: Fortran  code for ioe studient

Wednesday, September 24, 2008 Fortran for bce-I/II

do 30 j=1,p

c(i,j)=0

do 30 k=1,n

c(i,j)=c(i,j)+a(i,k)*b(k,j)

30 continue

end

subroutine add(m,n,mm,nn,a,b,c)

real a(mm,nn),b(mm,nn),c(mm,nn)

do 40 i=1,m

do 40 j=1,n

c(i,j)=a(i,j)+b(i,j)

40 continue

end

subroutine subs(n,p,mm,nn,a,b,c)

integer p

real a(mm,nn),b(mm,nn),c(mm,nn)

do 40 i=1,n

do 40 j=1,p

c(i,j)=a(i,j)-b(i,j)

40 continue

end

subroutine tran(p,n,mm,nn,a,b)

31

Page 32: Fortran  code for ioe studient

Wednesday, September 24, 2008 Fortran for bce-I/II

integer p

real a(mm,nn),b(mm,nn)

do 50 i=1,p

do 50 j=1,n

b(j,i)=a(i,j)

50 continue

end

subroutine output(m,p,mm,nn,c)

integer p

real c(mm,nn)

do 60 i=1,m

60 write(*,*)(c(i,j),j=1,p)

end

c Main Program........................

integer p

real a(10,10),b(10,10),c(10,10),d(10,10),e(10,10),f(10,10),

$ g(10,10), h(10,10)

write(*,*)'Enter Order of Matrix A and B'

read(*,*) m,n

write(*,*)'Enter Matrix A'

call input(m,n,10,10,a)

write(*,*)'Enter Matrix B'

32

Page 33: Fortran  code for ioe studient

Wednesday, September 24, 2008 Fortran for bce-I/II

call input(m,n,10,10,b)

5 write(*,*) 'Enter the Order of Matrix C'

read(*,*) n1, p

if(n1.ne.n) then

write(*,*) 'Order Mismatch'

goto 5

endif

write(*,*)'Enter Matrix C'

call input(n,p,10,10,c)

6 write(*,*) 'Enter the order of Matrix D'

read(*,*) P2, N2

IF(n2.ne.n.or.p2.ne.p) then

write(*,*) 'Oder Mismatch'

goto 6

endif

write(*,*)'Enter Matrix D'

call input(p,n,10,10,d)

call add(m,n,10,10,a,b,e)

call tran(p,n,10,10,d,g)

call subs(n,p,10,10,c,g,h)

call matmul(m,n,p,10,10,e,h,f)

write(*,*)'The Output is'

33

Page 34: Fortran  code for ioe studient

Wednesday, September 24, 2008 Fortran for bce-I/II

call output(m,p,10,10,f)

end

34