0

I need to translate a Fortran 77 code to C language, I have aprox 90% of the translation but I don't understand some parts of Fortran, for example, in Fortran I have:

DIMENSION COEF(3,3),EXPON(3,3) DATA COEF,EXPON/1.0D0,2*0.0D0,0.678914D0,0.430129D0,0.0D0, $ 0.444635D0,0.535328D0,0.154329D0,0.270950D0,2*0.0D0,0.151623D0, $ 0.851819D0,0.0D0,0.109818D0,0.405771D0,2.22766D0/

In this part, I have a two arrays of length 3, so, when I read the documentation of DATA, I need to put each value in the two arrays, so I have the next block of code in C:

COEF[0][0] = 1.0;
COEF[0][1] = 2.0;
COEF[0][2] = 0.6789140;
COEF[1][0] = 0.4301290;
COEF[1][1] = 0.0;
COEF[1][2] = 0.4446350;
COEF[2][0] = 0.5353280;
COEF[2][1] = 0.1543290;
COEF[2][2] = 0.2709500;

EXPON[0][0] = 2.0;
EXPON[0][1] = 0.1516230;
EXPON[0][2] = 0.8518190;
EXPON[1][0] = 0.0;
EXPON[1][1] = 0.1098180;
EXPON[1][2] = 0.4057710;
EXPON[2][0] = 2.227660;
EXPON[2][1] = 0.0;
EXPON[2][2] = 0.0;

Assume that I need to associate the list of argument to the list of values, I just have 16 values, but I have 18 spaces between two arrays, so what happen with the last two spaces? XD

snake_404
  • 111
  • 5
  • 15
  • Do not specify too many questions in one post, that makes it too broad. You can easily find some answer by searching around here. The meaning of 1.0d-6 is very easy to find (just a double prec. of 1e-6). The arithmetic if was also treated several times on this site. – Vladimir F Героям слава Mar 09 '16 at 09:28
  • I deleted the part that has no connection. See http://stackoverflow.com/questions/12567575/fortran-basic-help-d-operator and http://stackoverflow.com/questions/11124855/fortran-strange-if – Vladimir F Героям слава Mar 09 '16 at 09:45
  • 2
    In a `data` statement `2*0` does not mean multiplication, but multiplicity. That is, one gets two zero values, not one (of two times zero). There should be other questions to find covering that, if that is your confusion. – francescalus Mar 09 '16 at 09:48
  • That's true. I didn't even read the details of that part. I have found a question where that is mentioned but not an exact duplicate. – Vladimir F Героям слава Mar 09 '16 at 10:00
  • Sorry for the questions, in the first part, 2*0 is not my confusion, is about how to assign the values, COEF and EXPON, are arrays of length 3, i have assigned the first 9 values to COEF, but i have just 7 values remaining to assign to EXPON, so i don't know if i have assigned the values correctly. – snake_404 Mar 09 '16 at 17:35
  • Sorry again, EXPON and COEF are a bidimensional array of length 9. – snake_404 Mar 09 '16 at 18:20
  • 1
    In Fortran, the data is stored in row major. Assign your data COEF[0][0], COEF[1][0], COEF[2][0], COEF[0][1] ... otherwise when you translate any algorithms, it is going to go bezerk. – cup Mar 28 '16 at 11:54

2 Answers2

2

There are 18 values in the original DATA statement.

I believe you are confused by the multiplicity, just as stated by francescalus

The 2*0 means: two zeroes. It does not mean 2.0, it does not mean 2x0. It means: "Two zeroes, coming up!"

I think you need something like this:

COEF[0][1] = 0.0; COEF[0][2] = 0.0;

(and also for the 2 zeroes in EXPON)

Then your 18 values will line-up as you expect and you will not have any uninitialized slots in your C arrays.

Steve Rawlins
  • 350
  • 2
  • 10
0

You don't need to use executable assignments in C; if you wish, you can initialize a variable in its definition, which is the declaration that allocates storage. But aggregate initialization defaults to storage order and as @cup commented Fortran uses column-major while C uses row-major (and 0-origin vs default 1), so you must either transpose the data:

float coef[3][3] = { 1.0, 0.678914, 0.444635,
    0, 0.430129, 0.535328,   0, 0, 0.154329 };

or (assuming C99+) use designated initializer syntax:

float coef[3][3] = { [0][0]=1.0, [1][0]=0, [2][0]=0,
    [0][1]=0.678914,[1][1]=0.430129,[2][1]=0,
    [0][2]=0.444635,[1][2]=0.535328,[2][2]=0.154329 };
/* you can omit the zero-valued entries; in an aggregate 
    intializer any unspecified components are zero */
dave_thompson_085
  • 34,712
  • 6
  • 50
  • 70