2014-10-12 65 views
0

我使用的是Mac,並試圖編譯使用gfortran編譯器計算給定樣本的平均值和標準偏差一個非常簡單的Fortran代碼。代碼如下:Fortran數組名不接受架構X86_64

C This is a fortran program for calculation the average, standard deviation 
C and standard error of a given set of data. 
C Created by Ömer Faruk BODUR - 12 October 2014 

     PROGRAM AvDevErr 
     character(len=1) choise 
     character(len=50) path 
     real ave,x,std 
     real, DIMENSION(30) :: array1 
     integer h 
     write(*,*)'Do you want to enter your data manually ? 
    &Press "y" for yes or "n" for No then submit your choise by 
    &pressing Enter:' 
     read(*,*)choise 
     if(choise.eq.'y') then 
     sum1=0.0 
     icount=0 
     sum2=0.0 
     write(*,*)'Please enter the values to be used when calculating 
    &the average, standard deviation and standard error. When finished, 
    &enter 0 then press Enter:' 

     read(*,*)x 

1 if(x.ne.0) then 
     sum=sum+x 
     icount=icount+1 
     read(*,*)x 
     go to 1 
     endif 
     endif 
     ave=sum/real(icount) 

     if(choise.eq.'n') then 
     sum=0.0 
     icount=0 
     j=1 
     write(*,*)'Enter the name of your data file' 
     read(*,5)path 
5  format(a10) 
     write(*,*)'Enter the number of data to be used in your file: ' 
     read(*,*)ndata 
     open(10,FILE=path,status='old') 
     write(*,*)'The data in your file to be used is below: ' 
     do 14 i=1,ndata 
     read(10,7,END=99)array1(i) 
     write(*,*)array1(i) 
7  format(f4.2) 
     sum=sum+array1(i) 
     icount=icount+1 
14 enddo 
     ave=sum/real(ndata) 
99 endif 
     write(*,*)'The sum of the data is: ',sum 
     write(*,*)'The number of data used is: ',ndata 
     write(*,*)'the average of the data set is= ',ave 

     call stdeviation(ndata,ave) 
     write(*,*)'The standard deviation is: ',std 
     stop 
     end program AvDevErr 

     subroutine stdeviation(ndata,ave) 
     do 19 i=1,ndata 
     sum2=sum2 + (array1(i)-ave)**2 
19 enddo 
     std=sqrt(sum2/real(ndata-1)) 
     return 
     end 

但是,我得到低於引用我的數組名如下錯誤:

Undefined symbols for architecture x86_64: 
    "_array1_", referenced from: 
     _stdeviation_ in ccHYprZn.o 
ld: symbol(s) not found for architecture x86_64 
collect2: error: ld returned 1 exit status 

我在做什麼錯?

+0

有一件事沒有顯示我們'stdeviation'代碼負責的錯誤。我想你還沒有宣佈'array1'作爲數組在那裏(不,這將是相同的變量在這個程序 - 如果這是完整的程序[有沒有'end']),因此鏈接認爲它是一個功能。 – francescalus 2014-10-12 11:37:41

+0

非常抱歉。我通過添加子程序stdeviation編輯了這篇文章中的代碼。你能再看看嗎? – 2014-10-12 11:41:22

+3

*我做錯了什麼?*您正在編寫21世紀的Fortran代碼而沒有「隱含的無」。當我發現自己在這裏重複編寫SO時,任何在Fortran中編程而沒有「隱含」的編程人員都不應該承受所有的痛苦。 – 2014-10-12 11:42:59

回答

2

你需要傳遞array1的子程序(和std,太)!

 ! ... 
     call stdeviation(array1,ndata,ave,std) 
     write(*,*)'The standard deviation is: ',std 
     stop 
     end program AvDevErr 

     subroutine stdeviation(array1,ndata,ave,std) 
     real, dimension(30), intent(in) :: array1 
     integer, intent(in)    :: ndata 
     real, intent(in)    :: ave 
     real, intent(out)    :: std 
     ! ... 

但是,請您自己幫忙,並將您的代碼轉換爲implicit none

例如,子程序看起來是這樣:

你做錯
 subroutine stdeviation(array1,ndata,ave,std) 
     implicit none 
     real, dimension(30), intent(in) :: array1 
     integer, intent(in)    :: ndata 
     real, intent(in)    :: ave 
     real, intent(out)    :: std 

     integer       :: i 
     real       :: sum2 

     ! As High Performance Mark noticed, sum2 has to be initialized first: 
     sum2 = 0. 

     do 19 i=1,ndata 
      sum2=sum2 + (array1(i)-ave)**2 
19  enddo 
     std=sqrt(sum2/real(ndata-1)) 
     return 
     end 
+2

我相信你注意到了Alexander,但值得指出的是OP的好處是,這段代碼仍然包含一個嚴重的錯誤。當'SUM2 = SUM2 +(數組1(ⅰ)-ave)** 2'首先計算'sum2',在右手側,不具有已建立的值,以便使用它的任何計算將產生的垃圾。默認情況下,Fortran在啓動時不會將變量設置爲「0」。 – 2014-10-12 13:14:15

+0

@HighPerformanceMark那麼,我沒有;-)感謝您的提示,我將它添加到答案! – 2014-10-12 13:22:15

+0

謝謝你們,我完全用你的建議編輯了整個代碼。非常感謝。 – 2014-10-12 13:51:03

相關問題