user3705273
user3705273

Reputation: 335

What is error in this fortran program?

I am writing following fortran 90 program and compiling with GNU Fortran Compiler. However, I am getting some errors that I can't resolve. Can someone explain me where I am going wrong?

PROGRAM FILE1
IMPLICIT NONE

INTEGER:: status,a,b,c,i,j

a=5
b=6
c=1

REAL,ALLOCATABLE,DIMENSION (:,:) :: arr1

ALLOCATE (arr1(a,b))

DO i=1,a,1
    DO j=1,b,1
        arr1(i,j)=c
        c=c+1
    END DO
END DO

DO i=1,a,1
    DO j=1,b,1
        WRITE (*,100,ADVANCE=NO) arr1(i,j)
        100 FORMAT (1X,I4.1)
    END DO
        WRITE (*,110)
        110 FORMAT ('',/)
END DO

DEALLOCATE(arr1)

END PROGRAM

I got several errors, e.g. in statement REAL,ALLOCATABLE,DIMENSION (:,:) :: arr1 Error: Unexpected data declaration statement at (1). I checked syntax from several sources, but no luck. There is error in statement WRITE (*,100,ADVANCE=NO) arr1(i,j) too. Can someone comment?

Upvotes: 0

Views: 774

Answers (1)

Bahman
Bahman

Reputation: 261

The posted code has a couple of issues:

  1. you need to define all your variables before you execute any other statement.

    a=5
    b=6
    c=1
    

should come after your last variable definition:

REAL,ALLOCATABLE,DIMENSION (:,:) :: arr1
  1. You need to quote NO in the WRITE statement:

    WRITE (*,100,ADVANCE="NO") arr1(i,j)
    
  2. Since you are writing a REAL value, you need to give a floating point format in such as f or g

    100 FORMAT (1X,f7.3)
    

The working code would be:

PROGRAM FILE1
IMPLICIT NONE

INTEGER:: status,a,b,c,i,j

REAL,ALLOCATABLE,DIMENSION (:,:) :: arr1

a=5
b=6
c=1

ALLOCATE (arr1(a,b))

DO i=1,a,1
    DO j=1,b,1
        arr1(i,j)=c
        c=c+1
    END DO
END DO

DO i=1,a,1
    DO j=1,b,1
        WRITE (*,100,ADVANCE="NO") arr1(i,j)
        100 FORMAT (1X,f7.3)
    END DO
        WRITE (*,110)
        110 FORMAT ('',/)
END DO

DEALLOCATE(arr1)

END PROGRAM

Upvotes: 3

Related Questions