Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Fortran Max, Min on fortran 90

  1. Mar 4, 2017 #1
    Hi guys, I am playing around with different functions on Fortran 90. I tried to create a simply program that would take five number that I select in a do loop and write to the screen the Max number in that list and the min number in the list. My code is below along with my output:



    what I cannot understand is what is going wrong, my maximum is coming out at 0 and my minimum is coming out to be
    32749, what dose these number represent? why is my output coming out like this? and advice would be great.

    Attached Files:

  2. jcsd
  3. Mar 4, 2017 #2


    User Avatar
    Science Advisor
    Homework Helper
    2017 Award

    Your variable 'high' is not initialized and never updated. Idem 'low'
  4. Mar 4, 2017 #3


    User Avatar

    Staff: Mentor

    Hint: after you have looked at the first number in the list, before looking at the other ones, what are the largest and smallest numbers at that point?
  5. Mar 4, 2017 #4
    Ah thanks guys, after about 30 min or so I managed to re-write the code. As shown below.


    Is my inution on how this program works correct: Once in the do loop and I have selected my list of numbers I the set a varibale highest equal to my max function, now this is how i see the max function working. The n part will read all the values in the list, it will the assign the max/highest value to n1, now n1 has a new value I the set n1 equal to highest and output that to the screen is this correct??
  6. Mar 4, 2017 #5
    ignoring my top code. I have now corrected it. The code look as follows.

  7. Mar 4, 2017 #6


    User Avatar
    Science Advisor
    Homework Helper
    2017 Award

    Not good. n_1 and n_2 are not intialized. If your Fortran environment is decent enough to initialize all variables to zero before beginning, n_2 stays at zero, even if you enter e.g. 10 as the lowest number.

    And if n_1 (like the variable low in your post #1) starts off at a big number, that will remain the maximum (unless you enter a bigger number).
  8. Mar 4, 2017 #7


    User Avatar
    Science Advisor

    @BvU is driving at the key problem in your code. Let me try to point out a more cosmetic issue.

    You write, for instance:
    Code (Text):

    largest = max(n,n_1)
    n1 = largest
    It would be much clearer if you wrote instead:
    Code (Text):

    largest = max(n,largest)
    In an assignment statement, the first thing that happens is that the right hand side is evaluated. After that is done, the resulting value is placed in the variable in the left hand side. There is no problem using the same variable on both right and left hand sides.
  9. Mar 4, 2017 #8


    Staff: Mentor

    Please post your code as text rather than as a screen shot. As a screen shot, we can't copy and paste individual parts of your code -- we have to type it in ourselves.

    Also, use code tags, which preserve indentation you are using.

    Paste your code here inside a [code] and [/code] pair, like so:
    [code=fortran]program max_min <<<-- no spaces between code, =, and the language
    implicit none
    integer :: x, <etc.

    What you type or copy will look like the above, but the browser will render it like this:
    Code (Fortran):
    program max_min
    implicit none
    integer :: x, <etc.
    Also see this topic with more information--https://[URL="http://www.physicsforums.com/threads/read-this-before-posting-code-geshi-syntax-highlgihter.773407/"]www.physicsforums.com/threads/read-this-before-posting-code-geshi-syntax-highlgihter.773407/[/URL] [Broken]
    Last edited by a moderator: May 8, 2017
  10. Mar 5, 2017 #9
    Just like to say thank you, I am new too fortran90 so my code will probs not look great, but I haven taken your comment on board and have correct my code. Sorry about the print screen @Mark44 next code post I will do the above.

    Can anyone recommend some good sites, I have a fortran 90 book which i am doing exercise from, but I feel like I need more intuion. Normally what I do I spend about hour or two writing the code and then trying to see how it works. Which is why my code probably is not birllaint.
  11. Mar 5, 2017 #10


    User Avatar
    Science Advisor
    Homework Helper
    2017 Award

    Intuition is difficult to teach. Experience helps a lot. Perhaps you can increase the slope of your learning process by spending less time for initial coding and adopting a waterfall development approach with very short cycles: develop one small topic at a time and build on that.

    Another thing you need is a good debugging environment, so you can step through the program and check variable values are what you intend them to be.

    Fortran is a very old language and is pretty close to computer architectures. As such it can be very efficient and that's one of the reasons it survived many decades. (Another reason being that there's so much of it around). So some feeling for processes in ALU, registers, memory is very useful. You can get that from books on microprocessor architectures (preferably simple ones... the modern ones are extremely complicated through years of optimization).
  12. Mar 8, 2017 #11


    User Avatar
    Gold Member

    Hi there @Taylor_1989

    I saw you doing Fortran programming in gedit and winced visibly, so I felt as if it might be a good idea to give you two recommendations for development environments to use.

    1) Geany (should be available in your package manager, also check for "geany-plugins" and "geany-themes", my favourite of which is monokai)
    I'd recommend this if you only ever expect to work on small projects without complicated build steps required for all the components. But in those cases, it works wonderfully. Not just syntax highlighting, but also (some) autocompletion, a Symbol (Function/Variable/Type/Etc.) Browser, integrated Terminal pane... and even shortcuts for MAKEing your project (if you've written a Makefile, or cmake, or whatever). No built in debugging tools, and it won't give you tooltips on function argument descriptions (either for Intrinsics or your own), but like I say: if you're working light, this is more than sufficient.

    2) Code::Blocks for Fortran (I don't think there are any "packages" but if you have the necessary dependencies you should be able to run the binaries from its own folder, though I'd recommend putting it in somewhere like /opt)
    If you start making larger projects or find yourself using debugging tools like gdb much more frequently I would recommend this. This has all the features of Geany including the ones it lacks (except an integrated terminal window, and multiple built-in highlighting themes), and allows specifying not only Projects but also their relative interdependencies as members of Workspaces, as long as various Build Targets et cetera. I recommend this over vanilla C::B as some of the functionality goes beyond just the FortranProject plugin.

    I know there are others who use Eclipse Photran but as I've never used it I can't speak for it one way or the other.

    Also as some book recommendations (especially if you're not restricted to F90 or F95): "Modern Fortran Explained" (Metcalfe & Reid), "Modern Fortran in Practice" (Arjen Markus), and "Modern Fortran: Style and Usage" (Norman S. Clerman); and I'd also like to take the opportunity to plug the small ##fortran community over at freenode on IRC.

    @BvU I wanted also to chip in to your comment about Fortran being "close to computer architectures", to say that Fortran has for the most part toed an interesting line between "low level" architecture representation and "high level" mathematical expression convenience, and tried (usually succeeding) to strike a balance between. I suspect that someone with more C experience than I, but also familiar with Fortran, might argue that Fortran is simultaneously "clunkier" and "free-er" than C, declaring it "higher level" but doing so somewhat hesitantly. I'd be interested though to hear others' comments to this regard.
  13. Mar 10, 2017 #12


    Staff: Mentor

    I would assert that C is a lot closer to the hardware than Fortran. Because C was designed to be capable of creating operating systems, many of its operators map directly to assembly language op codes, such as left and right shifts, bitwise AND, OR, XOR, and NOT.

    Some of the features of Fortran, such as the implied do loops in the first two statements below, are quite high level -- each of these lines translates into a much larger number of machine operations.

    Code (Fortran):
    WRITE(UNIT=*, FMT=15) (M, X(M), Y(M), M=1,101,5)
    15    FORMAT(1X, I6, 2F12.3)
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted