So, you've written an impressive FORTRAN program and you're eager to print out its maximum value. Unfortunately, FORTRAN doesn't natively support the 'MAX' function like some other programming languages. But don't worry, there's a simple way around this. Let's dive into how you can print the maximum value from an array in FORTRAN.
First, let's understand why we don't have a built-in MAX function. FORTRAN 77, an early version of FORTRAN, didn't include it to keep the language simple and efficient. However, you can achieve the same result using just a few lines of code. Let's explore how.
Finding the Maximum Value in FORTRAN
To find the maximum value in a FORTRAN program, you'll need to initialize a variable with the first element of the array, iterate through the rest of the array, and replace this initializer whenever you find a greater value. Here's how you can do it.
Let's say you have an array called 'numbers' with dimensions (10). Here's a simple procedure to find the maximum value:
Initializing the Variable
Start by declaring a variable, say 'max_value', and initialize it with the first element of the array. This will serve as your temporary 'max' value.
```FORTRAN REAL :: max_value REAL :: numbers(10) max_value = numbers(1) ```
Iterating Through the Array
Next, use a DO loop to iterate through the remaining elements of the array. For each element, compare its value with 'max_value'. If the element's value is greater than 'max_value', update 'max_value'.
```FORTRAN DO i = 2, 10 IF (numbers(i) > max_value) THEN max_value = numbers(i) ENDIF ENDDO ```
Printing the Maximum Value
After your loop finishes, 'max_value' will hold the maximum value from your array. Now, you can print it out using the PRINT statement.
```FORTRAN PRINT *, "The maximum value is:", max_value ```
Sorting and Using the Last Element as Maximum
Alternatively, you could sort the array and use the last element as the maximum value. This method is easier but can be less efficient for large arrays, depending on the sorting algorithm used. Here's how you can sort the array:
```FORTRAN CALL SORT(numbers) ```
Then, print the last element of the sorted array:
```FORTRAN PRINT *, "The maximum value is:", numbers(10) ```
It's critical to understand that using SORT is a good alternative when you only need the maximum value, but it might be overkill if you need to use the sorted array later in your program. The first method is generally more efficient in such cases.
So, there you have it! You can now print the maximum value from an array in FORTRAN with ease. Happy coding!












