Error With REAL Statement In Fortran [closed] - fortran

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have a program that gets a Matrix of Celsius Temperature and Prints The Minimum, Maximum, Averange and Variance of Those Temperatures On The Screen. This Is The Code:
PROGRAM MATRIS
IMPLICIT NONE
INTEGER::M,N,I,J
REAL::AVG,VAR,LEAST,LARGEST,SUM,MIN_A,MAX_A
REAL,DIMENSION(:,:),ALLOCATABLE::A,B
PRINT*,"PLEASE ENTER column,raw"
READ*,M,N
ALLOCATE(A(M,N),B(M,N))
OPEN(10,FILE="C:/TEMP.txt",STATUS="OLD",ACTION="READ")
OPEN(10,FILE="C:/output.txt",STATUS="REPLACE",ACTION="WRITE")
OPEN(10,FILE="C:/output_statistic",STATUS="REPLACE",ACTION="WRITE")
READ*,(10,*)((A(I,J),I=1,M),J=1,N)
DO J=1,N
DO I=1,M
B(I,J)=A(I,J)+273.15
END DO
END DO
WRITE(20,'(2(F6.2,2X))')((A(I,J),I=1,M),J=1,N)
REAL::R1,R2,R3,R4,R5
PRINT*,"PLEASE ENTER YOUR NUMBER"
READ*,R1,R2,R3,R4,R5
CALL REVERSE(R1,R2,R3,R4,R5)
PRINT*,R2,R3,R4,R5
CONTAINS
SUBROUTINE REVERSE(A,D,E,F,G)
IMPLICIT NONE
REAL,INTENT(IN)::A
REAL,INTENT(OUT)::D,E,F,G
SUM=0.
VAR=0.
LARGEST=0.
LEAST=10000.
DO I=1,N
READ*,A
SUM=SUM+A
MAX_A=MAX(LARGEST,A)
LARGEST=MAX_A
MIN_A=MIN(LEAST,A)
LEAST=MIN_A
VAR=SQRT(VAR+(A-AVG)**2)
AVG=SUM/N
D=MAX_A
E=MIN_A
F=VAR
G=AVG
END DO
RETURN
PRINT*,D/E/F/G
END SUBROUTINE REVERSE
END PROGRAM MATRIS
At Line 19 I get this error:
REAL cannot appear after executable statements
And Line 34 I get this Error:
A appears on the left hand side of an assignment yet has the INTENT(IN) attribute
How Can I fix These. And Can You See If There Are Other Errors In My Program? I'm new in Fortran And I need your help. Thanks

You have to declare all your variables at the beginning of a program or subroutine.
You will have to move your
REAL::R1,R2,R3,R4,R5
up to where you declare the other variables.
As for the second question: You have declared A to be INTENT(IN) in your subroutine. That means that the subroutine can't change its value.
But the READ*,A would do just that. So the compiler tells you that this is inconsistent and can't be compiled.
But please, do me and yourself a favour and get a good book about Fortran programming. Or do some online Fortran courses, if you find some.
There are many more errors in your code, and if you try to plough ahead this way, your code will never do what you want it to do.

Related

Fortran Basic Conditional and Loop Code Errors

I'm doing a very basic Fortran tutorial to learn it for grad school and I input the codes for conditionals and loops exactly as they were written in the tutorial but I keep getting the "unexpected end of file" error when I try to compile with gfortran.
This is my conditional code:
if (angle < 90.0) then
print *, 'Angle is acute'
else if (angle < 180.0) then
print *, 'Angle is obtuse'
else
print *, 'Angle is reflex'
end if
This is my loop code:
integer :: i
do i=1,10,2
print *, i ! print odd numbers
end do
Both of them are completed with end statements, so I'm not sure what else it wants. I've only just started teaching myself today, so I'm still just copying codes verbatim from the tutorial and not sure how to troubleshoot anything.
In the tutorial you are following the pieces of code shown are not complete programs. They cannot be compiled as they are.
You will see this frequently, especially in answers on this site.
The code fragments shown miss a lot of context to make clear the parts that are to be taught. That's perhaps a little unfortunate, but Fortran is quite a verbose language, and so there's a trade-off for clarity.
For a complete program you've possibly seen that "all programs must have an end statement to complete them". You may think that the end if and end do statements are suitable for this. They are not: you need an end program statement. The following are two minimal programs:
end
and
end program
(There are also forms with a program statement.)
That is:
if (.true.) then
print *, "Hello, world!"
end if
end program
is compilable if and only if that last line exists.
Further, things like implicit none and variable declarations and definitions would be part of the implied context of example fragments.
If you are just learning and want to try code start with the following skeleton
program test
implicit none
<variable declarations>
<program code>
stop
contains
<function definitions>
end program
The above structure should be enough for most one use programs (write a program to do one thing). The stop above isn't required, it just makes it clear to see where the program instructions actually end.
If you want to use standard numeric types include the iso_fortran_env before the implicit none statement
program test
use, intrinsic :: iso_fortran_env
implicit none
the above will allow you to define integer(int32), integer(int64), real(real32), real(real64), etc.

Variable dependent formatting in Fortran90 [duplicate]

This question already has answers here:
Format string for output dependent on a variable
(4 answers)
Closed 2 years ago.
I am trying to understand "variable dependent formatting", specifically where the repeat count in FORTRAN format statement is a variable instead of a fixed number. I have gone through one of the similar questions here, where this is addressed by defining a format variable. I have tried to implement a similar type of test code but I get a run-time error saying "Fortran runtime error: Missing initial left parenthesis in format". I am attaching the code that I have. Could you please let me know where I am going wrong.
Thank you
program main
implicit none
integer num,i
real,dimension(:),allocatable :: logar
character(len = 100) fmt
print*, "enter any number"
read*,num
allocate(logar(num))
do i = 1,num
logar(i) = log(i/3.14)
end do
open(unit=200,file="num.txt",status="unknown")
write(fmt,'( I4,"(f10.5)" )') num
print*,fmt
write(200,fmt) (logar(i),i=1,num)
end program
Change
write(fmt,'( I4,"(f10.5)" )') num
to
write(fmt,'(a, I4,"(f10.5)",a )') '(', num, ')'
Else, you are missing the parenthesis in fmt. Formats strings are delimited by parentheses.

Replacing .or. statement with if inside a for loop [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am trying to replace an .or. statement with an if statement inside a do loop. But when using the second method, I am not getting the result in first method.
Is there anything I am doing wrong here?
do j =1,115
if (positions(i,1) .eq. j) .or. positions(i,2) .eq. j)then
write(24,201) 47, atoms(j,2),atoms(j,3),atoms(j,4)
endif
enddo
do j =1,115
do m =1,2
if (int(positions(i,m)) .eq. j)then
write(24,201) 47, atoms(j,2),atoms(j,3),atoms(j,4)
endif
enddo
enddo
The two statements you provide are logically different (ignoring the conversion to int which only occurs in the second case):
In the first snippet, you print a line if the first or the second condition is true, resulting in either one line of output or none.
In the second case you perform two check which each could print to unit 24. If both conditions are true, you get two lines of output.
If you want to simplify the first statement you could e.g. use any:
do j=1,115
if ( any(positions(j,1:2) == j) ) then
write(24,201) 47, atoms(j,2),atoms(j,3),atoms(j,4)
endif
enddo
Note that I replaced i in the loop with the loop counter j since there is no indication that i could be defined. This is pure guesswork, of course.

How to write a function prototype? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
my question is write function prototypes for
A function, isAbleToVote, which accepts the age of a potential voter (as a double). The
function should return true if the value of the double is greater than 18 and return false
otherwise.
I wrote bool isAbleToVote(double)
A function, named printPrice, to print a product name and its price to the screen. Both
outputs (i.e., the product name and its price) are passed in as arguments.
I wrote Void printPrice(string product_name, double price)
A function, sizeOf, which accepts a string as its argument. The function should return the length of the string.
I wrote string.length sizeOf(string)
4.A function, named getInt, to print the following message to the screen “Please enter an integer”, and to return the value of the user input.
I wrote cout getInt("please enter an integer")
Am I write? if not what am i doing wrong?
You did well on 1 and 2, those are function prototypes. Though remember, C and C++ function prototypes are followed by a semicolon (to be pedantic)
3 is close, but string.length isn't a return type. Find the return type that best represents the length of a string (hint check the std::string::length() function or strlen() function).
4 is not a prototype. It is a statement. prototypes are simple the signature, without any code body. Read the requirement again and think of the minimal input and output data for the function. Hint there is nothing wrong with a void function that takes no arguments, if you don't need any input or output values.

c++ error: term does not evaluate to a function taking 1 arguments [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm getting the error 'term does not evaluate to a function taking 1 arguments' on the following line in my code:
steerToSpiralRefPoint(m_CentrePos);
But I'm not sure why this is... Is it that the function will only take 1 argument as a parameter, but since the 'm_centrePos' variable holds more than 1 attribute, the function is effectively receiving several parameters?
I have defined 'steerToSpiralRefPoint' with the line:
CDirectOverflyItem steerToSpiralRefPoint = new CDirectOverflyItem::SteerStep(const CHeloData aHeloData);
'm_CentrePos' has been assigned the value 'cCentrePos' at the start of this file ('cCentrePos' is a variable of type 'CCoordinate', which has a number of attributes- latitude, longitude, altitude, etc).
'CDirectOverflyItem's also has a number of attributes- ground speed, wind speed, wind angle, etc.
Can anyone point out to me why I'm getting this error, and how I should correct it?
This expression
steerToSpiralRefPoint(m_CentrePos);
is a postfix expression of a function call. However as it follows from your post steerToSpiralRefPoint is not a function (or function pointer) but a pointer to an object. If you want to assign a value to the pointer then you have to write
steerToSpiralRefPoint = m_CentrePos;
Or if there is an operator function for this type then the code should look as
( *steerToSpiralRefPoint )( m_CentrePos );
And this construction
CDirectOverflyItem steerToSpiralRefPoint = new CDirectOverflyItem::SteerStep(const CHeloData );
is also invalid. You may not use qualifiers before variables in expressions. They may be used only in declarations.
It seems that the issue was that I was trying to pass the wrong data type into the parameter- it was expecting a 'CHeloData', but I was trying to give it a 'CCoordinate'.