我的代码有效,除非我尝试使用函数计算数组中一组值的平均值。我省略了程序的大部分,因为不执行这些步骤一切运行良好。谢谢!
在我的主程序中,我有
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
最佳答案
在您的代码中至少有一个语法错误会阻止编译,在这一行
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
到源文件的末尾;和 contains
这两个步骤将允许编译器检查过程接口(interface)。
最后,您的程序中有两个语义错误,一个严重,一个不太严重。
一,你有一个名为
stepsize
的变量。它的使用和描述清楚地表明这实际上是多个步骤。您甚至会提示用户输入步长,但将响应视为若干步。那是错误的。二、你有一个名为
subroutines
的程序.什么 ?!最后,这一次我是认真的,如果您在这里提出更多问题,我建议:
关于function - FORTRAN 95 在使用函数和函数中的预期形式参数列表时出现主程序错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22798707/