2015-06-22 46 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仍然有其價值。