max function
1. Definition
This folowinig function gives the largest number among the
two given numbers. Among two floats x and y; it outputs x if it is
larger than y, otherwise, it will put y.
2. Example in C language:
/*
Here is the fnction to output the maximum number among two v
any numbers .
*/
#include <stdio.h>
int main()
{
float value1, value2;
//Declaring the defined function maximum:
float maximum(float value1, float value2) ;
printf("Enter two floats -> \n");
scanf ("%f""%f", &value1, &value2);
/*Displaying the two entered values*/
printf("The read values are: %6.2f and %6.2f\n", value1, value2);
/* Displaying the maximum */
printf("The max number is: %6.2f\n", maximum(value1, value2));
return 0;
}
/*Defininig the function maximum*/
float maximum ( float x1, float x2 )
{
if ( x1 < x2 )
return x2 ;
else
return x1 ;
}
3. Other example in Fortran90 language
!This program fetches integers from a file "question.txt", call
!a subroutine max_and_min(array, min, max) and outputs the min and the max
!(resuls). In the subroutine, an array of dimension 6 A(6) is declared, a
!loop "do" crosses the array making tests to compare an element of the array
!to its predecessor. The maximum and minimum are then pointed at the end.
!1. main program:
!----------------
!
PROGRAM max_min_array
IMPLICIT NONE
integer:: A(6), i, maximum, minimum
OPEN(unit=15, File="question.txt")
Read(15,*) (A(i),i=1,6)
call max_and_min(A,maximum,minimum)
write(*,*) "the maximum is", maximum
write(*,*) "the minimum is", minimum
close(15)
end program midterm
!
!
!2. Subroutine:
!---------------
subroutine max_and_min(A,maximum,minimum)
implicit none
integer:: A(6), i, maximum,minimum
maximum=A(1)
minimum=A(1)
DO i=2,6
if (A(i)>maximum) THEN
maximum = A(i)
end if
if (A(i)<minimum) THEN
minimum = A(i)
end if
end do
end subroutine max_min_array
!------------------END----
!3. example:
!------------
!The file "question.txt" contains:
1
34
5
26
3
78
9
10
The program compiled with SciTE gives:
>g77 -x f77 -ffree-form -W -Wall "max_min_array.f90" -o "max_min_array.exe"
>Exit code: 0
Executed gives:
C:\Fortran90>max_min_array
the maximum is 78
the minimum is 1
C:\Fortran90>