This question already has an answer here:
How to check if Fortran array contains value?
(1 answer)
Closed 2 years ago.
Is there a way to check if a string is equal to any of the list together instead of individually explicitly check with a ==? For example,
if(color=='violet' .or. color=='indigo' .or. color=='blue' .or.&
color=='green' .or. color=='yellow' .or. color=='orange' .or. color=='red') then
print *, "It is a rainbow color"
end if
Is there a way compact way to do this? Something like if(color=='violet|indigo|blue|green|yellow|orange|red') ?
You can put the colors into an array and use any.
if (any(color == [character(6) :: "violet","indigo","blue","green","yellow","orange","red"]))
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 4 years ago.
Improve this question
I'm studying for a SAS certification exam, and I came across an unexplained behavior. Note the data step below:
data D;
A+1;
A+1;
A+1;
run;
Question 1: Why this step does not result in error?
Question 2: Why a variable A is created, and its value is 3 and not missing?
Question 3: Why when I change + for - , it results in error?
I have searched about it and i couldn't find nothing, even in SAS documentation
A+1 is sum statement initially A or anything in that form is automatically set to 0 and in your second line of code it becomes 0 +1 = 1 then this value is in A is retained that is A becomes 1 and then when you add 1 in your 3 line of code becomes 2 and then 3. There is nothing of sort is there for -, so it errors when you do A-1, becomes A is not defined, where as in A +1 A is automatically set to 0. Below is the documentation for Sum statement
http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000289454.htm.
Please see in below comment of #longfish explains to do the samething for -1, you need to do A+-1
That is a SUM statement. The syntax is
variable + expression ;
That is why replacing the + with - did not work. It no longer followed the pattern above. If you want to subtract then negate the expression.
variable + - (expression) ;
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
My friend showed me this and I have no idea how it works and what it's called. Can someone explain to me how it loops the way it does? For example:
for(;;){
cout << "loop" << endl;
}
It will just keep looping the string forever. This kind of loop can be used for anything. How does this work?
According tho the language specification, empty condition in for iteration statament is equivalent to true condition.
6.5.3 The for statement
1 The for statement
for ( for-init-statement conditionopt; expressionopt) statement
is equivalent to
{
for-init-statement
while ( condition ) {
statement
expression ;
}
}
...
2 Either or both of the condition and the expression can be omitted. A missing condition makes the implied while clause equivalent to while(true).
So, the loop loops forever. That's all there is to it.
It loops infinitely as no initialization, conditional and increment values are passed in the parameters of the loop. A typical for loop takes parameters as follows: (<initialization>;<conditional>;<increment>)
This post explains it quite well in my opinion. See the answer by spex:
Why can the condition of a for-loop be left empty?
With the structure of the for loop being for(clause; expression-2; expression-3){}, when expression-2 is left out it is replaced with a nonzero constant. This is the part of the loop that determines whether it should keep looping or not. As a nonzero constant evaluates to true, it becomes an infinite loop.
That for loop essentially says the following three things (each separated by the semicolons in your for loop "header?"):
Don't initialize anything.
Don't break from the loop.
Perform no afterthoughts for each loop iteration.
Wikipedia's for loop page actually has a section about this.
As many have pointed out, it is equivalent to while (1).
When is it useful? Wherever you need an infinite loop such as:
A game loop - would be kinda useful to have the game, loop indefinitely until the user decides to quit the game.
OS scheduler - The scheduler needs to loop indefinitely, scheduling processes according to some algorithm until the OS stops
An intepreter - If you have ever programmed in python, you may have come across the interpreter which lets you type some command and then executes it. This is also implemented using a similar infinite loop
In all those examples, the common factor that leads to using an infinite loop is that the terminating condition is not known or the terminating condition is complex (game loops for example)
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.
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.