回 帖 发 新 帖 刷新版面

主题:fortran 可变大小数组求助

program random
   implicit none
   real(kind=8),allocatable:: x(:),AIN(:,:),CL(:,:,:),f(:,:),maxf(:)
   real(kind=8) upperbound,lowerbound,seed,NRAB,a,upper,lower,afa,sum,replace
   integer number,j,N1,i,k,N3,L,M,count,ii,G
   number=2
   upperbound=6
   lowerbound=-6
   N1=5
   N3=10
   upper=1
   lower=-1
   afa=2
   G=5
   allocate(x(number))
   allocate(AIN(N1,number))
   allocate(maxf(N1))
   call random_seed ()         
   call random_number (seed)  
 
   do i=1,N1
     do L=1,number
       call randomnumber(seed,upperbound,lowerbound,NRAB)
       AIN(i,L)=NRAB
       end do
   end do
   
   allocate(CL(N1,N3,number))
   allocate(f(N1,N3))
   do i=1,N1
      do L=1,number  
        x(L)=AIN(i,L)
      end do
     call equation(x,f(i,1)) 
     do L=1,number
       CL(i,1,L)=AIN(i,L)
     end do
   end do
   call random_seed ()         ! 根据日期和时间随机地提供种
   call random_number (seed)   ! 每次随机数都不一样,不必再提供种子
   do i=1,N1
     do K=2,N3 
        do L=1,number
    x(L)=AIN(i,L)
        end do
10      call randomnumber(seed,upper,lower,NRAB)
        call equation(x,f(i,k))
        a=-f(i,k)/2000
        do L=1,number
    CL(i,k,L)=AIN(i,L)+NRAB**3*afa*exp(a)
           if((CL(i,k,L)>6.0).or.(CL(i,k,L)<-6.0)) then
       go to 10
           end if
         end do
       end do
   end do


   do i=1,N1
     maxf(i)=0
   end do


   do i=1,N1
     do k=1,N3
        do L=1,number
           x(L)=CL(i,k,L)
        end do
        call equation(x,f(i,k))
      end do
   end do

call create(N1,N3,number,CL,f)
       
end


subroutine randomnumber(R,upperbound,lowerbound,NRAB)
    implicit none
    INTEGER K,i,M,L
    real(kind=8) lowerbound,upperbound,S,J,R,NRAB
    S=upperbound-lowerbound+1.0
    K=LOG(S-0.5)/LOG(2.0)+1
    L=1
    DO I=1,K
        L=2*L
    end do
    K=1
    S=4.0*L
10    IF (K.LE.1) THEN
      R=5*R
      M=R/S
      R=R-M*S
      J=lowerbound+R/4.0
      IF (J.LE.upperbound) THEN
        NRAB=J
        K=K+1
      END IF
      GOTO 10
    END IF
    RETURN
END



subroutine equation(x,f)
  implicit none
  real(kind=8) x(2),f
  f=2200-(x(1)*x(1)+x(2)-11)*(x(1)*x(1)+x(2)-11)-(x(1)+x(2)*x(2)-7)*(x(1)+x(2)*x(2)-7)
  return
end

subroutine create(N1,N3,number,CL,f)
   implicit none
   integer number,j,N1,i,k,N3,L,M,count,ii,G
   real(kind=8),allocatable:: adCL(:,:),adf(:),replaceCL(:,:),replacef(:)
   integer,allocatable::pi(:)
   real(kind=8) upperbound,lowerbound,seed,NRAB,a,upper,lower,afa,sum,replace,CL(N1,N3,number),f(N1,N3), x(number),maxf(N1)

   afa=2
 
 do i=1,N1
    M=0
    count=0
    sum=0
    do k=1,N3
      if(f(i,k)>maxf(i)) then
         M=M+1
      end if
     end do


     allocate(adCL(M,number))
     allocate(adf(M))
     allocate(pi(M))     
     allocate(replacef(M*N3)) 
     allocate(replaceCL(M*N3,L))

     do k=1,M
        if(f(i,k)>maxf(i)) then
          do L=1,number
      adCL(k,L)=CL(i,k,L)
          end do
          adf(k)=f(i,k)
        end if
     end do


     do ii=1,M
       do k=1,N3
10       call randomnumber(seed,upper,lower,NRAB)
         a=-adf(ii)/2000
         do L=1,number
     replaceCL((ii-1)*N3+k,L)=adCL(ii,L)+NRAB**3*afa*exp(a)
     if((replaceCL((ii-1)*N3+k,L)>6.0).or.(replaceCL((ii-1)*N3+k,L)<-6.0)) then
              go to 10
           end if 
           x(L)=replaceCL((ii-1)*N3+k,L)
         end do
         call equation(x,replacef((ii-1)*N3+k))
       end do
     end do


     do k=1,N3*M-1
       do ii=k+1,N3*M
          if(replacef(ii)>replacef(k)) then
             replace=replacef(k)
             replacef(k)=replacef(ii)
             replacef(ii)=replace
             do L=1,number
               replace=replaceCL(k,L)
        replaceCL(k,L)=replaceCL(ii,L)
        replaceCL(ii,L)=replace    
      end do
     end if
         end do
       end do 
     
       do k=1,N3
         f(i,k)=replacef(k)
         write(*,*)f(i,k)
         do L=1,number
     CL(i,k,L)=replaceCL(k,L)
            write(*,*)CL(i,k,L)
          end do
       end do

        
      deallocate(adCL)
      deallocate(adf)
      deallocate(pi)
      deallocate(replaceCL)
      deallocate(replacef) 
   end do
   return


end 



运行出现数组越界,请高手指教!谢谢

回复列表 (共9个回复)

沙发

hehe, 还是帖你的报错信息吧. 一般解决不了才帖代码的. 这样谁看啊?

板凳

回1楼
嗯,就是编译什么的都没有错误,执行的时候出现forrtl:severe<161>:Program Exception-array bounds exceeded
谢谢啦

3 楼

create

函数里的

allocate(replaceCL(M*N3,L))

这里的 L 没有赋值就直接使用了,导致分配出问题。该数组越界。

4 楼

还有前面的 M 也是 0,M根据这段代码:

 do i=1,N1
    M=0
    count=0
    sum=0
    do k=1,N3
      if(f(i,k)>maxf(i)) then
         M=M+1
      end if
     end do

应该是做了个统计,可是 maxf 数组也没有给值,所以结果 M = 0

5 楼


回复3楼
那个L我改成了number  但还是出现那个错误   debug的时候发现根本就不给那五adCL adf pi repaceCL replacef 分配空间  求指点!谢谢

6 楼

因为你的 M 是 0

M 算错了。maxf 是没有内容的。

7 楼


回复4楼 
谢谢了  是maxf没有给值  谢谢啦

8 楼


回复6楼 
谢谢  是maxf没有给值

9 楼

!//下面给出了使用动态申请内存功能的实例
!          programe example
pointer (pa,a)       !建立指针与数组的对应关系
integer a(1)       !亦可写成pointer(pa,a(1))
common /p1/pa        !把指针pa定义在公共块p1中

   n =10          !输入需要的数组尺寸
pa=malloc(n)         !申请内存,地址给指针pa
   if(pa.lt.0)then
print*,'内存不足,申请失败'
     stop
   endif
    do I=1,n
     a(I)=I          !用户使用申请到的内存
    enddo
 write(*,*)     a(1:n)
!stop
end
以上程序可以实现动态改变数组,但运行完会提示读了内存,请大家来完善!!!

我来回复

您尚未登录,请登录后再回复。点此登录或注册