0

I tried to print the diagonal of a matrix using pointer remapping in fortran. But i received an error. Any idea how to solve it? This is the program

program try

    implicit none

    integer , dimension (3,3), target :: a
    a=reshape([1, 2, 3,4 ,5, 6, 7, 8, 9],[3,3])

    real, pointer, dimension(:, :) :: matrix
    real, pointer, dimension(:) :: diagonal, base
    allocate (base(3*3))

    matrix(1:3, 1:3) => base
    diagonal => base(1:9:4)

    print *, diagonal

end program try

I got this error:

unexpected data declaration statement. at line 9 and 10

(where i declare the pointers)

5
  • 1
    The remapping is an executable statement, it cannot be between the data declaration statements. Commented Mar 11, 2021 at 13:10
  • You can't declare variables after code. So the pointer declaration lines need to be before the line a = reshape.... Commented Mar 11, 2021 at 13:11
  • Ok thank u, now i have modified the code in this: program try implicit none integer , dimension (3,3), target :: a integer, dimension(:, :), pointer :: matrix integer, dimension(:), pointer :: diagonal, base allocate (base(3*3)) matrix(1:3, 1:3) => base diagonal => base(1:9:4) matrix => a a=reshape([1, 2, 3,4 ,5, 6, 7, 8, 9],[3,3]) print *, diagonal end program try but i get this: 17397152 1735357008 1701602145. Why i get these numbers? Commented Mar 11, 2021 at 13:41
  • @DomenicoBianchi please ask a new question rather than asking questions in comments; with no newlines that code is quite hard to read. Commented Mar 11, 2021 at 14:10
  • i did it now. i couldn t do it before cause i have to wait 1.5 hours beetween the questions, thanks Commented Mar 11, 2021 at 16:12

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.