2014-10-12 3 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을 배열로 선언하지 않았다고 추측한다. (이 프로그램에서와 같은 변수가 아닐 것이다 - 이것이 완전한 프로그램이라면 (끝은 없다)) 링커가 함수라고 생각한다. . – francescalus

+0

매우 유감스럽게 생각합니다.이 게시물의 코드를 편집 할 때 서브 루틴의 표준 편차를 추가했습니다. 다시 한번 보실래요? –

+3

* 내가 뭘 잘못하고 있니? * 21 세기에 Fortran 코드를'implicit none '없이 쓰고있다. 내가 여기에 반복적으로 글을 쓰고있는 것을 보았을 때,'암묵적인 사람 없음 '이없는 포트란에서 프로그래밍하는 모든 사람들은 고통을받을 자격이 있습니다. –

답변

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

알렉산더에 주목 하셨 겠지만, OP의 이점을 지적 해 주시길 바랍니다.이 코드에는 여전히 심각한 오류가 있습니다. 'sum2 = sum2 + (array1 (i) -ave) ** 2'가 처음 평가 될 때 오른쪽에있는 sum2는 설정된 값을 가지지 않으므로이를 사용하는 모든 계산은 쓸모 없게 만듭니다. 시작시 Fortran은 기본적으로 변수를 '0'으로 설정하지 않습니다. –

+0

@HighPerformanceMark 글쎄, 나는하지 않았다 ;-) 힌트를 주셔서 감사합니다, 나는 대답에 그것을 추가했습니다! –

+0

고마워, 너의 충고로 모든 코드를 완전히 편집했다. 고마워. –

관련 문제