#23 (04/16/2025)

FORTRAN

10 REM BASIC program
20 PRINT "HELLO WORLD"
30 END
c      FORTRAN program
       PRINT *,"HELLO WORLD !"
       STOP
       END
/* C program */
#include <stdio.h>
int main()
{
printf("Hello world !\n");
return 0;
}
/* C++ program */
#include <iostream>
int main()
{
cout<<"Hello world !\n";
return 0;
}
// Java program
public class MyClass{
   public static void main(String[] args){
             System.out.println("Hello world !");
         } 
}
FORTRAN (FOrmulaTRANslation) was created a half century ago and was the de-facto programming language for engineers and scientists until recently because of the inertia and a vast amount of subroutines written (and fully debugged). Although it's rare these days that you write a large program in FORTRAN from scratch, it is still important that you are able to read a FORTRAN program somebody wrote before.

FORTRAN features

  1. Case INsensitive.
  2. NOT free format, the entire line is 80 columns wide.
    1. Column 1 is reserved for comments. If any character is written in Column 1, that line is interpreted as a comment line.
    2. Columns 2-5 are reserved for line identification (used in go to statements).
    3. Column 6 is reserved for continuation. If you write any character on Column 6, that line is interpreted as a continuation line.
    4. Columns 7-72 are reserved for FORTRAN statement. This is the only space you can write your own program.
  3. Variables beginning with I-N are automatically declared as integers by default. All other variables are implicitly declared as real numbers. To override this rule, you need to explicitly declare variables as INTEGER, REAL or DOUBLE PRECISION. However, it is strongly encouraged that you declare all the variables explicitly.

How to run a FORTRAN program

FORTRAN is a compiled language so it is run much the same way as C programs are run. On omega.uta.edu, you can first prepare a FORTRAN source code and then issue:
$ f95 MyProgram.f; a.out

Free Fortran/C/C++ compilers (g77/gcc/g++) for Windows system

If you need GNU C/C++/Fortran compilers (gcc, g++, g77) that run under Windows systems, download the following file:
Double click (or execute) the file above to extract all the files under the c:\gcc-2.95.2 directory. Open a DOS window. cd to c:\gcc-2.95.2 and run mingw32.bat to add the path.
cd c:\gcc-2.95.2
mingw32
To compile a C program, issue
gcc MyProgram.c
To compile a C++ program, issue
g++ MyProgram.cpp
To compile a FORTRAN program, issue
g77 MyProgram.f
This will produce an executable file, a.exe instead of a.out.

Sketch of comparison between C and FORTRAN

Quadratic equation
/* This program computes roots 
for quadratic equation. */
#include <stdio.h>
#include <math.h>

int main()
{
double a, b, c, disc, x1, x2;

printf("Enter 3 coeffs separated by space =");
scanf("%lf %lf %lf", &a, &b, &c);

disc=b*b-4*a*c;

if (disc<= 0) 
  { printf("Imaginary roots !\n"); return 0;}

x1= (-b + sqrt(disc))/(2*a);
x2= (-b - sqrt(disc))/(2*a);

printf("The roots are %lf and %lf. \n", x1, x2);

return 0;
}
C     This program computes roots 
C       for quadratic equations.
      DOUBLE PRECISION A, B, C, DISC, X1,X2
      WRITE(*,*) "Enter three coefficients"
      READ(*,*) A, B, C
      DISC=B**2-4.0*A*C

      IF(DISC.LE.0.0) THEN
       WRITE(*,*) "Imaginary roots !"
      STOP
      ENDIF

      X1=(-B+SQRT(DISC))/(2.0*A)
      X2=(-B-SQRT(DISC))/(2.0*A)
      WRITE(*,*) "Roots are ", X1, X2
      STOP
      END

 
Note:
A.LE.B A is less than or equal to B (a <= b)
A.LT.B A is less than B (a < b)
A.EQ.B A is equal to B (a == b)
A.GT.B A is greater than B (a > b)
A.GE.B A is greater than or equal to B (a >= b)
A.NE.B A is not equal to B (a != b)
A.AND.B A and B (a && b)
A.OR.B A or B (a || b)
Series summation
#include <stdio.h>
#include <math.h>

int main()
{
  int i;
  double sum = 0.0;
  for (i=0; i<1000; i++) 
    sum+=pow(-1,i)/(double)(2*i+1);
  printf("approx= %f true value= %lf\n ",
   4*sum, 4*atan(1.0));
return 0;
}

      DOUBLE PRECISION SUM
      INTEGER I
      SUM=0.0

      DO 10 I=0,999,1
       SUM=SUM+(-1)**I/(2.0*FLOAT(I)+1.0)
 10   CONTINUE

      WRITE(*,*) "Approx and exact values =", 4.0*SUM,
     c  4.0*ATAN(1.0)
      STOP
      END

 
Array
#include <stdio.h>
#include <math.h>

#define N 10

int main()
{
  float x[]={ -4.0, 1.2, 1.3, 2.5, -12.7, 
   9.0, 1.41, 65.2, -2.1, 2.36};
  int i;
  float sum = 0.0, average, variance;

  for (i=0; i<N; i++) sum+=x[i];
  average=sum/N;

  for (i=0; i<N; i++) 
   variance += pow(x[i]-average, 2);
  variance = variance/(N-1);

  printf("avg.= %f std. dev. = %f \n ",
    average, sqrt(variance));
  return 0;
}


      PARAMETER(N=10)
      REAL X(N), SUM, AVE, VAR
      INTEGER I

      DATA X /-4.0,1.2,1.3,2.5,-12.7,9.0,1.41,
     c 65.2,-2.1, 2.36/

      SUM=0.0

      DO 10 I=1,N
       SUM=SUM+X(I)
 10   CONTINUE

      AVE=SUM/FLOAT(N)

      DO 20 I=1,N
       VAR=VAR+(X(I)-AVE)**2
 20   CONTINUE
      VAR=VAR/(FLOAT(N)-1.0)

      WRITE(*,*) "Average=", AVE,
     c  ' Standard deviation =', SQRT(VAR)
      STOP
      END

 
Functions
#include <stdio.h>
#include <math.h>
#define EPS 1.0e-6

double f(double x)
{
	return x*x-2;
}

double fp(double x)
{
	return 2*x;
}

double newton(double x)
{
	return x - f(x)/fp(x);
}

int main()
{
	double x1, x2;
	int i;

	printf("Enter initial guess  =");
	scanf("%lf", &x1);

	if (fp(x1)==0.0) {
		printf("No convergence.\n");
		return;
	}

	for (i=0;i<100;i++)
	{
		x2=newton(x1);
		if (fabs(x1-x2)< EPS) break;
		x1=x2;
	}

	printf("iteration = %d\n", i);
	printf("x= %lf\n", x1);
	return 0;
}


******************************
      FUNCTION F(X)
      DOUBLE PRECISION F, X
      F=X*X-2.0
      RETURN
      END
******************************
      FUNCTION FP(X)
      DOUBLE PRECISION FP, X
      FP=2.0*X
      RETURN
      END
******************************
      FUNCTION NEWTON(X)
      DOUBLE PRECISION NEWTON, X, F, FP
      NEWTON=X-F(X)/FP(X)
      RETURN
      END
****************************

      PARAMETER(EPS=1.0E-6)
      DOUBLE PRECISION X1, X2, NEWTON, F, FP
      INTEGER I

      WRITE(*,*) "Enter initial guess"
      READ(*,*) X1

      IF(FP(X1).EQ.0.0) THEN
        WRITE(*,*) "No convergence"
        STOP
      ENDIF

      DO 10 I=0,99,1
       X2=NEWTON(X1)
       IF(ABS(X1-X2).LT.EPS) GOTO 11
       X1=X2
 10   CONTINUE

 11   CONTINUE

      WRITE(*,*)"Iteration =",I
      WRITE(*,*)"X=",X1

      STOP
      END

 
File handling
#include <stdio.h>
int main()
{
   FILE *fp1, *fp2; 
   float a,b,c;
   fp1=fopen("junk1.dat","r");
   fscanf(fp1, "%f", &a);
   fclose(fp1)

   fp2=fopen("junk2.dat","w");
   fprintf(fp2,"This is the first file.\n");
   fclose(fp2);

   return 0;
}

      REAL A, B, C
      B=3.14
C
      OPEN(UNIT=1, FILE='junk1.dat', STATUS='old')
      READ(1, *) A
      WRITE(*,*) A
      CLOSE (1)
C
      OPEN(UNIT=2, FILE='junk2.dat', STATUS='new')
      WRITE(2,*) 'This is the first file.'
      WRITE(2,*) B
      CLOSE(2)
C
      STOP
      END

 

Why FORTRAN should not be used

  1. Spaghetti programming
          integer i,k,j
          write(*,*) 'Enter i, k and j'
          read(*.*) i,k,j
      99  if(i .lt. j) goto 33
          goto 55
      33  i=j
          goto 99
       55 k=j+1
          stop
          end
    
  2. Recursive algorithm is not allowed.
  3. No structured programming.

Why FORTRAN should be used

  1. Fully debugged subroutines
  2. Compilers are optimized.
  3. Vast amount of legacy code




File translated from TEX by TTH, version 4.03.
On 13 Apr 2025, 20:17.