CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > General Forums > Main CFD Forum

Arrays in Fortran & C

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   December 11, 2004, 08:44
Default Arrays in Fortran & C
  #1
SwF
Guest
 
Posts: n/a
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?
  Reply With Quote

Old   December 11, 2004, 19:24
Default Re: Arrays in Fortran & C
  #2
zxaar
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[array-size] it will give you an exception ....
  Reply With Quote

Old   December 11, 2004, 19:52
Default Re: Arrays in Fortran & C
  #3
andy
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.
  Reply With Quote

Old   December 12, 2004, 02:04
Default Re: Arrays in Fortran & C
  #4
SwF
Guest
 
Posts: n/a
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?
  Reply With Quote

Old   December 12, 2004, 02:37
Default Re: Arrays in Fortran & C
  #5
zxaar
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 ...

  Reply With Quote

Old   December 12, 2004, 03:21
Default Re: Arrays in Fortran & C
  #6
SwF
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(i-1) - 2.0*a(i) + a(i+1)

end do


Please write this piece in C/C++.
  Reply With Quote

Old   December 13, 2004, 11:06
Default Re: Arrays in Fortran & C
  #7
andy
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.

  Reply With Quote

Old   December 13, 2004, 12:45
Default Re: Arrays in Fortran & C
  #8
Praveen
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.
  Reply With Quote

Old   December 13, 2004, 14:25
Default Re: Arrays in Fortran & C
  #9
D.M. Lipinski
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 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
  Reply With Quote

Old   December 14, 2004, 10:01
Default Re: Arrays in Fortran & C
  #10
Amadou Sowe
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<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; }
  Reply With Quote

Old   December 14, 2004, 10:55
Default Re: Arrays in Fortran & C
  #11
Amadou Sowe
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 e-mail to send it to.
  Reply With Quote

Old   January 14, 2005, 07:43
Default Re: Arrays in Fortran & C
  #12
Alexander Starostin
Guest
 
Posts: n/a
If you've got inevitably accustomed. Always create in C the arrays with plus one extra element.
  Reply With Quote

Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Fortran Compiler-CFX12.1 Araz CFX 13 March 27, 2017 05:37
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 15:16
Why Favoring Fortran over C/C++? Zi-Wei Chiou Main CFD Forum 35 September 26, 2001 09:34


All times are GMT -4. The time now is 08:13.