2015-06-22 32 views
-1

我正在编写一个代码,将一个圆圈离散化,然后在用户指定的时间间隔内返回哪些点。使用变量x,y和theta,它将y和theta的值按照它们应该写入文件info.dat的方式写入,但写入x不管我做什么都是零。写到points.dat也没有问题。顺便说一下所有变量进行了适当从一开始就为两种分配,目标,指针定义等需要帮助调试写入Fortran中的文件

open(unit=2, file="points.DAT") 

print*, 'Please enter the reference angle of the arc in degrees, number of points on the arc, and radius of the arc.' 
read(*,*) a, n, r 
a = a * pi/180 


allocate(x(1:n),y(1:n),theta(1:n)) 
do i = 1,n 
    theta(i:i) = a*(i-1)/n 
    x(i:i) = r * cos(theta(i:i)) 
    y(i:i) = r * sin(theta(i:i)) 
    xcoord(i:i) => x(i:i) 
    ycoord(i:i) => y(i:i) 
    angle(i:i) => theta(i:i)   
write(2,*) 'x',i,'=',x(i:i),'y',i,'=',y(i:i), 'theta', i,'=', theta(i:i)  
end do 
deallocate(x,y,theta) 
    close(2) 

    open(unit=3, file="info.DAT") 
print*, 'Please specify the interval of interest between 0 and 360 degrees' 
read(*,*) b, c 
b = b * pi/180 
c = c * pi/180 

do i = 1, n 
    if (any(b <= angle(i:i) .and. angle(i:i) <= c)) then 
     write(3,*) 'x', i, '=', xcoord(i:i), 'y', i, '=', ycoord(i:i), 'theta', i, '=', angle(i:i) 
    end if 
end do 
close(3) 
+0

它可能会帮助任何人试图帮助你在你的代码中显示变量的声明,实际上是发布一个MCVE - http://stackoverflow.com/help/mcve –

+0

@高级别1 dim( 1)数组与(标量)元素 用于计算(至少这里是简单用法)和I/O,但不用于指针关联(显然使用不是F95的边界重新映射),也不传递参数(这里不使用),因为在这些情况下,排名必须匹配(以及类型和种类)。 –

+0

始终使用标签[tag:fortran],只有在必要时添加版本才能区分您的问题是否具体。例如,您不能使用Fortran 2008,而只能使用Fortran 90。 –

回答

0

虽然你没有表现出来xcoord ycoord angle必须声明为POINTER。您将它们设置为依次指向x() y() theta()的每个单元素片, 使它们指向第N个元素,然后释放基础数组,以便指针现在不明确(指向释放的内存)。

如果您在编译器(或可能运行时)上有调试选项并使用它们,那么当指针关联设置为(n:n)时,它们应该明确检测到对1..n-1 的访问,由于释放,检测到even(n)无效。看起来好运以前使用的内存 由x已被其他东西破坏,但运气不好ytheta仍然有其价值。