TEACHING

(FORTRAN)


Lab Solutions


	double precision dx, points(100), ferror(100), cerror(100), deriv(100)
	real point, pointm1, pointp1
	read*, dx
!	nPoint is the number of points
	nPoint = 1 + 2 / dx
!	Compute the forward and central differences and derivative
	do 1 i=1, nPoint
	  point		= -1 + dx*(i-1)
	  pointm1	= point - dx
	  pointp1	= point + dx
	  diffF		= (sin(pointp1)-sin(point))/dx
	  diffC		= (sin(pointp1)-sin(pointm1))/(2*dx)
	  ferror(i)	= abs(diffF - cos(point))
	  cerror(i)	= abs(diffC - cos(point))
1	continue
!	Find the maximum error for central and forward differencing
	fMaxF	= 0
	fMaxC	= 0
	do 2 i=1, nPoint
	  if(ferror(i).ge.fMaxF) fMaxF=ferror(i)
	  if(cerror(i).ge.fMaxC) fMaxC=cerror(i)
2	continue
	write(*,100) fMaxC, fMaxF
100	format(F10.5, 5x, F10.5)
	stop
	end



	dimension input(10), maxInp(10)
!	Read the inputs
	read*, (input(i), i=1, 10)
!	Find the maximum once, set it to zero,
!	so that that is not maximum anymore.
!	Repeat this step 10 times to sort all array
	do 1 i=1, 10
	  fMax = -100
	  do 2 j= 1, 10
	    if(input(j).ge.fMax) then
		  fMax		= input(j)
		  indexM	= j
	    endif
2	  continue
!	Display the output at each step
	  print*, fMax
	  input(indexM)	= 0
!	Store the sort values in an array (in case you need)
	  maxInp(i)		= fMax
1	continue
	stop
	end



!	Program evaluting the transpose of a specific matrix	
	integer matrix(10,10), matrixT(10,10)
!	Read the dimension of the square matrix
	read*, N
	if(N.lt.7) then
	  icount = 1
	  do 10 i=1, N
	    do 20 j= 1, N
	      matrix(i,j) = icount
	      icount = icount +1
20	    continue
10	  continue
!	Interchange the indices so that you obtain transpose of the matrix
	  do 30 i=1, N
	    do 40 j= 1, N
	      matrixT(j,i) = matrix(i,j)
40	    continue
30	  continue
!	Display the output
	  do 50 i=1, N
	    print*, (matrixT(i,j), j=1, N)
50	  continue
	else
	  print*, "INVALID VALUE!"
	endif
	stop
	end



	integer matrix(10,10)
	read*, N
	icount = 1
!	Construct the matrix
	do 10 i=1, N
	  do 20 j=1, N
	    if(j.ge.i) then
		  matrix(i,j) = icount
	      icount = icount + 1
	    else
	      matrix(i,j) = 0
	    endif
20	  continue
10	continue
!	Display the matrix
	do 30 i=1, N
	  print*, (matrix(i,j), j=1, N)
30	continue
	stop
	end