Variable dependent formatting in Fortran90 [duplicate] - fortran

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.

Related

simple fortran program to print back what it reads [duplicate]

This question already has answers here:
Using a deferred-length character string to read user input
(4 answers)
Reading a string with spaces in Fortran
(2 answers)
Reading a character string of unknown length
(4 answers)
How to read text files with possibly very long lines in Fortran
(2 answers)
Closed last month.
It's a very simple task, but there are two problems with my current implementation.
I want it to read a whole line. It currently only reads the first few characters if they are separated by space. I think this can be fixed with a custom format, but I don't know what to use.
Fortran needs to know how much to allocate and with my current implementation it can only read 10 characters. I want it to read without a limit. The only way that I know that can fix this right now is asking the user, how many characters they want to be read, but other programming languages can do this without it and I don't want to ask the user for additional things.
program main
implicit none
character(:), allocatable :: name
allocate(character(10) :: name)
read *, name
print *, name
deallocate(name)
end program main

Concentation spacing in Fortran is unexpected [duplicate]

This question already has answers here:
Output formatting: too much whitespace in gfortran
(2 answers)
Integer output formatting with print statement
(3 answers)
How to get rid of unwanted spacing in Fortran's print output?
(1 answer)
Closed 2 years ago.
I'm trying out Fortran, and when I run my code
program main
implicit none
integer :: somevar
somevar = 5
print*,"why is there so much spacing between this and",somevar
end program main
It gives such output:
$ gfortran main.f90 -o main && ./main
why is there so much spacing between this and 5
I found this: How to get rid of unwanted spacing in Fortran's print output?
But I tried to implement the answer, and it didn't work.
How do you fix this spacing problem?

trying to write string in subroutine causing error

Due to some restriction on my assignment, F77 is used.
I am learning to use subroutine but I encounter error when trying to write string out.
PROGRAM test
IMPLICIT NONE
INTEGER a
CHARACTER*20 STR,str1
STR = 'Hello world'
a = 1
WRITE (*,*) a
WRITE (*,10) STR
CALL TEST(str1)
STOP
END
SUBROUTINE test(str2)
CHARACTER*20 str2
str2 = 'testing'
WRITE (*,10) STR2
RETURN
END
When trying to compile this code, it returns that 'Error: missing statement number 10'
Also, I have some other questions:
What does the *20 mean in CHARACTER*20 STR?
Is this the size of the string?
How about 10 in WRITE (*,10) STR? Is this the length of string to be written?
what does (*,*) mean in WRITE (*,*) a
As you can read for example here:
https://www.obliquity.com/computer/fortran/io.html
the second value given to write is an argument for the implicit format keyword, which is the label of a statement within the same program unit, a character expression or array containing the complete format specification, or an asterisk * for list-directed formatting.
Thus if you provide the data directly, you may want to use * there instead.
Otherwise, your program needs to have the label 10 at some line with formatting statement.
And yes, CHARACTER*20 STR means that the variable STR is of length 20, as you can read for instance here: https://www.obliquity.com/computer/fortran/datatype.html
The *20 after CHARACTER specifies the size of the CHARACTER variable (in this case 20 characters). FORTRAN doesn't use null-terminated strings like other languages, instead you have to reserve a specific number of characters. Your actual string can be shorter than the variable, but never longer.
The comma ( , ) in the write statement is used to separate the various arguments. Some versions of FORTRAN allow you to supply 'named' arguments but the default is the first argument is the file code to write to (a '*' implies the standard output). The second argument would be the line number of a FORMAT statement. There can be more arguments, you'd have to look up the specifics for the OPEN statement in your version of FORTRAN.
Some of your WRITE() statements are specifying to use the FORMAT statement found at lable '10'. But your sample doesn't provide any FORMAT statement, so this would be an error.
If you don't want to deal with a FORMAT statement, you can use an asterisk ( * ) as the second argument and then FORTRAN will use a general default format. That is what your first WRITE(,) is doing. It writes to 'stdout' using a general format.

Fortran IF loop [duplicate]

This question already has answers here:
Strange label usage for an IF condition in a DO loop [duplicate]
(2 answers)
Closed 9 years ago.
I have a question about some code I'm looking at written in Fortran. The section of the code I'm confused about is written below.
DO 40 LL=1,N
DO 40 I=1,N-1,2
IF((LL-I)*(LL-I-1)*(LL-I*2)*(LL-I+N-2)) 22,21,22
NODO=LL-I+1
IF((LL.EQ.1) .AND. (I.EQ.N-1)) NODO=NODO+N
I don't understand the condition for the first IF statement. It just looks like numbers are being multiplied together but that number isn't checked against anything. Then 3 line numbers are written after the IF statement. Do anyone know what this IF statement is doing? The last IF statement makes sense as a condition is actually being checked. Thanks.
The line
IF((LL-I)*(LL-I-1)*(LL-I*2)*(LL-I+N-2)) 22,21,22
is an arithmetic if statement, which is certainly obsolescent (the Fortran standard term for deprecated) and may even have been removed in the latest language standard(s). If the condition evaluates to a negative number program control branches to the line with the first label (ie 22), if it evaluates to 0 to the second label (21), if to a positive value to the third label (22). As you see the three labels need not all be different.

DATA declaration in Fortran [duplicate]

This question already has an answer here:
what does DATA TKX/2HKX/ mean in fortran?
(1 answer)
Closed 1 year ago.
Does anyone know the meaning of 4HEND in the following line which comes from an old Fortran code?
DATA XHEND / 4HEND /
4HEND is a Hollerith code to represent the character string "END ". In very old FORTRAN, the variable XHEND might even be a 4-byte integer or real used to hold character data. If implicit typing was in effect in this program, XHEND would have been a real. Another recent question with Hollerith codes: Writing both characters and digits in an array
It replaces assignment
XHEND='END '