0

Im new to using fortran and im having some problems with an if statement. Im trying to write something along the lines of if element in array 1 is larger than element in array 2 then let h = some expression, else h = 0. I have coppied my code below

  DO I=1,NPOIN 

  IF ((X(I)*COS(0.0)+0.0*Y(I)*COS(0.0)+0.1)*0.15 .GT. ZF(I)) THEN  
    H%R(I) =0.15*((X(I)*COS(0.0))+(0.0*Y(I)*COS(0.0))+0.1)
  ELSE
    H%R(I) = 1 
  ENDIF

  ENDDO 

The error im getting is:

 IF ((X(I)*COS(0.0)+0.0*Y(I)*COS(0.0)+0.1)*0.15 .GT. ZF(I)) THEN  

Error: Syntax error in IF-expression at (1)

Apologies if this is somethign really obvious but ive tried all sorts to fix it with no joy so far. Any help would be appreciated.

There may be a better way to compare the two arrays but yeah essentially im trying have ZF as my main array, but where H is larger than ZF i want the ZF value to be replaced with the H value. SO essentially i have a new array with the maximum value possible between the two arrays.

Thanks in advance

4
  • The expression compiles fine... I assumed real :: X(NPOIN), Y(NPOIN), ZF(NPOIN), R(NPOIN) and exchanged 'H%R' -> 'R'. Maybe there is something else going wrong. Could you post something compilable? Commented Jul 23, 2014 at 14:44
  • 1
    This expression -- (X(I)*COS(0.0)+0.0*Y(I)*COS(0.0)+0.1) -- occurs twice in your code and can be reduced to X(I)+0.1. I can't see the extra terms as the source of the problem you report but they get in your way, and ours, in trying to understand what is going on. Get rid of them. Commented Jul 23, 2014 at 15:13
  • if using fixed source form be sure not to go past col 72. Commented Jul 23, 2014 at 16:12
  • 1
    Read about the MERGE statement in Fortran 90 and later versions of the language. Commented Jul 23, 2014 at 17:07

1 Answer 1

2

There may be a better way to compare the two arrays but yeah essentially im trying have ZF as my main array, but where H is larger than ZF i want the ZF value to be replaced with the H value. SO essentially i have a new array with the maximum value possible between the two arrays.

If h and zf have the same shape you can simply write:

where (h>zf) 
    zf = h
end where

If they don't have the same shape clarify what you are trying to do.

(Modern) Fortran is an array language (up to a first approximation) so think whole-array operations rather than loops. They may be faster, more likely they're not, but they're quicker to write and easier to understand. If performance isn't good enough then think about a do loop replacement.

Sign up to request clarification or add additional context in comments.

2 Comments

Or even zf=MAX(zf,h)? Performance may well be even worse than the where construct but it could be clearer in intention.
@francescalus: yes, even better your way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.