
[Sponsors] 
December 11, 2004, 09:44 
Arrays in Fortran & C

#1 
Guest
Posts: n/a

I got accustomed that in Fortran A(i) is ith 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?


December 11, 2004, 20:24 
Re: Arrays in Fortran & C

#2 
Guest
Posts: n/a

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[arraysize] it will give you an exception ....


December 11, 2004, 20:52 
Re: Arrays in Fortran & C

#3 
Guest
Posts: n/a

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.


December 12, 2004, 03:04 
Re: Arrays in Fortran & C

#4 
Guest
Posts: n/a

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


December 12, 2004, 03:37 
Re: Arrays in Fortran & C

#5 
Guest
Posts: n/a

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 ...


December 12, 2004, 04:21 
Re: Arrays in Fortran & C

#6 
Guest
Posts: n/a

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(i1)  2.0*a(i) + a(i+1) end do Please write this piece in C/C++. 

December 13, 2004, 12:06 
Re: Arrays in Fortran & C

#7 
Guest
Posts: n/a

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. 

December 13, 2004, 13:45 
Re: Arrays in Fortran & C

#8 
Guest
Posts: n/a

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. 

December 13, 2004, 15:25 
Re: Arrays in Fortran & C

#9 
Guest
Posts: n/a

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 A1 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. fortranlike. Hope it solves your problems regards DML 

December 14, 2004, 11:01 
Re: Arrays in Fortran & C

#10 
Guest
Posts: n/a

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<N1;i++){ b[i]= a[i1]  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; } 

December 14, 2004, 11:55 
Re: Arrays in Fortran & C

#11 
Guest
Posts: n/a

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 email to send it to. 

January 14, 2005, 08:43 
Re: Arrays in Fortran & C

#12 
Guest
Posts: n/a

If you've got inevitably accustomed. Always create in C the arrays with plus one extra element.


Thread Tools  
Display Modes  


Similar Threads  
Thread  Thread Starter  Forum  Replies  Last Post 
Fortran CompilerCFX12.1  Araz  CFX  12  November 2, 2014 13:46 
Intrinsic Procedure 'ISNAN' in GNU FORTRAN 77  hawk  Main CFD Forum  1  April 12, 2005 22:13 
visual fortran  Monica  Main CFD Forum  1  August 28, 2004 20:45 
Fortran77 or Fortran 90  Swapnil  CFX  2  November 26, 2002 16:16 
Why Favoring Fortran over C/C++?  ZiWei Chiou  Main CFD Forum  35  September 26, 2001 09:34 