Fortran Fortran Error Unexpected data declaration statement

Click For Summary
The Fortran error "Unexpected data declaration statement" arises from incorrect variable type declarations and parameter assignments. The user mistakenly declared integers D and P with an assignment syntax instead of using a proper parameter statement. In Fortran, variables starting with certain letters default to specific types, leading to confusion when not explicitly declared. To resolve the issue, D and P should be declared as integers first and then assigned values in a parameter statement. Correcting these declarations will eliminate the compilation errors related to array dimensions and types.
TamuKevin
Messages
2
Reaction score
0
Fortran Error "Unexpected data declaration statement"

Hi all, I'm writing a program to perform a linear inversion on a set of magnetic data. I'm fairly new to Fortran, but I have coded Fourier and wavelet transformation programs in it. I'm having a problem when trying to declare my variable types. I'm using the "dimension" command as I have in previous programs to define the size of multiple arrays at once. For some reason when it compiles I get the error "Unexpected data declaration statement." The code is more lengthy than this, so I just copied and pasted the parameter declaration into another file, yet the error still exists. Its as if my compiler does not recognize the dimension command, but if I try to compile a previous program that uses it, it does so fine. Also, even if I don't use the dimension command the compiler still finds errors. I'm sure something simple is wrong, I just cannot seem to find it.



[...~/documents/Assignment2] gfortran A2P1test.f
A2P1test.f:26.72:

real, dimension(0:D-1) :: Inp_Data, T, B_obs
1
Error: Unexpected data declaration statement at (1)
A2P1test.f:27.72:

real, dimension(0:P-1) :: Bx, Bz
1
Error: Unexpected data declaration statement at (1)
A2P1test.f:28.72:

real, dimension(0:D-1,0:P-1) :: A
1
Error: Unexpected data declaration statement at (1)

Code:
*This code will perform a linear inversion to a set of magnetic data 
*via the use of 2D prisms

*>>>>>>>>>>>>>>>>>>>>>Declare Variable Definitions<<<<<<<<<<<<<<<<<<	
*pi= pi
*u_0= permeability of free space
*D= Number of magnetic data
*P= Number of parameters (prisms)
*T= Total field anomaly
*B_obs= Raw data in absolute intensity form
*Bx= Bx to be used in filling matrix A
*Bz= Bz to be used in filling matrix A
*A= Forward problem matrix with physics


*>>>>>>>>>>>>>>>>>>>>>>>>>Declare Variable Types<<<<<<<<<<<<<<<<<<<<<	

	parameter(pi=3.141592, u_0=1.25663706e-6)
	integer D=251
	integer P=25
	real, dimension(0:D-1) :: Inp_Data, T, B_obs
	real, dimension(0:P-1) :: Bx, Bz
	real, dimension(0:D-1,0:P-1):: A

*>>>>>>>>>>>>>>>>>>>>>>>Quantify Various Variables<<<<<<<<<<<<<<<<<<

	end

Another odd thing to me is if I define P and D in the parameter statement (as I have done in the past) the compiler treating them as real and not integer.

Code:
A2P1test.f:28.24:

      real, dimension(0:D-1,0:P-1) :: A                                 
                        1
Error: Expression at (1) must be of INTEGER type, found REAL
A2P1test.f:28.30:

      real, dimension(0:D-1,0:P-1) :: A                                 
                              1
Error: Expression at (1) must be of INTEGER type, found REAL
A2P1test.f:28.39:

      real, dimension(0:D-1,0:P-1) :: A                                 
                                       1
Error: The module or main program array 'a' at (1) must have constant shape
A2P1test.f:26.24:

      real, dimension(0:D-1) :: Inp_Data, T, B_obs                      
                        1
Error: Expression at (1) must be of INTEGER type, found REAL
A2P1test.f:26.50:

      real, dimension(0:D-1) :: Inp_Data, T, B_obs                      
                                                  1
Error: The module or main program array 'b_obs' at (1) must have constant shape
A2P1test.f:27.24:

      real, dimension(0:P-1) :: Bx, Bz                                  
                        1
Error: Expression at (1) must be of INTEGER type, found REAL
A2P1test.f:27.34:

      real, dimension(0:P-1) :: Bx, Bz                                  
                                  1
Error: The module or main program array 'bx' at (1) must have constant shape
A2P1test.f:27.24:

      real, dimension(0:P-1) :: Bx, Bz                                  
                        1
Error: Expression at (1) must be of INTEGER type, found REAL
A2P1test.f:27.38:

      real, dimension(0:P-1) :: Bx, Bz                                  
                                      1
Error: The module or main program array 'bz' at (1) must have constant shape
A2P1test.f:26.24:

      real, dimension(0:D-1) :: Inp_Data, T, B_obs                      
                        1
Error: Expression at (1) must be of INTEGER type, found REAL
A2P1test.f:26.40:

      real, dimension(0:D-1) :: Inp_Data, T, B_obs                      
                                        1
Error: The module or main program array 'inp_data' at (1) must have constant shape
A2P1test.f:26.24:

      real, dimension(0:D-1) :: Inp_Data, T, B_obs                      
                        1
Error: Expression at (1) must be of INTEGER type, found REAL
A2P1test.f:26.43:

      real, dimension(0:D-1) :: Inp_Data, T, B_obs                      
                                           1
Error: The module or main program array 't' at (1) must have constant shape

Code:
*>>>>>>>>>>>>>>>>>>>>>>>>>Declare Variable Types<<<<<<<<<<<<<<<<<<<<<	

	parameter(pi=3.141592, u_0=1.25663706e-6, D=251, P=25)
*	integer D=251
*	integer P=25
	real, dimension(0:D-1) :: Inp_Data, T, B_obs
	real, dimension(0:P-1) :: Bx, Bz
	real, dimension(0:D-1,0:P-1) :: A

Thank you for any insight on these problems,

TamuKevin
 
Technology news on Phys.org
TamuKevin said:
Hi all, I'm writing a program to perform a linear inversion on a set of magnetic data. I'm fairly new to Fortran, but I have coded Fourier and wavelet transformation programs in it. I'm having a problem when trying to declare my variable types. I'm using the "dimension" command as I have in previous programs to define the size of multiple arrays at once. For some reason when it compiles I get the error "Unexpected data declaration statement." The code is more lengthy than this, so I just copied and pasted the parameter declaration into another file, yet the error still exists. Its as if my compiler does not recognize the dimension command, but if I try to compile a previous program that uses it, it does so fine. Also, even if I don't use the dimension command the compiler still finds errors. I'm sure something simple is wrong, I just cannot seem to find it.


Code:
*This code will perform a linear inversion to a set of magnetic data 
*via the use of 2D prisms

*>>>>>>>>>>>>>>>>>>>>>Declare Variable Definitions<<<<<<<<<<<<<<<<<<	
*pi= pi
*u_0= permeability of free space
*D= Number of magnetic data
*P= Number of parameters (prisms)
*T= Total field anomaly
*B_obs= Raw data in absolute intensity form
*Bx= Bx to be used in filling matrix A
*Bz= Bz to be used in filling matrix A
*A= Forward problem matrix with physics


*>>>>>>>>>>>>>>>>>>>>>>>>>Declare Variable Types<<<<<<<<<<<<<<<<<<<<<	

	parameter(pi=3.141592, u_0=1.25663706e-6)
	integer D=251
	integer P=25
	real, dimension(0:D-1) :: Inp_Data, T, B_obs
	real, dimension(0:P-1) :: Bx, Bz
	real, dimension(0:D-1,0:P-1):: A

*>>>>>>>>>>>>>>>>>>>>>>>Quantify Various Variables<<<<<<<<<<<<<<<<<<

	end

Another odd thing to me is if I define P and D in the parameter statement (as I have done in the past) the compiler treating them as real and not integer.

Code:
A2P1test.f:28.24:

      real, dimension(0:D-1,0:P-1) :: A                                 
                        1
Error: Expression at (1) must be of INTEGER type, found REAL
A2P1test.f:28.30:

      real, dimension(0:D-1,0:P-1) :: A                                 
                              1
Error: Expression at (1) must be of INTEGER type, found REAL
A2P1test.f:28.39:

      real, dimension(0:D-1,0:P-1) :: A                                 
                                       1
Error: The module or main program array 'a' at (1) must have constant shape
A2P1test.f:26.24:

      real, dimension(0:D-1) :: Inp_Data, T, B_obs                      
                        1
Error: Expression at (1) must be of INTEGER type, found REAL
A2P1test.f:26.50:

      real, dimension(0:D-1) :: Inp_Data, T, B_obs                      
                                                  1
Error: The module or main program array 'b_obs' at (1) must have constant shape
A2P1test.f:27.24:

      real, dimension(0:P-1) :: Bx, Bz                                  
                        1
Error: Expression at (1) must be of INTEGER type, found REAL
A2P1test.f:27.34:

      real, dimension(0:P-1) :: Bx, Bz                                  
                                  1
Error: The module or main program array 'bx' at (1) must have constant shape
A2P1test.f:27.24:

      real, dimension(0:P-1) :: Bx, Bz                                  
                        1
Error: Expression at (1) must be of INTEGER type, found REAL
A2P1test.f:27.38:

      real, dimension(0:P-1) :: Bx, Bz                                  
                                      1
Error: The module or main program array 'bz' at (1) must have constant shape
A2P1test.f:26.24:

      real, dimension(0:D-1) :: Inp_Data, T, B_obs                      
                        1
Error: Expression at (1) must be of INTEGER type, found REAL
A2P1test.f:26.40:

      real, dimension(0:D-1) :: Inp_Data, T, B_obs                      
                                        1
Error: The module or main program array 'inp_data' at (1) must have constant shape
A2P1test.f:26.24:

      real, dimension(0:D-1) :: Inp_Data, T, B_obs                      
                        1
Error: Expression at (1) must be of INTEGER type, found REAL
A2P1test.f:26.43:

      real, dimension(0:D-1) :: Inp_Data, T, B_obs                      
                                           1
Error: The module or main program array 't' at (1) must have constant shape

Code:
*>>>>>>>>>>>>>>>>>>>>>>>>>Declare Variable Types<<<<<<<<<<<<<<<<<<<<<	

	parameter(pi=3.141592, u_0=1.25663706e-6, D=251, P=25)
*	integer D=251
*	integer P=25
	real, dimension(0:D-1) :: Inp_Data, T, B_obs
	real, dimension(0:P-1) :: Bx, Bz
	real, dimension(0:D-1,0:P-1) :: A

Thank you for any insight on these problems,

TamuKevin
By default, variables whose names start with I, J, K, L, M, or N are considered to be integer-type variables, and variables that start with other letters in the alphabet are considered to be real-type variables.

I haven't written any Fortran for many years, so I'm a little rusty on the syntax, but I believe your declarations and parameter statements are causing you problems.

Since P and D would by default be floating point variables, to make them integer parameters, I believe that you need to first declare them as integers, and then give them values in a parameter statement, like so:
Code:
integer P, D
parameter (P = 25, D = 251)

When you fix these two, that should take care of the error involving the arrays that are described as not having a constant shape.
 
Mark, you nailed it. I knew it was something silly. Thank you for your help, I really appreciate it.
 
Mark44 said:
I haven't written any Fortran for many years, so I'm a little rusty on the syntax, but I believe your declarations and parameter statements are causing you problems.

I think the OP got caught by one of Fortran's interesting features ("interesting" as in " strange, and not very useful"...)

All whitespace characters in statements are ignored, except in character strings. So the statement

Code:
integer D=251
is legal, but it is actually an assignment statement giving a value to the variable named
Code:
integerD
... which is not what you wanted, of course.

A similar "gotcha" is a typo like
Code:
DO 10 I = 1.10
which is legal, and sets the real variable
Code:
DO10I
to 1.1, instead of starting a DO loop. :redface:
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 16 ·
Replies
16
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
Replies
3
Views
12K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K