I am very new in Fortran and I am stuck with the following program to find roots using quadratic equation.
It is showing the following error:
d = sqrt(bsq \xE2\x80\x93 ac4)
1
Error: Syntax error in argument list at (1)
program quadratic
implicit none
real :: a, b, c, root1, root2
real :: bsq, ac4, d
print *, 'Please enter the coefficients a, b, and c as real numbers'
read *, a, b, c
bsq = b*b
ac4 = 4*a*c
if ( bsq < ac4) then
d = sqrt(bsq – ac4)
root1 = (-b+d)/(2*a)
root2 = (-b+d)/(2*a)
print *, 'The real roots are ', root1, root2
else if ( root1==root2) then
root1 = root2
print *, 'There is one real root which is ', root1
else
print *, 'There are no real roots'
end if
end program quadratic
You need a minus sign between bsq and ac4, not a dash. Look closely.
Minus sign: -
Dash: –
Related
I am making a physics calculator in Fortran and I have run into a problem. Recently I had some assistance with my code that makes it possible to do, say, a speed calculation, and then go back to the menu to do a time calculation. However, I just added 2 other settings (E= mc2 and current/charge/time), with another menu to choose which one you want to use. My current code (which I will input below) only takes you back to the calculation menu. How would I go about making it so that after you click a button you go back to the main menu?
module kinematics
implicit none
real :: t, d, s
contains
subroutine time_from_distance_and_speed()
print *, 'Input distance in metres'
read *, d
print *, 'Input speed in metres per second'
read *, s
t = d / s
print*, 'Time is ', s
end subroutine
subroutine distance_from_speed_and_time()
print *, 'Input speed in metres per second'
read *, s
print *, 'Input time in seconds'
read *, t
d = s * t
print*, 'Distance is ', d
end subroutine
subroutine speed_from_time_and_distance()
print *, 'Input distance in metres'
read *, d
print *, 'Input time in seconds'
read *, t
s = d / t
print *, 'Speed is ', s
end subroutine
end module
module electronics
implicit none
real :: Q, I, T
contains
subroutine charge_from_current_and_time()
print *, 'Input current in amps'
read *, I
print *, 'Input time in seconds'
read *, T
Q = I * T
print*, 'Charge is ', Q
end subroutine
subroutine current_from_charge_and_time()
print *, 'Input charge in coulombs'
read *, Q
print *, 'Input time in seconds'
read *, T
I = Q/T
print*, 'Current is ', I
end subroutine
subroutine time_from_current_and_charge()
print *, 'Input current in coulombs'
read *, Q
print *, 'Input charge in amps'
read *, I
T = Q/I
print*,'time is ', T
end subroutine
end module
module energy
implicit none
real :: e, m, c
contains
subroutine energy_from_mass_and_lspeed()
print *, 'Warning- speed of light rounded to 300000000'
read *,
print *, 'Input mass in kilograms'
read *, m
c = 300000000
e = m * c * c
print*, 'Energy is ', e
end subroutine
end module
program bike
use kinematics
use electronics
use energy
implicit none
integer :: gg, pp
print *, 'Press 0 for speed, distance, and time. Press 1 for current, charge and time. Press 2 for E= mc^2'
read *, pp
if ( pp == 0 ) then
do while(.true.)
print *, 'Press 1 for speed, 2 for distance, and 3 for time'
read *, gg
if(gg == 1) then
call speed_from_time_and_distance
else if(gg == 2) then
call distance_from_speed_and_time
else if(gg == 3) then
call time_from_distance_and_speed
end if
print *, 'Press 5 to exit the console, or press 4 to do another calculation'
read *, gg
if(gg== 5) then
exit
end if
end do
end if
if ( pp == 1 ) then
do while(.true.)
print *, 'Press 1 for charge, 2 for current, and 3 for time'
read *, gg
if(gg == 1) then
call charge_from_current_and_time
else if(gg == 2) then
call current_from_charge_and_time
else if(gg == 3) then
call time_from_current_and_charge
end if
print *, 'Press 5 to exit the console, or press 4 to do another calculation'
read *, gg
if(gg== 5) then
exit
end if
end do
end if
if ( pp == 2 ) then
do while(.true.)
call energy_from_mass_and_lspeed
print *, 'Press 5 to exit the console, or press 4 to do another calculation'
read *, gg
if(gg== 5) then
exit
end if
end do
end if
end program
This question already has answers here:
Unclassifiable statement and other errors in an IF in Fortran
(1 answer)
"unclassifiable statement" at if statement [duplicate]
(1 answer)
Closed 2 years ago.
I'm just starting out with Fortran and tried to write a basic program but have been getting tons of errors that I don't understand. Please check the following. I've referred to other such questions but to no avail.
program roots
real a,b,c
real disc
real root1,root2
write (*,*) , "Please provide a, b and c"
read (*,*) ,a,b,c
disc = b**2 -4.0*a*c
if disc < 0
print *,"No real roots"
else if disc = 0
print *,"Equal roots"
root1 = -b/2
print *,"root is :" ,root1
else
print *,"2 real roots"
2*root1 = -b + sqrt(b**2 - 4.0*a*c)
2*root2 = -b - sqrt(b**2 - 4.0*a*c)
print *,"root 1 is:" , root1
print *,"root 2 is:" , root2
end roots
errors:
Error: Unclassifiable statement at (1)
Error: Unexpected junk after ELSE statement at (1)
Error: Unexpected ELSE statement at (1)
Error: Non-numeric character in statement label at (1)
Error: Non-numeric character in statement label at (1)
Error: Expecting END PROGRAM statement at (1)
There were mulitple mistakes in your code.
First of all: always use implicit none otherwise undefined variables might have implicit data types which is hard to debug.
Your if .. end if block had multiple mistakes. Compare both codes.
program roots
implicit none
real a,b,c
real disc
real root1,root2
print *, "Please provide a, b and c"
read (*,*) a,b,c
disc = b**2 -4.0*a*c
if (disc < 0) then
print *, "No real roots"
else if (disc == 0) then
print *, "Equal roots"
root1 = -b/2
print *, "root is :", root1
else
print *,"2 real roots"
root1 = (-b + sqrt(b**2 - 4.0*a*c)) / 2
root2 = (-b - sqrt(b**2 - 4.0*a*c)) / 2
print *, "root 1 is:", root1
print *, "root 2 is:", root2
end if
end program
I am very new in Fortran and I am stuck with the following program to find roots using quadratic equation.
It is showing the following error:
d = sqrt(bsq \xE2\x80\x93 ac4)
1
Error: Syntax error in argument list at (1)
program quadratic
implicit none
real :: a, b, c, root1, root2
real :: bsq, ac4, d
print *, 'Please enter the coefficients a, b, and c as real numbers'
read *, a, b, c
bsq = b*b
ac4 = 4*a*c
if ( bsq < ac4) then
d = sqrt(bsq – ac4)
root1 = (-b+d)/(2*a)
root2 = (-b+d)/(2*a)
print *, 'The real roots are ', root1, root2
else if ( root1==root2) then
root1 = root2
print *, 'There is one real root which is ', root1
else
print *, 'There are no real roots'
end if
end program quadratic
You need a minus sign between bsq and ac4, not a dash. Look closely.
Minus sign: -
Dash: –
i would like someone to assist in converting this code to C++
c ----------------------------------------------------------------------
c Calculate pressure based on the generalized Peng-Robinson equation of state.
c for water.
c Variables Used ...
c T ... temperature (K)
c P ... vapor pressure (MPa)
c V ... volume (m^3/kmol)
c ----------------------------------------------------------------------
c Instructor: Nam Sun Wang
c ----------------------------------------------------------------------
common /cblock/T
c Program Header -------------------------------------------------------
print *, 'This program calculates pressure based on the'
print *, 'generalized Peng-Robinson equation of state for water.'
print *, ' '
c Temperature ----------------------------------------------------------
print *, 'Enter temperature (K): '
read *, T
c Generate a table of P at different values of V in 0.5 increments.
print *, ' '
print *, '------------------------'
print *, ' Volume Pressure '
print *, '(m^3/kmol) (MPa) '
print *, '------------------------'
c xx.x123456789012345678 --- ruler
do i=1, 100
V = 0.5*float(i)
print 650, V, P(V)
end do
c Some formats ---------------------------------------------------------
650 format(f7.1, 1p, e18.6)
end
c ----------------------------------------------------------------------
function P(V)
c ----------------------------------------------------------------------
c Calculate pressure based on the generalized Peng-Robinson equation of state.
c for water.
c ----------------------------------------------------------------------
common /cblock/T
c Gas Constant ---------------------------------------------------------
R = 8.314E-3 ! (in MPa m3/kmol K)
c Critical parameters for water ----------------------------------------
Tc = 647.3 ! (critical temperature in K)
Pc = 22.048 ! (critical pressure in MPa)
w = 0.344 ! (acentric factor, dimensionless)
c Peng-Robinson EOS parameters -----------------------------------------
xk = 0.37464 + 1.54226*w - 0.26992*w*w
alpha = ( 1. + xk*(1.-sqrt(T/Tc)) )**2
a = 0.45724*R*R*Tc*Tc*alpha/Pc
b = 0.07780*R*Tc/Pc
P = R*T/(V-b) - a/(V*(V+b)+b*(V-b))
end
Here are Some conversions for you, have a go and then post your results. We can then help you complete it.
a message
print *, '...'
replace with
cout << "..."
a counted loop
do i=1, 100
...
end do
replace with
for(int i = 1; i <= 100; ++i) {
....
}
a comment
.... ! A comment
replace with
....; // a comment
a variable
X = 99.879
replace with
float X = 99.879
a function
function P(V)
.
.
.
P = .... ! the result
replace with
double P(double V){
.
.
.
return ....; // the result
}
I know this is late but I came here looking for an answer and found another solution.
Try the package f2c. I just used it on your code sample and it worked perfectly. Although it is a bit ugly as it links to libraries that emulate Fortran functions like print but you could just use the main logic part and do the I/O yourself.
I'm attempting my first program in Fortran, trying to solve a quadratic equation. I have double and triple checked my code and don't see anything wrong. I keep getting "Invalid character in name at (1)" and "Unclassifiable statement at (1)" at various locations. What is wrong with the code?
! This program solves quadratic equations
! of the form ax^2 + bx + c = 0.
! Record:
! Name: Date: Notes:
! Damon Robles 4/3/10 Original Code
PROGRAM quad_solv
IMPLICIT NONE
! Variables
REAL :: a, b, c
REAL :: discrim, root1, root2,
COMPLEX :: comp1, comp2
CHARACTER(len=1) :: correct
! Prompt user for coefficients.
WRITE(*,*) "This program solves quadratic equations "
WRITE(*,*) "of the form ax^2 + bx + c = 0. "
WRITE(*,*) "Please enter the coefficients a, b, and "
WRITE(*,*) "c, separated by commas:"
READ(*,*) a, b, c
WRITE(*,*) "Is this correct: a = ", a, " b = ", b
WRITE(*,*) " c = ", c, " [Y/N]? "
READ(*,*) correct
IF correct = N STOP
IF correct = Y THEN
! Definition
discrim = b**2 - 4*a*c
! Calculations
IF discrim > 0 THEN
root1 = (-b + sqrt(discrim))/(2*a)
root2 = (-b - sqrt(discrim))/(2*a)
WRITE(*,*) "This equation has two real roots. "
WRITE(*,*) "x1 = ", root1
WRITE(*,*) "x2 = ", root2
IF discrim = 0 THEN
root1 = -b/(2*a)
WRITE(*,*) "This equation has a double root. "
WRITE(*,*) "x1 = ", root1
IF discrim < 0 THEN
comp1 = (-b + sqrt(discrim))/(2*a)
comp2 = (-b - sqrt(discrim))/(2*a)
WRITE(*,*) "x1 = ", comp1
WRITE(*,*) "x2 = ", comp2
PROGRAM END quad_solv
Re the additional question of how to loop back to redo input -- an example program demonstrating loop features of Fortran >= 90. The loop is apparently infinite -- exit controlled by the IF statement exits the loop and makes the loop finite. Also shown is the cycle control, which is used if invalid input is encountered, which would otherwise crash the program. (For example, type "A" as input to the first read, instead of a number.) In this case, the iostat variable acquires a non-zero value, the IF statement activates the cycle, and the rest of the DO loop is skipped, so that the loop cycles anew.
program test_readdata
real :: a, b, c
integer :: ReturnCode
character (len=1) :: correct
ReadData: do
write (*, '( "This program solves quadratic equations of the form ax^2 + bx + c = 0. " )' )
write (*, '( "Please enter the coefficients a, b, and c, separated by commas: " )', advance='no' )
read (*,*, iostat=ReturnCode) a, b, c
if ( ReturnCode /= 0 ) cycle ReadData
write (*,*) "Is this correct: a = ", a, " b = ", b, " c = ", c
write (*, '( "Enter Y or N: " )', advance='no' )
read (*,*, iostat=ReturnCode) correct
if ( ReturnCode /= 0 ) cycle ReadData
if ( correct == 'Y' .or. correct == 'y' ) exit ReadData
end do ReadData
stop
end program test_readdata
My book recommendation: Fortran 95/2003 explained by Metcalf, Reid and Cohen.
The first thing I noticed with your code is the syntax error at the end of your program.
END
should have preceded the word program
Doesn't your editor highlight your syntax errors?
You can get the student version of ELF 90 pretty inexpensively and it's a good place to start. I would then upgrade to Lahey ELF 95 with the Visual Studio and algorithm flow chart generator which color codes the pathways of the passing of the values.
So many errors... It seems you don't know Fortran basics...
With minimal corrections
! This program solves quadratic equations
! of the form ax^2 + bx + c = 0.
! Record:
! Name: Date: Notes:
! Damon Robles 4/3/10 Original Code
PROGRAM quad_solv
IMPLICIT NONE
! Variables
REAL :: a, b, c
REAL :: discrim, root1, root2
COMPLEX :: comp1, comp2
CHARACTER(len=1) :: correct
! Prompt user for coefficients.
WRITE(*,*) "This program solves quadratic equations "
WRITE(*,*) "of the form ax^2 + bx + c = 0. "
WRITE(*,*) "Please enter the coefficients a, b, and "
WRITE(*,*) "c, separated by commas:"
READ(*,*) a, b, c
WRITE(*,*) "Is this correct: a = ", a, " b = ", b
WRITE(*,*) " c = ", c, " [Y/N]? "
READ(*,*) correct
IF (correct == 'N') STOP
IF (correct == 'Y') THEN
! Definition
discrim = b**2 - 4*a*c
! Calculations
IF (discrim > 0) THEN
root1 = (-b + sqrt(discrim))/(2*a)
root2 = (-b - sqrt(discrim))/(2*a)
WRITE(*,*) "This equation has two real roots. "
WRITE(*,*) "x1 = ", root1
WRITE(*,*) "x2 = ", root2
ELSEIF (discrim == 0) THEN
root1 = -b/(2*a)
WRITE(*,*) "This equation has a double root. "
WRITE(*,*) "x1 = ", root1
ELSE
comp1 = (-b + sqrt(discrim))/(2*a)
comp2 = (-b - sqrt(discrim))/(2*a)
WRITE(*,*) "x1 = ", comp1
WRITE(*,*) "x2 = ", comp2
END IF
END IF
END PROGRAM quad_solv
P.S. I didn't check the correctness.
P.P.S. Always indent your code to make it readable and don't use STOP statement. Each program (or subroutine) should have one entry and one exit. The only right place for STOP is exactly before END PROGRAM statement where it is redundant. So don't use STOP at all.
Some changes: correct IF syntax; constants to be real numbers rather than integers, when appropriate:
IF (CORRECT == "N" ) stop
if ( discrim > 0.0 ) then
Apply at additional locations, and you should be very close. Good luck.
It can be a poor algorithm to test a floating point number for having an exact value. What if rounding error makes disrim have the value 1.0E-30, when perfect arithmetic would give the value zero and indicate a double root?