Should I convert FORTRAN code to C?

Kjartan Pierre Emilsson Jardedlisfraedi kjartan at raunvis.UUCP
Tue Jun 14 01:41:02 AEST 1988




  Now here there seems to be a little misunderstanding.  I have never
understood why Fortran users keep saying that array indexing is awkward
in C.  This is probably because they find the idea of having to allocate
memory for an array rather repulsive.  I  remember that when I made
the change from Fortran to C, I thought that the memory allocation
routine malloc() was only intended for compiler designers and other
computer wizards.  But one quickly finds out that allocating memory
for whatever you are doing is not only simple but also helps you a lot
in keeping the whole program under control, since you have to have in 
mind a clear image of all your variables.
  For people that frequently use the same sort of data structures, e.g
two dimensional double precision array, it is a simple task to write
a little subroutine wich handles the allocation. For this particuliar
data structure, the subroutine is basically a one-liner:

double **Create2DArray(w,h)
int w,h;{ double **r;
for(r=(double**)calloc(h,sizeof(*r));h-->0;r[h]=(double*)calloc(w,sizeof(**r)));
return(r);}

  This may look very cryptic but then you only write it once anyway! A small
extension of the routine above could handle multidimensional array of
arbitrary dimensions and arbitrary data structures.
  

  If the array had been initialized with Create2DArray() then indexing
would be just as normal,i.e:

		DoSomething(array,m,n)
		double **array;
		int m,n;
		{
		...
		array[ i+k ][ j-i ]= 1.0;
		...
		}

 More generally: if an array is initialized with arbitrary dimensions, its
indexing is exactly as in FORTRAN, and can be passed to functions in exactly
the same manner (that is by pointers, which in this case is simply the array
name).

 I personally made the change from FORTRAN to C one year ago, and today I
do not regret it , though there came times when I wanted to do some really
mean things to that stupid computer.  I haven't  completely given up 
FORTRAN though, as there are still many libraries in FORTRAN around here, but I
simply build jacket routines for them (i.e routines which interface calling
conventions of two different languages) with the JBL utility. In that way
I don't have to write anything in FORTRAN but I still can use all the
available FORTRAN subroutines. Knowing that FORTRAN calls everything by
reference and C calls everything by value, it is easy to pass the arguments
from the C program in such a way that the only interfacing you need to do is
converting the pre-underscored C routine function name to the upper-case
FORTRAN name.

  I think that the C offers two qualities that makes it a very comfortable
scientific programming language:

		i) Argument are passed by value, which translates to
		   the fact that to let a subroutine change a variable
		   you have to specify it (by passing its adress).  This 
		   makes subroutines more like real mathematical operators
		   which have a very definite input and a very definite
		   output. This is really a psychological quality, but I
		   think that it has its effect in organizing your program
		   (a What You Get Is What You Asked For quality).

	       ii) You can define your own data structures, which can be any
		   combination of system defined structures and user defined
		   structures, array of structures, self-referential
		   structures and so on. This I think is perhaps the most
		   interesting quality and probably anyone who has done some
		   mathematics will appreciate the fact that most complex
		   problems can be made simpler by a suitable change of
		   formalism which most often is a change of data structure
		   with accompagnment of new operators acting on these
		   structures. For example if you are working in
		   4-dimensional space you simply define the data structure
		   4-vector and then proceed to write the basic operators
		   that you are going to need (DotProduct(vector1,vector2)
		   ,Add(vector1,vector2),Scale(scalar,vector), and so on).
		    Quickly you will see that your program is talking the
		    same mathematical language as you are, and program
		    writing then becomes a real FORmula TRANslation!

  This is not meant as an artillery in the Language Wars, but rather as an
explanation and maybe a clarification for those who haven't had any special
experience with C ,and still think that indexing doesn't exist in C and that
C is bad because it doesn't have the complex data type!

	Say it in any language you like, but say it clearly!


...........................................................................    

    "If you don't like what your left hemisphere thinks, shoot if off."

			Kjartan Pierre Emilsson, Iceland.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



More information about the Comp.lang.c mailing list