## Simpson 1/3 Program Errors

## Simpson 1/3 Program Errors

(OP)

I am very new to fortran and I have been assigned to write a Simpsons 1/3 function. Below is what I have come up with but it is outputting the area as 0. All help is greatly appreciated.

Program test

IMPLICIT NONE

REAL :: n, simp13, Simpson13

INTEGER, DIMENSION(10) :: V

INTEGER :: h

V = (/4,5,6,7,8,9,10,11,12,13/)

h = 1

n = 9

Simp13=Simpson13(V,h,n)

WRITE (*,*) 'The area is:', Simp13

Pause

End Program test

!*******************************************

REAL FUNCTION Simpson13 (V,h,n)

IMPLICIT NONE

INTEGER, INTENT(IN) :: V(n)

INTEGER, INTENT(IN) :: h

Real :: i,Vodd,Veven,n

Vodd=0

Veven=0

DO i=1,((n-2)/2)

Vodd=Vodd+V(2*i-1)

END DO

Do i=1,((n-2)/2)

Veven=Veven+V(2*i)

END DO

Simpson13=(h/3)*(V(1)+4*Vodd+2*Veven+V(n))

END FUNCTION Simpson13

Program test

IMPLICIT NONE

REAL :: n, simp13, Simpson13

INTEGER, DIMENSION(10) :: V

INTEGER :: h

V = (/4,5,6,7,8,9,10,11,12,13/)

h = 1

n = 9

Simp13=Simpson13(V,h,n)

WRITE (*,*) 'The area is:', Simp13

Pause

End Program test

!*******************************************

REAL FUNCTION Simpson13 (V,h,n)

IMPLICIT NONE

INTEGER, INTENT(IN) :: V(n)

INTEGER, INTENT(IN) :: h

Real :: i,Vodd,Veven,n

Vodd=0

Veven=0

DO i=1,((n-2)/2)

Vodd=Vodd+V(2*i-1)

END DO

Do i=1,((n-2)/2)

Veven=Veven+V(2*i)

END DO

Simpson13=(h/3)*(V(1)+4*Vodd+2*Veven+V(n))

END FUNCTION Simpson13

## RE: Simpson 1/3 Program Errors

First, the index to the array needs to be an integer variable; so, you need to declare n an integer.

Second, you need to know about integral division...in the various programming languages that I know, 1/2=0, 2/3=0, 4/7=0 5/2=2 7/3=2, 9/2=4 ....get the picture?

The last line in your listing shows: h/3, but h=1 and, so, h/3=0.

So, you need to need to be aware of typecasting and operand promotion and that kind of things when you put together your mathematical expressions.

## RE: Simpson 1/3 Program Errors

Anymore help?

This is what I have now...

Program test

IMPLICIT NONE

REAL :: Simpson13

INTEGER, DIMENSION(7) :: V

INTEGER :: h,n,simp13

V = (/1.000,1.167,1.333,1.500,1.667,1.833,2.000/)

h = 0.1667

n = 6.0

Simp13=Simpson13(V,h,n)

WRITE (*,*) 'The area is:', Simp13

Pause

End Program test

!*******************************************

REAL FUNCTION Simpson13(V,h,n)

IMPLICIT NONE

INTEGER, INTENT(IN) :: V,h,n

REAL :: Vodd, Veven, i

Vodd=0

Veven=0

DO i=1,((n-2)/2)

Vodd=Vodd+V(2*i)

END DO

Do i=1,((n-2)/2)

Veven=Veven+V(2*i+1)

END DO

Simpson13=(h/3.)*(V(1)+4*Vodd+2*Veven+V(n+1))

END FUNCTION Simpson13

## RE: Simpson 1/3 Program Errors

First, you have declared array V to an integer, yet, you assign real numbers to it.

You have declared h to an integer, but you assign not only a real to it, but a number less than 1, which will then be truncated to zero!

You have declared simp13 to be an integer, yet, you expect it to catch the returning value from a real function (truncation will happen).

You really need to make sure you declare all these variables correctly no only in the main program but also in the function and make sure the list of arguments matches the parameters you are going to pass...for example, V in the function is not declared as an array! (and should be real, too).

Inside the function, the loop variable i needs to be an integer variable, too.

## RE: Simpson 1/3 Program Errors

Anymore advice?

This is what I have now...

Program test

IMPLICIT NONE

REAL :: Simpson13

INTEGER, DIMENSION(7) :: V

INTEGER :: h,n,simp13

V = (/1.000,1.167,1.333,1.500,1.667,1.833,2.000/)

h = 0.1667

n = 6.0

Simp13=Simpson13(V,h,n)

WRITE (*,*) 'The area is:', Simp13

Pause

End Program test

!*******************************************

REAL FUNCTION Simpson13(V,h,n)

IMPLICIT NONE

INTEGER, INTENT(IN) :: h,n

INTEGER, DIMENSION(n+1), INTENT(IN) :: V

REAL :: Vodd, Veven, i

Vodd=0

Veven=0

DO i=1,((n-2)/2)

Vodd=Vodd+V(2*i)

END DO

Do i=1,((n-2)/2)

Veven=Veven+V(2*i+1)

END DO

Simpson13=(h/3.)*(V(1)+4*Vodd+2*Veven+V(n+1))

END FUNCTION Simpson13

## RE: Simpson 1/3 Program Errors

Thank you

## RE: Simpson 1/3 Program Errors

## RE: Simpson 1/3 Program Errors