Logo

Programming-Idioms

History of Idiom 195 > diff from v1 to v2

Edit summary for version 2 by tkoenig:
[Fortran] Included calling side.

Version 1

2019-09-28, 16:24:08

Version 2

2019-09-28, 16:25:11

Idiom #195 Pass a two-dimensional array

Pass an array a of real numbers to the procedure (resp. function) foo. Output the size of the array, and the sum of all its elements when each element is multiplied with the array indices i and j (assuming they start from one).

Idiom #195 Pass a two-dimensional array

Pass an array a of real numbers to the procedure (resp. function) foo. Output the size of the array, and the sum of all its elements when each element is multiplied with the array indices i and j (assuming they start from one).

Code
subroutine foo(a)
  implicit none
  real, dimension(:,:) :: a
  real :: s
  integer :: i,j
  print *,size(a,1), size(a,2)
  s = 0
  do j=1,size(a,2)
    do i=1,size(a,1)
      s = s + a(i,j) * i * j
    end do
  end do
  print *,s
end subroutine foo
Code
subroutine foo(a)
  implicit none
  real, dimension(:,:) :: a
  real :: s
  integer :: i,j
  print *,size(a,1), size(a,2)
  s = 0
  do j=1,size(a,2)
    do i=1,size(a,1)
      s = s + a(i,j) * i * j
    end do
  end do
  print *,s
end subroutine foo
!
  call foo(a)
Comments bubble
This is an assumed-shape array. The compiler automatically passes the bounds.
Comments bubble
This is an assumed-shape array. The compiler automatically passes the bounds.