Search code examples
arraysfortrandimensions

How to use dimension x(n) in main program in Fortran?


I want to make x(1),x(2),x(3),...,x(n) using dimension in Fortran.

Example :

n=10 
dimension x(n) 
do n=1,10
print*,x(n)
enddo
stop
end

But it's not allowed as far as I know...
And I get this error:

symbol N is not permitted in a specification expression Errors in declarations, no further processing for main

How can I make x(1),x(2),x(3),...,x(n)?


Solution

  • Best is to take a good text book to learn Fortran and look what the possibilities are in the Fortran language.

    There are multiple ways e.g. using an allocatable:

    integer, parameter :: n=10 
    double precision :: x(:)
    integer :: i
    allocate(x(n))
    
    do i=1,n
      x(i) = i
    enddo
    
    do i=1,n
      print*,x(i)
    enddo
    
    end
    

    or using a fixed array:

    integer, parameter :: n=10 
    double precision :: x(n
    integer :: i
    
    
    do i=1,n
      x(i) = i
    enddo
    
    do i=1,n
      print*,x(i)
    enddo
    
    end