Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > passing 3D arrrays from c++ to fortran and viceversa

Reply
Thread Tools

passing 3D arrrays from c++ to fortran and viceversa

 
 
NM
Guest
Posts: n/a
 
      07-28-2005
Hello all,

I am supposed to do some mixed programming with c++ and fortran.
I was succeeful in exchanging the 2D arrays from fortran to c++ and the
other way, but was unable
to that same with the 3D arrays, the values passed are not all the
same. I am also pasting the fortran and c++ codes so that you could
have a look at them.

////C++ Code

#include <math.h>
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;

extern "C" {
void setup_(int *, int *, int *, int *, int *);
void getsetup_( float *,float *);

}


int main()
{

float **testarr,***arr3d;
int rowfor,colfor,i,j,k=1;
int three,two,one,inc;
setup_(&rowfor,&colfor,&three,&two,&one);

// 2D array dynamical allocation
testarr = new float*[colfor];

for (i=0; i < colfor; i++)
testarr[i]= new float[rowfor];


//3d array dynamic allocation
//cout<<"\nThree = "<<three<<"\tTwo = "<<two<<"\tOne= "<<one;
arr3d = new float**[three];

for (i=0;i<three;i++)
arr3d[i]=new float*[two];

for(i=0;i<three;i++)
for(j=0;j<two;j++)
arr3d[i][j]=new float[one];


// Assigning values to the 2D array of c++
inc=1;
for(i=0;i<colfor;i++)
for(j=0;j<rowfor;j++)
testarr[i][j]=inc++;


// Assigning values to the 3D array of c++
inc=1;
for(i=0;i<one;i++)
for(j=0;j<two;j++)
for(k=0;k<three;k++)
arr3d[k][j][i]=inc++;

//Printing the 2D array in c++ before getsetup
cout<<"\nC++ 2D Array IN C++ BEFORE GETSETUP----------";
for(i=0;i<colfor;i++)
{
cout<<endl;
for(j=0;j<rowfor;j++)
cout<<" "<<testarr[i][j];

}

//Printing the 3D array in c++ before getsetup
cout<<"\nC++ 3D Array IN C++ BEFORE GETSETUP----------";
for(i=0;i<three;i++)
{
cout<<"\n";
for(j=0;j<two;j++)
{
cout<<"\n";
for(k=0;k<one;k++)
cout<<" "<<arr3d[i][j][k];
}
}

getsetup_(*testarr,&arr3d[0][0][0]);

//Printing the 2D array in c++ after getsetup
cout<<"\n C++ 2D Array IN C++ AFTER GETSETUP ----------";
for(i=0;i<colfor;i++)
{
cout<<endl;
for(j=0;j<rowfor;j++)
cout<<" "<<testarr[i][j];
}


//Printing the 3D array in c++ after getsetup
cout<<"\nC++ 3D Array IN C++ BEFORE GETSETUP----------";
for(i=0;i<three;i++)
{
cout<<"\n";
for(j=0;j<two;j++)
{
cout<<"\n";
for(k=0;k<one;k++)
cout<<" "<<arr3d[i][j][k];
}
}

cout<<"\n";

return 0;

}


!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Fortran Code

module chek
integer rowfor
integer colfor,onefor,twofor,threefor
real*8, allocatable:: arrfor(:,
real*8, allocatable:: arrfor3d(:,:,
end module chek

subroutine setup(row, col, three, two, one)
use chek
integer row,col,i,j,k,three,two,one
k=1

rowfor = 3
colfor= 10

three = 4
two = 3
one = 2

allocate(arrfor(rowfor,colfor),stat=ierr)
allocate(arrfor3d(one,two,three),stat=ierr)

! Assinging values to 2D array of Fortran
do i=1,rowfor
do j=1,colfor
arrfor(i,j)= 222
end do
end do

! Assinging values to 3D array of Fortran
do i=1,one
do j=1,two
do k=1,three
arrfor3d(i,j,k)= 333
end do
end do
end do

onefor = one
twofor = two
threefor = three
col=colfor
row=rowfor

end subroutine




subroutine getsetup(arr,arr3d)
use chek
real arr(0:rowfor,0:colfor)
real arr3d(0nefor,0:twofor,0:threefor)
integer i,j,k

print *,''
print *,'@@@@@ C++ 2D array IN Fortran i.e IN GETSETUP @@@@@@@@@@@@@@'
do i=0,rowfor-1
print *, ' ',(arr(i,j),j=0,colfor-1)
end do


print*,'@@@@@c++ 3D array in fortran in Getsetup@@@@@@@@@@@@@ '
do i=0,onefor-1
do j=0,twofor-1
print *, ' ',(arr3d(i,j,k),k=0,threefor-1)
enddo
enddo

! Assinging values for the 2D array of c++ with 2d array of fortran
do i=0, rowfor-1
do j=0, colfor-1
arr(i,j)=arrfor(i+1,j+1)
end do
end do


! Assinging values to 3D array of c++ withe the 3D array of fortran
do i=1,onefor
do j=1,twofor
do k=1,threefor
arr3d(i-1,j-1,k-1)=arrfor3d(i,j,k)
enddo
enddo
enddo

end subroutine

////////////////////The Output!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
C++ 2D Array IN C++ BEFORE GETSETUP----------
1 2 3
4 5 6
7 8 9
10 11 12
13 14 15
16 17 18
19 20 21
22 23 24
25 26 27
28 29 30
C++ 3D Array IN C++ BEFORE GETSETUP----------

1 13
5 17
9 21

2 14
6 18
10 22

3 15
7 19
11 23

4 16
8 20

@@@@@ C++ 2D array IN Fortran i.e IN GETSETUP @@@@@@@@@@@@@@
1.000000 4.000000 7.000000 10.00000 13.00000 16.00000 19.00000
22.00000 25.00000 28.00000
2.000000 5.000000 8.000000 11.00000 14.00000 17.00000 20.00000
23.00000 26.00000 29.00000
3.000000 6.000000 9.000000 12.00000 15.00000 18.00000 21.00000
24.00000 27.00000 30.00000
@@@@@c++ 3D array in fortran in Getsetup@@@@@@@@@@@@@
1.000000 2.000000 3.000000 4.000000
0.0000000 0.0000000 0.0000000 0.0000000
0.0000000 0.0000000 0.0000000 0.0000000
13.00000 14.00000 15.00000 16.00000
5.000000 6.000000 7.000000 8.000000
0.0000000 0.0000000 0.0000000 0.0000000
12 24
C++ 2D Array IN C++ AFTER GETSETUP ----------
222 222 222
222 222 222
222 222 222
222 222 222
222 222 222
222 222 222
222 222 222
222 222 222
222 222 222
222 222 222
C++ 3D Array IN C++ BEFORE GETSETUP----------

333 333
333 17
9 21

333 333
333 18
10 22

333 333
333 19
11 23

333 333
333 20
12 24

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!////////////////////////////////////////

It can be observed that the 2D arrays are working fine, i.e the changes
done in c++ are reflected in fortran and vice versa, but it doesnt seem
to happen with 3D, some values are not changed as expected. I hope I
have made my point clear.
Any suggestions would be of great help
Thanks in advance

Regards,
NM

 
Reply With Quote
 
 
 
 
E. Robert Tisdale
Guest
Posts: n/a
 
      07-29-2005
NM wrote:

> I am supposed to do some mixed [language] programming with C++ and Fortran.
> I was successful in exchanging the 2D arrays from Fortran to C++
> and the other way but was unable to the same with the 3D arrays.
> The values passed are not all the same.
> I am also posting the Fortran and C++ codes
> so that you could have a look at them.


Fortran array subscripts appear in order from left to right.
C & C++ array subscripts appear in *reverse* order from right to left.
So Fortran and C arrays appear to be transposes of each other.
For an N-dimensional array:

A[n_N]...[n_2][n_1] ~ A(n_1, n_2, ..., n_N)

Fortran 77/90/95 [sub]programs don't understand pointers to pointers
so you must pass a pointer to the first element of the array

&A[0]...[0][0]

For example:

> cat main.cc

#include <math.h>
#include <iostream>
#include <stdlib.h>
#include <time.h>

extern "C" {
void setup_(int& rowfor, int& colfor,
int& three, int& two, int& one) {
rowfor = 3; colfor = 10;
three = 4; two = 3; one = 2;
}
void getsetup_(float* testarr, float* arr3d) {
// ?
}
}

int main(int argc, char* argv[]) {

int rowfor, colfor, three, two, one;
setup_(rowfor, colfor, three, two, one);

// 2D array dynamic allocation
// testarr[colfor][rowfor] ~ testarr(rowfor, colfor)
float** testarr = new float*[colfor];

testarr[0] = new float[rowfor*colfor];
for (int j = 1; j < colfor; ++j)
testarr[j] = testarr[j-1] + rowfor;

// 3D array dynamic allocation
// arr3d[three][two][one] ~ arr3d(one, two, three)
float*** arr3d = new float**[three];

arr3d[0] = new float*[three*two];
for (int i = 1; i < three; ++i)
arr3d[i] = arr3d[i-1] + two;

arr3d[0][0] = new float[three*two*one];
for (int j = 1; j < two; ++j)
arr3d[0][j] = arr3d[0][j-1] + one;
for (int i = 1; i < three; ++i) {
arr3d[i][0] = arr3d[i-1][0] + two*one;
for (int j = 1; j < two; ++j)
arr3d[i][j] = arr3d[i][j-1] + one;
}

// Assigning values to the 2D array of C++
int inc = 0;
for (int i = 0; i < colfor; ++i)
for (int j = 0; j < rowfor; ++j)
testarr[i][j] = ++inc; // testarr(j, i)

// Assigning values to the 3D array of C++
inc = 0;
for (int i = 0; i < three; ++i)
for (int j = 0; j < two; ++j)
for (int k = 0; k < one; ++k)
arr3d[i][j][k]= ++inc; // arr3d(k, j, i)

//Printing the 2D array in C++ before getsetup
std::cout << "\nC++ 2D Array IN C++ BEFORE GETSETUP --------";
for (int i = 0; i < colfor; ++i) {
std::cout << std::endl;
for (int j = 0; j < rowfor; ++j)
std::cout << " " << testarr[i][j]; // testarr(j, i)
}

//Printing the 3D array in C++ before getsetup
std::cout << "\nC++ 3D Array IN C++ BEFORE GETSETUP --------";
for (int i = 0; i < three; ++i) {
std::cout << "\n";
for (int j = 0; j < two; ++j) {
std::cout << "\n";
for (int k = 0; k < one; ++k)
std::cout << " " << arr3d[i][j][k]; // arr3d(k,j,i)
}
}

getsetup_(&testarr[0][0], &arr3d[0][0][0]);

//Printing the 2D array in C++ after getsetup
std::cout << "\nC++ 2D Array IN C++ AFTER GETSETUP --------";
for (int i = 0; i < colfor; ++i) {
std::cout << std::endl;
for (int j = 0; j < rowfor; ++j)
std::cout << " " << testarr[i][j]; // testarr(j, i)
}

//Printing the 3D array in C++ after getsetup
std::cout << "\nC++ 3D Array IN C++ AFTER GETSETUP --------";
for (int i = 0; i < three; ++i) {
std::cout << "\n";
for (int j = 0; j < two; ++j) {
std::cout << "\n";
for (int k = 0; k < one; ++k)
std::cout << " " << arr3d[i][j][k]; // arr3d(k,j,i)
}
}

std::cout << "\n";

// Deallocate arrays in *reverse* order!
delete [] arr3d[0][0];
delete [] arr3d[0];
delete [] arr3d;
delete [] testarr[0];
delete [] testarr;

return 0;
}

> g++ -Wall -ansi -pedantic -o main main.cc
> ./main


C++ 2D Array IN C++ BEFORE GETSETUP ----------
1 2 3
4 5 6
7 8 9
10 11 12
13 14 15
16 17 18
19 20 21
22 23 24
25 26 27
28 29 30
C++ 3D Array IN C++ BEFORE GETSETUP ----------

1 2
3 4
5 6

7 8
9 10
11 12

13 14
15 16
17 18

19 20
21 22
23 24
C++ 2D Array IN C++ AFTER GETSETUP ----------
1 2 3
4 5 6
7 8 9
10 11 12
13 14 15
16 17 18
19 20 21
22 23 24
25 26 27
28 29 30
C++ 3D Array IN C++ AFTER GETSETUP ----------

1 2
3 4
5 6

7 8
9 10
11 12

13 14
15 16
17 18

19 20
21 22
23 24

Of course, Fortran is off-topic in comp.lang.c++.

> !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Fortran Code
>
> module chek
> integer rowfor
> integer colfor,onefor,twofor,threefor
> real*8, allocatable:: arrfor(:,
> real*8, allocatable:: arrfor3d(:,:,
> end module chek
>
> subroutine setup(row, col, three, two, one)
> use chek
> integer row,col,i,j,k,three,two,one
> k=1
>
> rowfor = 3
> colfor= 10
>
> three = 4
> two = 3
> one = 2
>
> allocate(arrfor(rowfor,colfor),stat=ierr)
> allocate(arrfor3d(one,two,three),stat=ierr)
>
> ! Assinging values to 2D array of Fortran
> do i=1,rowfor
> do j=1,colfor
> arrfor(i,j)= 222
> end do
> end do
>
> ! Assinging values to 3D array of Fortran
> do i=1,one
> do j=1,two
> do k=1,three
> arrfor3d(i,j,k)= 333
> end do
> end do
> end do
>
> onefor = one
> twofor = two
> threefor = three
> col=colfor
> row=rowfor
>
> end subroutine
>
>
>
>
> subroutine getsetup(arr,arr3d)
> use chek
> real arr(0:rowfor,0:colfor)
> real arr3d(0nefor,0:twofor,0:threefor)


Fortran thinks that these arrays
are larger than they actually are.
You need to make up your mind here.
It's either

real arr(0:rowfor-1, 0:colfor-1)
real arr3d(0nefor-1, 0:twofor-1, 0:threefor-1)

or it's

real arr(1:rowfor, 1:colfor)
real arr3d(1nefor, 1:twofor, 1:threefor)


You can simply write

arr = arrfor

and

arr3d = arrfor3d

to copy the arrays.

> integer i,j,k
>
> print *,''
> print *,'@@@@@ C++ 2D array IN Fortran i.e IN GETSETUP @@@@@@@@@@@@@@'
> do i=0,rowfor-1
> print *, ' ',(arr(i,j),j=0,colfor-1)
> end do
>
>
> print*,'@@@@@c++ 3D array in fortran in Getsetup@@@@@@@@@@@@@ '
> do i=0,onefor-1
> do j=0,twofor-1
> print *, ' ',(arr3d(i,j,k),k=0,threefor-1)
> enddo
> enddo
>
> ! Assinging values for the 2D array of c++ with 2d array of fortran
> do i=0, rowfor-1
> do j=0, colfor-1
> arr(i,j)=arrfor(i+1,j+1)
> end do
> end do
>
>
> ! Assinging values to 3D array of c++ withe the 3D array of fortran
> do i=1,onefor
> do j=1,twofor
> do k=1,threefor
> arr3d(i-1,j-1,k-1)=arrfor3d(i,j,k)
> enddo
> enddo
> enddo
>
> end subroutine
>
> ////////////////////The Output!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
> C++ 2D Array IN C++ BEFORE GETSETUP----------
> 1 2 3
> 4 5 6
> 7 8 9
> 10 11 12
> 13 14 15
> 16 17 18
> 19 20 21
> 22 23 24
> 25 26 27
> 28 29 30
> C++ 3D Array IN C++ BEFORE GETSETUP----------
>
> 1 13
> 5 17
> 9 21
>
> 2 14
> 6 18
> 10 22
>
> 3 15
> 7 19
> 11 23
>
> 4 16
> 8 20
>
> @@@@@ C++ 2D array IN Fortran i.e IN GETSETUP @@@@@@@@@@@@@@
> 1.000000 4.000000 7.000000 10.00000 13.00000 16.00000 19.00000
> 22.00000 25.00000 28.00000
> 2.000000 5.000000 8.000000 11.00000 14.00000 17.00000 20.00000
> 23.00000 26.00000 29.00000
> 3.000000 6.000000 9.000000 12.00000 15.00000 18.00000 21.00000
> 24.00000 27.00000 30.00000
> @@@@@c++ 3D array in fortran in Getsetup@@@@@@@@@@@@@
> 1.000000 2.000000 3.000000 4.000000
> 0.0000000 0.0000000 0.0000000 0.0000000
> 0.0000000 0.0000000 0.0000000 0.0000000
> 13.00000 14.00000 15.00000 16.00000
> 5.000000 6.000000 7.000000 8.000000
> 0.0000000 0.0000000 0.0000000 0.0000000
> 12 24
> C++ 2D Array IN C++ AFTER GETSETUP ----------
> 222 222 222
> 222 222 222
> 222 222 222
> 222 222 222
> 222 222 222
> 222 222 222
> 222 222 222
> 222 222 222
> 222 222 222
> 222 222 222
> C++ 3D Array IN C++ BEFORE GETSETUP----------
>
> 333 333
> 333 17
> 9 21
>
> 333 333
> 333 18
> 10 22
>
> 333 333
> 333 19
> 11 23
>
> 333 333
> 333 20
> 12 24
>
> !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!////////////////////////////////////////
>
> It can be observed that the 2D arrays are working fine,
> i.e., the changes done in C++ are reflected in Fortran and vice versa
> but it doesn't seem to happen with 3D.
> Some values are not changed as expected.
> I hope [that] I have made my point clear.
> Any suggestions would be of great help

 
Reply With Quote
 
 
 
 
NM
Guest
Posts: n/a
 
      07-29-2005

E. Robert Tisdale wrote:
> NM wrote:
>
> > I am supposed to do some mixed [language] programming with C++ and Fortran.
> > I was successful in exchanging the 2D arrays from Fortran to C++
> > and the other way but was unable to the same with the 3D arrays.
> > The values passed are not all the same.
> > I am also posting the Fortran and C++ codes
> > so that you could have a look at them.

>
> Fortran array subscripts appear in order from left to right.
> C & C++ array subscripts appear in *reverse* order from right to left.
> So Fortran and C arrays appear to be transposes of each other.
> For an N-dimensional array:
>
> A[n_N]...[n_2][n_1] ~ A(n_1, n_2, ..., n_N)
>
> Fortran 77/90/95 [sub]programs don't understand pointers to pointers
> so you must pass a pointer to the first element of the array
>
> &A[0]...[0][0]
>
> For example:
>
> > cat main.cc

> #include <math.h>
> #include <iostream>
> #include <stdlib.h>
> #include <time.h>
>
> extern "C" {
> void setup_(int& rowfor, int& colfor,
> int& three, int& two, int& one) {
> rowfor = 3; colfor = 10;
> three = 4; two = 3; one = 2;
> }
> void getsetup_(float* testarr, float* arr3d) {
> // ?
> }
> }
>
> int main(int argc, char* argv[]) {
>
> int rowfor, colfor, three, two, one;
> setup_(rowfor, colfor, three, two, one);
>
> // 2D array dynamic allocation
> // testarr[colfor][rowfor] ~ testarr(rowfor, colfor)
> float** testarr = new float*[colfor];
>
> testarr[0] = new float[rowfor*colfor];
> for (int j = 1; j < colfor; ++j)
> testarr[j] = testarr[j-1] + rowfor;
>
> // 3D array dynamic allocation
> // arr3d[three][two][one] ~ arr3d(one, two, three)
> float*** arr3d = new float**[three];
>
> arr3d[0] = new float*[three*two];
> for (int i = 1; i < three; ++i)
> arr3d[i] = arr3d[i-1] + two;
>
> arr3d[0][0] = new float[three*two*one];
> for (int j = 1; j < two; ++j)
> arr3d[0][j] = arr3d[0][j-1] + one;
> for (int i = 1; i < three; ++i) {
> arr3d[i][0] = arr3d[i-1][0] + two*one;
> for (int j = 1; j < two; ++j)
> arr3d[i][j] = arr3d[i][j-1] + one;
> }
>
> // Assigning values to the 2D array of C++
> int inc = 0;
> for (int i = 0; i < colfor; ++i)
> for (int j = 0; j < rowfor; ++j)
> testarr[i][j] = ++inc; // testarr(j, i)
>
> // Assigning values to the 3D array of C++
> inc = 0;
> for (int i = 0; i < three; ++i)
> for (int j = 0; j < two; ++j)
> for (int k = 0; k < one; ++k)
> arr3d[i][j][k]= ++inc; // arr3d(k, j, i)
>
> //Printing the 2D array in C++ before getsetup
> std::cout << "\nC++ 2D Array IN C++ BEFORE GETSETUP --------";
> for (int i = 0; i < colfor; ++i) {
> std::cout << std::endl;
> for (int j = 0; j < rowfor; ++j)
> std::cout << " " << testarr[i][j]; // testarr(j, i)
> }
>
> //Printing the 3D array in C++ before getsetup
> std::cout << "\nC++ 3D Array IN C++ BEFORE GETSETUP --------";
> for (int i = 0; i < three; ++i) {
> std::cout << "\n";
> for (int j = 0; j < two; ++j) {
> std::cout << "\n";
> for (int k = 0; k < one; ++k)
> std::cout << " " << arr3d[i][j][k]; // arr3d(k,j,i)
> }
> }
>
> getsetup_(&testarr[0][0], &arr3d[0][0][0]);
>
> //Printing the 2D array in C++ after getsetup
> std::cout << "\nC++ 2D Array IN C++ AFTER GETSETUP --------";
> for (int i = 0; i < colfor; ++i) {
> std::cout << std::endl;
> for (int j = 0; j < rowfor; ++j)
> std::cout << " " << testarr[i][j]; // testarr(j, i)
> }
>
> //Printing the 3D array in C++ after getsetup
> std::cout << "\nC++ 3D Array IN C++ AFTER GETSETUP --------";
> for (int i = 0; i < three; ++i) {
> std::cout << "\n";
> for (int j = 0; j < two; ++j) {
> std::cout << "\n";
> for (int k = 0; k < one; ++k)
> std::cout << " " << arr3d[i][j][k]; // arr3d(k,j,i)
> }
> }
>
> std::cout << "\n";
>
> // Deallocate arrays in *reverse* order!
> delete [] arr3d[0][0];
> delete [] arr3d[0];
> delete [] arr3d;
> delete [] testarr[0];
> delete [] testarr;
>
> return 0;
> }
>
> > g++ -Wall -ansi -pedantic -o main main.cc
> > ./main

>
> C++ 2D Array IN C++ BEFORE GETSETUP ----------
> 1 2 3
> 4 5 6
> 7 8 9
> 10 11 12
> 13 14 15
> 16 17 18
> 19 20 21
> 22 23 24
> 25 26 27
> 28 29 30
> C++ 3D Array IN C++ BEFORE GETSETUP ----------
>
> 1 2
> 3 4
> 5 6
>
> 7 8
> 9 10
> 11 12
>
> 13 14
> 15 16
> 17 18
>
> 19 20
> 21 22
> 23 24
> C++ 2D Array IN C++ AFTER GETSETUP ----------
> 1 2 3
> 4 5 6
> 7 8 9
> 10 11 12
> 13 14 15
> 16 17 18
> 19 20 21
> 22 23 24
> 25 26 27
> 28 29 30
> C++ 3D Array IN C++ AFTER GETSETUP ----------
>
> 1 2
> 3 4
> 5 6
>
> 7 8
> 9 10
> 11 12
>
> 13 14
> 15 16
> 17 18
>
> 19 20
> 21 22
> 23 24
>
> Of course, Fortran is off-topic in comp.lang.c++.
>
> > !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Fortran Code
> >
> > module chek
> > integer rowfor
> > integer colfor,onefor,twofor,threefor
> > real*8, allocatable:: arrfor(:,
> > real*8, allocatable:: arrfor3d(:,:,
> > end module chek
> >
> > subroutine setup(row, col, three, two, one)
> > use chek
> > integer row,col,i,j,k,three,two,one
> > k=1
> >
> > rowfor = 3
> > colfor= 10
> >
> > three = 4
> > two = 3
> > one = 2
> >
> > allocate(arrfor(rowfor,colfor),stat=ierr)
> > allocate(arrfor3d(one,two,three),stat=ierr)
> >
> > ! Assinging values to 2D array of Fortran
> > do i=1,rowfor
> > do j=1,colfor
> > arrfor(i,j)= 222
> > end do
> > end do
> >
> > ! Assinging values to 3D array of Fortran
> > do i=1,one
> > do j=1,two
> > do k=1,three
> > arrfor3d(i,j,k)= 333
> > end do
> > end do
> > end do
> >
> > onefor = one
> > twofor = two
> > threefor = three
> > col=colfor
> > row=rowfor
> >
> > end subroutine
> >
> >
> >
> >
> > subroutine getsetup(arr,arr3d)
> > use chek
> > real arr(0:rowfor,0:colfor)
> > real arr3d(0nefor,0:twofor,0:threefor)

>
> Fortran thinks that these arrays
> are larger than they actually are.
> You need to make up your mind here.
> It's either
>
> real arr(0:rowfor-1, 0:colfor-1)
> real arr3d(0nefor-1, 0:twofor-1, 0:threefor-1)
>
> or it's
>
> real arr(1:rowfor, 1:colfor)
> real arr3d(1nefor, 1:twofor, 1:threefor)
>
>
> You can simply write
>
> arr = arrfor
>
> and
>
> arr3d = arrfor3d
>
> to copy the arrays.
>
> > integer i,j,k
> >
> > print *,''
> > print *,'@@@@@ C++ 2D array IN Fortran i.e IN GETSETUP @@@@@@@@@@@@@@'
> > do i=0,rowfor-1
> > print *, ' ',(arr(i,j),j=0,colfor-1)
> > end do
> >
> >
> > print*,'@@@@@c++ 3D array in fortran in Getsetup@@@@@@@@@@@@@ '
> > do i=0,onefor-1
> > do j=0,twofor-1
> > print *, ' ',(arr3d(i,j,k),k=0,threefor-1)
> > enddo
> > enddo
> >
> > ! Assinging values for the 2D array of c++ with 2d array of fortran
> > do i=0, rowfor-1
> > do j=0, colfor-1
> > arr(i,j)=arrfor(i+1,j+1)
> > end do
> > end do
> >
> >
> > ! Assinging values to 3D array of c++ withe the 3D array of fortran
> > do i=1,onefor
> > do j=1,twofor
> > do k=1,threefor
> > arr3d(i-1,j-1,k-1)=arrfor3d(i,j,k)
> > enddo
> > enddo
> > enddo
> >
> > end subroutine
> >
> > ////////////////////The Output!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
> > C++ 2D Array IN C++ BEFORE GETSETUP----------
> > 1 2 3
> > 4 5 6
> > 7 8 9
> > 10 11 12
> > 13 14 15
> > 16 17 18
> > 19 20 21
> > 22 23 24
> > 25 26 27
> > 28 29 30
> > C++ 3D Array IN C++ BEFORE GETSETUP----------
> >
> > 1 13
> > 5 17
> > 9 21
> >
> > 2 14
> > 6 18
> > 10 22
> >
> > 3 15
> > 7 19
> > 11 23
> >
> > 4 16
> > 8 20
> >
> > @@@@@ C++ 2D array IN Fortran i.e IN GETSETUP @@@@@@@@@@@@@@
> > 1.000000 4.000000 7.000000 10.00000 13.00000 16.00000 19.00000
> > 22.00000 25.00000 28.00000
> > 2.000000 5.000000 8.000000 11.00000 14.00000 17.00000 20.00000
> > 23.00000 26.00000 29.00000
> > 3.000000 6.000000 9.000000 12.00000 15.00000 18.00000 21.00000
> > 24.00000 27.00000 30.00000
> > @@@@@c++ 3D array in fortran in Getsetup@@@@@@@@@@@@@
> > 1.000000 2.000000 3.000000 4.000000
> > 0.0000000 0.0000000 0.0000000 0.0000000
> > 0.0000000 0.0000000 0.0000000 0.0000000
> > 13.00000 14.00000 15.00000 16.00000
> > 5.000000 6.000000 7.000000 8.000000
> > 0.0000000 0.0000000 0.0000000 0.0000000
> > 12 24
> > C++ 2D Array IN C++ AFTER GETSETUP ----------
> > 222 222 222
> > 222 222 222
> > 222 222 222
> > 222 222 222
> > 222 222 222
> > 222 222 222
> > 222 222 222
> > 222 222 222
> > 222 222 222
> > 222 222 222
> > C++ 3D Array IN C++ BEFORE GETSETUP----------
> >
> > 333 333
> > 333 17
> > 9 21
> >
> > 333 333
> > 333 18
> > 10 22
> >
> > 333 333
> > 333 19
> > 11 23
> >
> > 333 333
> > 333 20
> > 12 24
> >
> > !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!////////////////////////////////////////
> >
> > It can be observed that the 2D arrays are working fine,
> > i.e., the changes done in C++ are reflected in Fortran and vice versa
> > but it doesn't seem to happen with 3D.
> > Some values are not changed as expected.
> > I hope [that] I have made my point clear.
> > Any suggestions would be of great help


I was pleased to get a quick reply and
Thanks a lot Robert, I fixed my problemm.

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing an array from FORTRAN to C then passing it within C andReturning it to FORTRAN deadpickle C Programming 1 11-07-2010 02:38 PM
Many Fortran code! How to call Fortran code from C/C++? Luna Moon C++ 9 09-04-2007 03:49 PM
Using Ptr of derived class to point to base class and viceversa Bhan C++ 10 09-19-2005 02:23 PM
desktops see laptop, but can not connect to it and viceversa =?Utf-8?B?b25hc3Npcw==?= Wireless Networking 1 11-01-2004 12:10 AM
Calling tcl from python and viceversa Carlos P. Python 4 07-20-2003 03:15 AM



Advertisments