1

This is my code:

program test
integer, dimension(3,3) :: a =(/1,2,3,4,5,6,7,8,9/)
do i=1,3
write(*,*) (a(i,j),j=1,3)
enddo
end program

I get the following error:

Incompatible ranks 2 and 1 in assignment at (1)

Is the initialization method wrong?

Alexander Vogt
  • 17,879
  • 13
  • 52
  • 68
Yogesh Yadav
  • 404
  • 5
  • 16

1 Answers1

5

Currently you are trying to assign a 1D array (of length 9) to a 2D (3x3) array. You need to reshape the array before the assignment:

program test
  integer, dimension(3,3) :: a = reshape( (/1,2,3,4,5,6,7,8,9/), (/ 3,3/))
  do i=1,3
    write(*,*) (a(i,j),j=1,3)
  enddo
end program
Alexander Vogt
  • 17,879
  • 13
  • 52
  • 68