除了我尝试使用函数计算数组中一组值的平均值时,我的代码才有效。我省略了程序的大部分内容,因为如果不执行这些步骤,一切运行正常。谢谢!
在我的主程序中我有
averagecalc=average(array(stepsize),stepsize)
WRITE(*,*) averagecalc
我的主程序中有
FUNCTION average(array(),stepsize)
REAL,INTENT(IN),DIMENSION(stepsize)::array
INTEGER,INTENT(IN)::stepsize
average=SUM(array(stepsize))/stepsize
END FUNCTION
我的完整计划是
PROGRAM subroutines IMPLICIT NONE !variables INTEGER:: i,stepsize,j,counts CHARACTER:: choice REAL,EXTERNAL:: functions,average REAL:: a,function1,function2,function3,x,upperbound,lowerbound,averages,sums,averagecalc REAL,ALLOCATABLE::array(:) !formats 101 FORMAT(A) !single text element only 102 FORMAT() ! <description> !-------Variable Definitions-------! ! INTEGER: ! i: used as a counter ! stepsize: the number of steps the user inputs ! j: used as a counter ! counts: used to keep track of steps ! ! CHARACTER: ! choice: ! REAL,EXTERNAL: ! functions: ! average: ! REAL: ! ! ! ! ! ! ! ! ! ! !----------------------------------! !<Begin Coding Here> !Taking in information on which equation and bounds and stepsize CALL section1(lowerbound,upperbound,stepsize,choice) !Calculating equations based on choices and allocating array,writing out array ALLOCATE(array(stepsize)) x=lowerbound counts=0 WRITE(*,101) ' |----------------------|' WRITE(*,101) ' |Step | x | f(x)|' WRITE(*,101) ' |----------------------|' DO i=1,stepsize IF(choice.EQ.'A') THEN array(i)=function1(x) ELSE IF(choice.EQ.'B') THEN array(i)=function2(x) ELSE IF(choice.EQ.'C') THEN array(i)=function3(x) END IF counts=counts+1 WRITE(*,'(I10,F10.3,F10.3)') counts,x,array(i) x=x+(upperbound-lowerbound)/stepsize END DO !Writing the averages averagecalc=average(array(stepsize),stepsize) WRITE(*,*) averagecalc END PROGRAM subroutines !------------------------------------------------SECTION 1------------------------------------------------- SUBROUTINE section1(lowerbound,upperbound,stepsize,choice) IMPLICIT NONE REAL,INTENT(OUT)::lowerbound,upperbound INTEGER,INTENT(OUT):: stepsize CHARACTER,INTENT(OUT):: choice 101 FORMAT(A) !single text element only 102 FORMAT() ! <description> WRITE(*,101) 'Please choose one of the following choices with a capital letter;' WRITE(*,*) WRITE(*,101) 'A) f(x)=x^2+2*x+4' WRITE(*,*) WRITE(*,101) 'B) f(x)=|x+4|' WRITE(*,*) WRITE(*,101) 'C) f(x)=sin(x)+42' READ(*,*) choice IF(choice.EQ.'A') THEN WRITE(*,*) ELSE IF(choice.EQ.'B') THEN WRITE(*,*) ELSE IF(choice.EQ.'C') THEN WRITE(*,*) ELSE STOP 'Please enter either A, B, or C' END IF WRITE(*,101) 'Please enter a lower bound' READ(*,*) lowerbound WRITE(*,101) 'Please enter a upper bound' READ(*,*) upperbound WRITE(*,101) 'Please enter a step size' READ(*,*) stepsize END SUBROUTINE section1 !-------------------------------------------------------functions------------------------------------------ FUNCTION function1(x) REAL,INTENT(IN)::x function1=((x**2)+(2*x)+4) END FUNCTION FUNCTION function2(x) REAL,INTENT(IN)::x function2=ABS(x+4) END FUNCTION FUNCTION function3(x) REAL,INTENT(IN)::x function3=sin(x)+42 END FUNCTION !---------------------------------------average value-------------------------- FUNCTION average(array(),stepsize) REAL,INTENT(IN),DIMENSION(stepsize)::array INTEGER,INTENT(IN)::stepsize average=SUM(array(stepsize))/stepsize END FUNCTION
答案 0 :(得分:1)
在您的代码中至少有一个语法错误会阻止编译,在此行中
FUNCTION average(array(),stepsize)
不允许array
之后的空括号。我个人不会删除它们,我会像
real function average(array)
real, dimension(:), intent(in) :: average
average = sum(array)/size(array)
end function average
在现代Fortran中,将数组的大小作为单独的参数传递是不必要的,而且正如您之前的问题所做的那样,建议您从1979年开始通过虫洞发送问题。
在构建源文件时,编译器无法检查传递给过程的参数是否与过程定义匹配。您可以按照已有的建议将它们放入模块中并使用 - 关联它们,或者:
end program subroutines
移动到源文件的末尾; AND contains
这两个步骤将允许编译器检查过程接口。
最后,你的程序中有两个语义错误,一个严重,一个不那么。
一,你有一个名为stepsize
的变量,其使用和描述清楚地表明这实际上是一些步骤。您甚至提示用户输入步长,但将响应视为多个步骤。那是错的。
二,你有一个名为subroutines
的程序。什么?!
最后,我的意思是这一次,如果你在这里提出进一步的问题,我建议: