2014-10-12 73 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