Fortran output has lots of errors that I don't understand

In summary, the conversation discusses the challenges of writing a Fortran 95 program to integrate x^2 from 0 to 1. The program also includes a function definition for x2(x) and a discussion about the need for the pi constant. The errors in the program are caused by missing statements, such as an END or END PROGRAM statement, which can confuse the compiler. The solution to the errors is to add an appropriate END statement after the PRINT statement to indicate the end of the main program.
  • #1
ngendler
20
0
I am trying to write a program that in Fortran 95 will integrate x^2 from 0 to 1. Easy right?? I'm getting errors, though, that I really don't understand. Here is my code:

program montecarlo2
implicit none
real :: fmax,iseed,srand,xmax,x2,x,y
!why do we need pi??
integer :: icount,nmax,ans0
iseed = 329048234
call srand(iseed)

nmax = 10000

!integrate x^2 from zero to one
icount = 0
xmax = 1
fmax = x2(xmax)
ans0 = 0.
do i=1,nmax
x = rand()
y = rand()
f = x2(x)
if(y.le.f/fmax) then
icount = icount+1
end if
ans0 = ans0 + x2(y)
end do
print *,dfloat(icount)/dfloat(nmax),ans0/dfloat(nmax)

function x2(x)
implicit none
x2 = x**2
end function x2


and here is the output:

montecarlo2.f95:26:

function x2(x)
1
Error: Unclassifiable statement at (1)
montecarlo2.f95:27.13:

implicit none
1
Error: Unexpected IMPLICIT NONE statement at (1)
montecarlo2.f95:28.2:

x2 = x**2
1
Error: 'x2' at (1) is not a variable
montecarlo2.f95:29.3:

end function x2
1
Error: Expecting END DO statement at (1)
Error: Unexpected end of file in 'montecarlo2.f95'


please help!
 
Technology news on Phys.org
  • #2
You probably should have and END or END PROGRAM statement after the PRINT statement. You have not instructed the compiler that there are no more statements in the main program 'montecarlo2'. When the compiler sees a function definition, then it becomes confused.
 
Last edited:
  • #3
Thank you, @SteamKing! It worked!
 

Related to Fortran output has lots of errors that I don't understand

1. Why am I getting errors when I try to output my Fortran code?

Errors in Fortran output can occur for a variety of reasons, such as syntax errors, uninitialized variables, or incorrect data types. It is important to carefully check your code for any mistakes and to make sure your input data is correct.

2. How can I debug my Fortran code to fix these errors?

One way to debug your Fortran code is to use a debugger tool, such as GDB or Intel Parallel Studio. These tools allow you to step through your code line by line and see the values of variables at each step, helping you identify where the error is occurring. Additionally, using print statements or writing to a log file can also help pinpoint the source of the error.

3. Is there a way to turn off error messages in Fortran?

Yes, you can use the "quiet" option in your compiler to suppress error messages. However, it is not recommended to do this as it may hide important information that can help you identify and fix errors in your code.

4. I am new to Fortran, where can I find resources to help me understand the errors in my output?

There are many online resources available for Fortran, including forums, tutorials, and documentation. You can also refer to your compiler's manual or seek help from experienced Fortran programmers to understand and troubleshoot the errors in your code.

5. How can I prevent errors in my Fortran output in the future?

To prevent errors in your Fortran output, it is important to thoroughly test and debug your code before running it. Make sure to use proper coding practices and conventions, as well as carefully check your input data. Regularly reviewing and optimizing your code can also help prevent errors in the long run.

Similar threads

  • Programming and Computer Science
Replies
4
Views
25K
  • Programming and Computer Science
Replies
8
Views
3K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
5
Views
14K
  • Programming and Computer Science
Replies
9
Views
4K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
13
Views
7K
  • Programming and Computer Science
Replies
1
Views
9K
Back
Top