CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   Main CFD Forum (https://www.cfd-online.com/Forums/main/)
-   -   Arrays in Fortran & C (https://www.cfd-online.com/Forums/main/8438-arrays-fortran-c.html)

SwF December 11, 2004 08:44

Arrays in Fortran & C
 
I got accustomed that in Fortran A(i) is i-th element of the array A, but in C/C++ A[i] is (i+1)-th element of the array. Is it possible to do (to work) with indexes of arrays in C/C++ as in Fortran?

zxaar December 11, 2004 19:24

Re: Arrays in Fortran & C
 
you probaly want to say that in C/C++ arrays A[i] is i - 1 th element , ..... since arrays in C/C++ start at element 0, that is array with size 5 is 0 1 2 3 4 (from element index) ..... and in the end .... in all the computer languages ... A[i] means ith element .. only in C if you try to access A[array-size] it will give you an exception ....

andy December 11, 2004 19:52

Re: Arrays in Fortran & C
 
Yes. You can overload ( and ) in C++ but why bother? But more importantly in C/C++ arrays are not arrays in the Fortran sense but only pointers. This defeats the optimiser (aliasing) and leads to lower performance.

SwF December 12, 2004 02:04

Re: Arrays in Fortran & C
 
To zxaar: let i=0, on your logic A[0] is 0-1=-1-th element of the array (minus one element, i.e. nonsense). In fact: A[0] is 0+1=1-th element (the first element of the array). Is not it?

zxaar December 12, 2004 02:37

Re: Arrays in Fortran & C
 
but by your logic ... let i = 0 , then if u ask for A[0] computer shall return you A[0+1] or A[1] ...so please read first what you have written ... then we will think on other logic ...


SwF December 12, 2004 03:21

Re: Arrays in Fortran & C
 
I use logic accepted in Fortran and in usual life: a[0] is the first element, a[1] is the second, etc.

Suppose I have a piece of code in Fortran:

real*8 a(1:100),b(2:99)

do i=2,99

b(i)= a(i-1) - 2.0*a(i) + a(i+1)

end do


Please write this piece in C/C++.

andy December 13, 2004 11:06

Re: Arrays in Fortran & C
 
I think I misunderstood your question first time. You cannot do it in C but you can in C++ if you want to make the effort. Here is an example:

http://math.nist.gov/tnt/

of some relatively straightforward to understand C++ (unlike most). It will, of course, take ages to compile, require you to use the same version of the compiler for all code and be slower than Fortran but it can be done.


Praveen December 13, 2004 12:45

Re: Arrays in Fortran & C
 
If you want an array of size N just allocate for N+1. Then use the indices from i=1 to i=N as in fortran.

real a[N+1];

for(i=1; i<=N; i++) use a[i] for some computation

You will be wasting a[0] but no big deal.

D.M. Lipinski December 13, 2004 14:25

Re: Arrays in Fortran & C
 
SwF,

If you declare A as a ponter in C/C++ (i.e. double *A) , then allocate the required N elements with calloc/malloc. Now, you can pass A-1 instead of A into your C/C++ function. By doing this you pass just a fictitious address (one element less than the address of A) and, within the function, you can access the first element of A as A[1], i.e. fortran-like.

Hope it solves your problems

regards

DML

Amadou Sowe December 14, 2004 10:01

Re: Arrays in Fortran & C
 
I think this might be useful in attempting to do what Lipinski suggested:

#include <stdio.h> #include <math.h> #include <stdlib.h> #define N 101

int main() {

double *a, *b, pi;

int i;

/* Allocate memory for vector a so index manipulation instead can be done */ if((a=(double *) calloc(N, sizeof(double)))==(double *) NULL){

perror("calloc:a:"); /* Gives error message if memory not allocated */

exit(1); }

/* Allocate memory for vector b so index manipulation instead can be done */ if((b=(double *) calloc(N, sizeof(double)))==(double *) NULL){

perror("calloc:b:");/* Gives error message if memory not allocated */

exit(1); }

pi=4.0*atan(1.0); for(i=1;i<N;i++){ a[i]=pi*i; printf("a[%d]:%f\n",i,a[i]);

}

for(i=2;i<N-1;i++){ b[i]= a[i-1] - 2.0 * a[i] + a[i+1]; printf("b[%d]:%f\n",i,b[i]); }

free(a); /* Free up allocated memory for vector a */ free(b);/* Free up allocated memory for vector b */

return 0; }

Amadou Sowe December 14, 2004 10:55

Re: Arrays in Fortran & C
 
SwF,

I tried pasting the example in but it did not come out right. The print statements are some how missing. I can send it to you if you give me an e-mail to send it to.

Alexander Starostin January 14, 2005 07:43

Re: Arrays in Fortran & C
 
If you've got inevitably accustomed. Always create in C the arrays with plus one extra element. :)


All times are GMT -4. The time now is 15:27.