My code works except for when I am trying to use a function to calculate the average of a set of values from an array. I omitted the large part of my program because without doing these steps everything runs fine. Thank you!
Inside my main program I have
averagecalc=average(array(stepsize),stepsize)
WRITE(*,*) averagecalc
Out of my main program I have
FUNCTION average(array(),stepsize)
REAL,INTENT(IN),DIMENSION(stepsize)::array
INTEGER,INTENT(IN)::stepsize
average=SUM(array(stepsize))/stepsize
END FUNCTION
My full program is
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