fortran中利用函数的形式判断数组的最大值

 

fortran中利用函数的形式判断数组的最大值

 

001、

root@ubuntu26:/home/test01/fortran# cat test01.f90               ## 测试程序
program test01_fortran
    implicit none

    integer :: a,i,result; integer, allocatable :: ay1(:)
    write(*, "(A)", advance="no") "input a: "; read *, a
    if(a <= 0) then; print *, "Error: a must be greater than 0!"; stop; end if
    allocate(ay1(a))
    do i = 1, a
        write(*, "(A, I0, A)", advance="no") "input ay1(", i, "):"; read *, ay1(i)
    end do
    result = array_max(ay1)
    print *, "result = ", result
    deallocate(ay1)
contains

    integer function array_max(x)
        implicit none
        integer,intent(in) :: x(:); integer :: i; array_max = x(1)
        do i = 2,size(x)
            if(x(i) > array_max) then; array_max = x(i); end if
        end do
    end function array_max
end program test01_fortran
root@ubuntu26:/home/test01/fortran# gfortran test01.f90 -Wall -O2 -o kkk     ## 编译
root@ubuntu26:/home/test01/fortran# ls
kkk  test01.f90
root@ubuntu26:/home/test01/fortran# ./kkk         ## 测试
input a: 3
input ay1(1):30
input ay1(2):40
input ay1(3):20
 result =           40

 

image

 。

 

posted @ 2026-11-10 16:17  小鲨鱼2018  阅读(3)  评论(0)    收藏  举报