program main !*****************************************************************************80 ! !! recursive_sub_test() demonstrates the use of recursive subroutines. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 08 June 2007 ! ! Author: ! ! John Burkardt ! implicit none integer, parameter :: n = 10 integer n_copy integer i4vec(n) call timestamp ( ) write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'RECURSIVE_SUB_TEST():' write ( *, '(a)' ) ' FORTRAN90 version.' write ( *, '(a)' ) ' ' write ( *, '(a)' ) ' Demonstrate recursive subroutine definitions.' write ( *, '(a)' ) ' ' write ( *, '(a)' ) ' We sort a vector by recursion.' write ( *, '(a)' ) ' Our recursive step finds the largest element and moves' write ( *, '(a)' ) ' it to the end.' call i4vec_uniform_ab ( n, 0, 10, i4vec ) call i4vec_print ( n, i4vec, ' Unsorted vector:' ) n_copy = n if ( 1 < n ) then call i4vec_max_back ( n_copy, i4vec ) end if call i4vec_print ( n, i4vec, ' Sorted vector:' ) ! ! Terminate. ! write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'RECURSIVE_SUB_TEST:' write ( *, '(a)' ) ' Normal end of execution.' write ( *, '(a)' ) ' ' call timestamp ( ) stop end recursive subroutine i4vec_max_back ( n, i4vec ) !*****************************************************************************80 ! !! I4VEC_MAX_BACK sorts a vector recursively. ! ! Discussion: ! ! On each call, this subroutine finds the maximum entry in the ! vector, moves it to position N, reduces N by 1, and calls itself ! again. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 27 November 2006 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer N, the size of the input vector. ! ! Input/output, integer I4VEC(N), the vector being sorted. ! implicit none integer n integer big integer i4vec(n) integer loc(1) ! ! Find maximum, move to the end. ! if ( 2 <= n ) then loc = maxloc ( i4vec(1:n) ) big = i4vec(loc(1)) i4vec(loc(1):n-1) = i4vec(loc(1)+1:n) i4vec(n) = big end if ! ! Sort the vector of length N-1. ! if ( 3 <= n ) then call i4vec_max_back ( n-1, i4vec ) end if return end subroutine i4vec_print ( n, a, title ) !*****************************************************************************80 ! !! I4VEC_PRINT prints an I4VEC. ! ! Discussion: ! ! An I4VEC is a vector of integer values. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 28 November 2000 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer N, the number of components of the vector. ! ! Input, integer A(N), the vector to be printed. ! ! Input, character ( len = * ) TITLE, a title to be printed first. ! TITLE may be blank. ! implicit none integer n integer a(n) integer i character ( len = * ) title if ( 0 < len_trim ( title ) ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) trim ( title ) end if write ( *, '(a)' ) ' ' do i = 1, n write ( *, '(2x,i8,2x,i12)' ) i, a(i) end do return end subroutine i4vec_uniform_ab ( n, a, b, x ) !*****************************************************************************80 ! !! i4vec_uniform_ab() returns a scaled pseudorandom I4VEC. ! ! Discussion: ! ! An I4VEC is a vector of I4's. ! ! The pseudorandom numbers should be scaled to be uniformly distributed ! between A and B. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 15 April 2025 ! ! Author: ! ! John Burkardt ! ! Input: ! ! integer n: the dimension of the vector. ! ! integer a, b: the limits of the interval. ! ! Output: ! ! integer x(n): a vector of numbers between A and B. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer n integer a integer b integer i real ( kind = rk ) r integer x(n) do i = 1, n call random_number ( harvest = r ) x(i) = a + int ( ( b + 1 - a ) * r ) end do return end subroutine timestamp ( ) !*****************************************************************************80 ! !! timestamp() prints the current YMDHMS date as a time stamp. ! ! Example: ! ! 31 May 2001 9:45:54.872 AM ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 06 August 2005 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! None ! implicit none character ( len = 8 ) ampm integer d integer h integer m integer mm character ( len = 9 ), parameter, dimension(12) :: month = (/ & 'January ', 'February ', 'March ', 'April ', & 'May ', 'June ', 'July ', 'August ', & 'September', 'October ', 'November ', 'December ' /) integer n integer s integer values(8) integer y call date_and_time ( values = values ) y = values(1) m = values(2) d = values(3) h = values(5) n = values(6) s = values(7) mm = values(8) if ( h < 12 ) then ampm = 'AM' else if ( h == 12 ) then if ( n == 0 .and. s == 0 ) then ampm = 'Noon' else ampm = 'PM' end if else h = h - 12 if ( h < 12 ) then ampm = 'PM' else if ( h == 12 ) then if ( n == 0 .and. s == 0 ) then ampm = 'Midnight' else ampm = 'AM' end if end if end if write ( *, '(i2,1x,a,1x,i4,2x,i2,a1,i2.2,a1,i2.2,a1,i3.3,1x,a)' ) & d, trim ( month(m) ), y, h, ':', n, ':', s, '.', mm, trim ( ampm ) return end