INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

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

RE: Simpson 1/3 Program Errors

There are a couple of problems.

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

(OP)
So I made the changes but I am still having outputs of zero.
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

There still several problems with the code...you really need to be more careful and consistent.

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

(OP)
So I declared n as an integer in the function and declared the h division to produce an actual value but it is still producing a 0 value.
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

(OP)
This second post was an accident please ignore it.

Thank you

RE: Simpson 1/3 Program Errors

did you see my post, though? I did not think it was going to get through as it seemed the site went into some kind of maintainance mode...

RE: Simpson 1/3 Program Errors

(OP)
Yes I did I corrected the declaration portion. Then made sure the variables matched from the main program to the function and it now puts out a value. It is not correct so it is a math issue which I will hunt down. Thank you for the help it is greatly appreciated!!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close