Fortran 90: Find Max & Min Values

  • Fortran
  • Thread starter Taylor_1989
  • Start date
  • Tags
    Fortran Max
In summary: GFortran (a commercial product, but it has a lot of features for programming in Fortran, as well as other languages).
  • #1
Taylor_1989
402
14
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:

upload_2017-3-4_12-10-59.png


upload_2017-3-4_12-13-7.png


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.
 

Attachments

  • upload_2017-3-4_12-9-59.png
    upload_2017-3-4_12-9-59.png
    16.5 KB · Views: 1,158
Technology news on Phys.org
  • #2
Your variable 'high' is not initialized and never updated. Idem 'low'
 
  • #3
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?
 
  • #4
Ah thanks guys, after about 30 min or so I managed to re-write the code. As shown below.

upload_2017-3-4_13-28-50.png


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??
 
  • #5
ignoring my top code. I have now corrected it. The code look as follows.

upload_2017-3-4_15-21-59.png
 
  • #6
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).
 
  • #7
@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:
largest = max(n,n_1)
n1 = largest

It would be much clearer if you wrote instead:
Code:
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.
 
  • #8
Taylor_1989 said:
ignoring my top code. I have now corrected it. The code look as follows.

View attachment 114076
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.
[/code]

What you type or copy will look like the above, but the browser will render it like this:
Fortran:
program max_min
implicit none
integer :: x, <etc.

Also see this topic with more information--https://[URL="https://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]
 
Last edited by a moderator:
  • #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.
 
  • #10
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).
 
  • #11
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 into 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.
 
  • #12
aphirst said:
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 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.

Fortran:
READ(UNIT=*, FMT=*) (ARRAY(I), I= IMIN, IMAX)
WRITE(UNIT=*, FMT=15) (M, X(M), Y(M), M=1,101,5)
15    FORMAT(1X, I6, 2F12.3)
 

1. How can I find the maximum value in an array using Fortran 90?

To find the maximum value in an array using Fortran 90, you can use the MAXVAL function. This function takes an array as its argument and returns the maximum value in that array.

2. Can I find the maximum value in a multi-dimensional array using Fortran 90?

Yes, you can use the MAXVAL function to find the maximum value in a multi-dimensional array. You just need to specify the dimension along which you want to find the maximum value.

3. How do I find the minimum value in an array using Fortran 90?

To find the minimum value in an array using Fortran 90, you can use the MINVAL function. This function takes an array as its argument and returns the minimum value in that array.

4. Can I find the minimum value in a multi-dimensional array using Fortran 90?

Yes, you can use the MINVAL function to find the minimum value in a multi-dimensional array. You just need to specify the dimension along which you want to find the minimum value.

5. Is it possible to find both the maximum and minimum values in an array using Fortran 90?

Yes, you can use both the MAXVAL and MINVAL functions to find both the maximum and minimum values in an array. You can either use them separately or use an array assignment statement to store both values in separate variables.

Similar threads

  • Programming and Computer Science
2
Replies
37
Views
3K
  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
7
Views
3K
  • Programming and Computer Science
Replies
29
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
13
Views
2K
  • Programming and Computer Science
Replies
19
Views
5K
  • Programming and Computer Science
Replies
8
Views
3K
  • Programming and Computer Science
Replies
12
Views
10K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
Back
Top