[fortran90] have problem in real array index

In summary, the conversation is about a problem with understanding array indexing in a Fortran code. The person has shared a subroutine called tmz2d and is asking for advice on how to solve an error regarding real array indexing. Another person suggests using int(dpp-0.5) instead of int(dpp)-0.5 to remove the warning and explains that Fortran does not allow real array indexing.
  • #1
s_hy
61
0
hi all...i have problem in understanding in array index. my codes are as below

Code:
subroutine  tmz2d
implicit none


double precision                   :: phi
double precision                   :: cosphi,sinphi
double precision                   :: d,dp,dpp,d1,d2
integer                            :: i,j,m,h
integer                            :: minit,mlast
integer                            :: l,p
integer                            :: iinit,ilast,hlast,pinit,plast
integer                            :: n
integer                            :: linit,llast
integer                            :: jinit,jlast
double precision,dimension(200)    :: Hm,Em
double precision                   :: Einc,Hinc
double precision,dimension(200,200):: Ezinc,Hxinc,Hyinc
double precision, parameter        :: pi = 3.14159265
character(len=20)                  :: filename
 

 minit = 1
 mlast = 100
 linit = 1
 llast = 100
 pinit = 1
 plast = 100

 iinit = 1 
 ilast = 100
 jinit = 1
 jlast = 100


 phi = pi/4.0
 cosphi = cos(phi)
 sinphi = sin(phi)
 print *, 'cosphi=',cosphi,'sinphi=',sinphi


do n = 1,100
   
  write (filename, "('data',I3.3,'.dat')") n
  open (unit=130,file=filename)


 call bg1d
 !print *, 'see me2'

  do m = 2,2*ilast-1
    do p = 2,2*jlast-1
      if (Mod(m,2)/=0) then
	if (Mod(p,2)/=0) then
         d = cosphi*(m - minit) + sinphi*(p - pinit)
         dp = d - int(d)
         Einc = (1-dp)*Em(int(d))+dp*(Em(int(d)+1))
         Ezinc(m,p+2) = Einc
         write(130,*) Ezinc(m,p+2)
        end if
      end if
    end do
  end do

  do m = 2,2*ilast-1
    do p = 2,2*jlast-1
      if (Mod(m,2) == 0) then
	if (Mod(p,2) == 0) then
         d = cosphi*(m - minit) + sinphi*(p - pinit)
         dpp = d+1
         dp = dpp-int(d)
           Hinc = (1-dp)*Hm(int(dpp)-0.5)+dp*Hm(0.5+int(dpp))
           Hxinc(m,p+1) = sinphi*Hinc
           Hyinc(m-1,p+2) = -cosphi*Hinc
        end if
      end if
    end do
  end do

end do !n
print *, 'see me3'


end subroutine tmz2d

and i got this warning when execute

subroutines.f90:167.48:

Hinc = (1-dp)*Hm(int(dpp)-0.5)+dp*Hm(0.5+int(dpp))
1
Warning: Extension: REAL array index at (1)
subroutines.f90:167.28:

Hinc = (1-dp)*Hm(int(dpp)-0.5)+dp*Hm(0.5+int(dpp))
1
Warning: Extension: REAL array index at (1)


###########################################

the problem come from line 167 and i found out that fortran do not allow real index...(int(dpp) -0.5)..can anyone give me an advice how to solve this problem.

thank you in advance
 
Technology news on Phys.org
  • #2
The obvious answer (which should remove the warning, but doesn't have to be correct for your problem) is to use int(dpp-0.5) - right now you are converting dpp to an int, then subtracting (or adding) 0.5 converting it back to a real. Do the subtraction first, conversion to an int later.
 
  • #3
O.k., you found out that Fortran does not allow "real" index; more precisely, it allows a "real argument" but the final index needs to be integer...to that end, it allows a "real argument" which gets automatically typecast into an integer. If "real argument" wasn't allowed, you would get a compiler "error" instead of a "warning".

Say, is there a chest of drawers in your room? Does it have drawer 1.37?...It's the same with arrays.
 
  • #4
thanks Borek, its worked and removed the warning

thanks gsal for the explanation..i am really new in fortran90
 
  • #5


Hello,

It seems like the warning you are receiving is due to using a real number as an index for your array. In Fortran, array indices must be integers. To solve this problem, you can convert your real number to an integer using the INT function.

For example, instead of using int(dpp)-0.5, you can use int(dpp-0.5). This will convert the result of dpp-0.5 to an integer before using it as an index.

You can also use the FLOOR function which will round down the real number to the nearest integer. So instead of int(dpp)-0.5, you can use floor(dpp).

I hope this helps. Happy coding!
 

Related to [fortran90] have problem in real array index

1. What is Fortran90 and how is it different from other versions?

Fortran90 is a programming language used for scientific and numerical computation. It is an updated version of Fortran, with added features such as dynamic memory allocation and recursion. It is also more structured and supports modular programming, making it easier to write and maintain complex programs.

2. What is a real array in Fortran90?

A real array in Fortran90 is a data structure that stores a collection of real numbers in memory. It can be one-dimensional or multi-dimensional, and the size of each dimension can be specified at runtime. Real arrays are commonly used to store and manipulate large amounts of data in scientific and engineering applications.

3. What is a real array index and why am I having problems with it in Fortran90?

A real array index refers to the position of an element in a real array. It is used to access and manipulate specific elements in the array. Users may encounter problems with real array indexing in Fortran90 if they are not familiar with the syntax or if they are trying to access elements outside the bounds of the array.

4. How can I fix my problems with real array indexing in Fortran90?

To fix issues with real array indexing in Fortran90, it is important to carefully review the syntax and make sure the index values are within the bounds of the array. It may also be helpful to use debugging tools or print statements to track the values of the index and make sure they are correct.

5. Are there any resources available to help me learn more about real arrays and indexing in Fortran90?

Yes, there are plenty of online resources and tutorials available to help you learn more about real arrays and indexing in Fortran90. You can also refer to the official Fortran90 documentation or join online forums for discussions and assistance with any specific problems you may encounter.

Similar threads

  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
11
Views
2K
  • Programming and Computer Science
Replies
8
Views
3K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
8
Views
4K
  • Programming and Computer Science
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
  • Atomic and Condensed Matter
Replies
8
Views
4K
Back
Top