fortran中函数形式输出数组元素的第二大值
001、
root@ubuntu26:/home/test01/fortran02# cat test01.f90 ## 测试程序 program test01_fortran implicit none integer,allocatable :: ay1(:) integer :: a,i,result do write(*, "(A)", advance="no") "input a: "; read *, a if(a >= 2) then exit end if print *, "a must be >= 2" end do allocate(ay1(a)) do i = 1, a write(*, "(A, I0, A)", advance="no") "input ay1(", i, "): " read *, ay1(i) end do result = second_max(ay1) print *, "result = ", result deallocate(ay1) contains integer function second_max(x) implicit none integer,intent(in) :: x(:) integer :: max1, max2,i if(all(x == x(1))) then error stop "all the elements are same!" end if max1 = maxval(x); max2=minval(x) do i = 1,size(x) if(x(i) < max1 .AND. x(i) > max2) then max2 = x(i) end if end do second_max = max2 end function second_max end program test01_fortran root@ubuntu26:/home/test01/fortran02# gfortran test01.f90 -Wall -O2 -o kkk root@ubuntu26:/home/test01/fortran02# ls kkk test01.f90 root@ubuntu26:/home/test01/fortran02# ./kkk ## 测试 input a: 4 input ay1(1): 33 input ay1(2): 44 input ay1(3): 99 input ay1(4): 33 result = 44 root@ubuntu26:/home/test01/fortran02# ./kkk input a: 0 a must be >= 2 input a: 3 input ay1(1): 33 input ay1(2): 88 input ay1(3): 22 result = 33

。

浙公网安备 33010602011771号