Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Quick Question Regarding Multi-Dimensional Arrays

Status
Not open for further replies.

Bevo3

Technical User
May 26, 2011
4
US

Hello,

I was wondering if anyone knows whether or not Fortran95 supports multi-dimensional arrays wherein each element contains an array (i.e., an array of arrays). In VB I believe this situation is referred to as a so-called "jagged array."

Thank you for your help!
 
You cannot do that with a single array but this is typically managed using a derived type. Here is for instance a matrix of derived types, each matrix element containing a tensor (array with 3 dimensions) :

Code:
program test
implicit none

type tensor
  real,allocatable :: array(:,:,:)
end type

type(tensor),allocatable :: matrix(:,:)
integer :: n,m,i,j

write(*,'(a)',advance='no') 'matrix size ? '
read(*,*) n,m
allocate(matrix(n,m))
do i=1,n
do j=1,m
  allocate(matrix(i,j)%array(i,j,i+j))
  matrix(i,j)%array=j-i
enddo
enddo

do i=1,n
do j=1,n
  write(*,*) 'i=',i,' j=',j,sum(matrix(i,j)%array)
enddo
enddo

end program

Result :

Code:
[lcoul@localhost test]$ ifort test6.f90
[lcoul@localhost test]$ ./a.out
matrix size ? 2 3
 i=           1  j=           1  0.0000000E+00
 i=           1  j=           2   6.000000    
 i=           2  j=           1  -6.000000    
 i=           2  j=           2  0.0000000E+00

François Jacq
 

Hello Francois,

Thank you very much for taking the time to help me out. I very much appreciate it!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top