Skip to main content
Engineering LibreTexts

16.4: Example One

  • Page ID
    54369
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    In this example, we will write a simple program to read two times from the user, time one and time two, and calculate the sum of the two times. For this example, the time will consist of hour, minutes, seconds in 24-hour format. For this exercise, the hours may exceed 23 when the times are summed. The program should declare the appropriate variables using a derived data type, use a subroutine to read a time (which should be called twice), and another subroutine to calculate the sum of the times. The subroutine to read the times must perform appropriate error checking. The main should display both the times and the final time sum.

    Understand the Problem

    The main is expected to define the appropriate derived data type for time, declare some variables of that type and call the subroutines. The first subroutine will read a time from the user which will consist of hour, minutes, and seconds in 24-hour format. This subroutine will be called twice. The second subroutine will add the times together and provide a result.

    The first subroutine to read a time from the user is expected to perform error checking on the data entered by the user. Specifically, this requires that hours range from 0 to 23, minutes range from 0 to 59, and seconds range from 0 to 59. Values outside these ranges, 60 seconds for example, are not valid. For this simple example, we will re-prompt for incorrect data entry (until correct data is provided).

    The second subroutine will add the two times and must ensure that the correct ranges for seconds and minutes are maintained. When adding the two times, it is possible to add the seconds, minutes, and hours. However, if the sum of the two seconds values exceeds 60, the seconds must be adjusted and the minutes must be updated accordingly (add one extra minute). This applies to the minutes as well. However, when added in this exercise, the sum of the final time may exceed 23 hours.

    For example, given time one as 14 hours, 47 minutes and 22 seconds (i.e., 14:47:22) and time two as 18 hours, 22 minutes, and 50 seconds, (i.e., 18:22:50), the total time would be 33 hours, 10 minutes and 12 seconds (i.e., 33:10:12).

    Create the Algorithm

    For this example, there are three parts; the main, the read time subroutine, and the time summation subroutine. The basic steps for the main include:

    ! define derived data type for time
    !     must include → hours, minutes, seconds
    ! declare variables, including → time1, time2, and timesum
    ! display initial header
    ! call subroutine to read time1
    ! call subroutine to read time2
    ! call subroutine to add times
    ! display results
    

    The basic steps for the read time subroutine include:

    ! subroutine header and appropriate declarations
    ! loop
    !     prompt for time
    !     read time (hours, minutes, seconds)
    !     check time entered
    !     → if [ hours(0-23), minutes(0-59), seconds (0-59) ] exit
    !     display error message
    ! end loop
    

    The basic steps for the time summation subroutine include:

    ! subroutine header and appropriate declarations
    ! add the seconds
    ! add the minutes
    ! add the hours
    ! if seconds > 59, then
    !     subtract 60 from seconds
    !     add 1 to minutes
    ! if minutes > 59, then
    !     subtract 60 from minutes
    !     add 1 to hours
    

    For convenience, the steps are written as program comments.

    Implement the Program

    Based on the algorithm, the below program could be created.

    program timeSummation
    
    ! define derived data type for time (hours, minutes, seconds)
    implicit none
    
    
    type time
        integer :: hours, minutes, seconds
    end type time
    
    ! declare variables
    !     includes → time1, time2, and timesum
    type(time) :: time1, time2, timesum
    
    ! display initial header
        write (*,'(/,a,/)') "Time Summation Example Program."
    
    ! call subroutine to read each time
        call readtime(time1)
        call readtime(time2)
    
    ! call subroutine to add times
        call addtimes(time1, time2, timesum)
    
    ! display results
        write (*,'(/,a,i2.2,a1,i2.2,a1,i2.2)') "Time One: ",        &
            time1%hours, ":", time1%minutes, ":", time1%seconds
        write (*,'(a,i2.2,a1,i2.2,a1,i2.2)') "Time Two: ",          &
            time2%hours, ":", time2%minutes, ":", time2%seconds
        write (*,'(a,i2.2,a1,i2.2,a1,i2.2,/)') "Time Sum: ",        &
            timesum%hours, ":", timesum%minutes, ":",               &
                timesum%seconds
    
    contains
    
    ! *******************************************************
    ! Subroutine to prompt for, read, and check
    !     a time (hours:minutes:seconds) in 24-hour format.
    
    subroutine readtime ( timeval )
    type(time), intent(out) :: timeval
    
        do
    !   prompt for time
            write (*,'(a)',advance="no")                    &
                "Enter time (hh mm ss): "
    
    !   read time (hours, minutes, seconds)
            read (*,*) timeval%hours, timeval%minutes,      &
                        timeval%seconds
    
    !   check time entered
            if ( timeval%hours >= 0 .and.                   &
                timeval%hours <= 23.and.                    &
                timeval%minutes >= 0 .and.                  &
                timeval%minutes <= 59 .and.                 &
                timeval%seconds >= 0 .and.                  &
                    timeval%seconds <= 59 ) exit
    
    !   display error message
            write (*,'(a,/,a)')                             &
                "Error, invalid time entered.",             &
                "Please re-enter time."
        
        end do
        
        return
    end subroutine readtime
    
    ! *******************************************************
    ! Subroutine to add two times.
    !     Ensures seconds and minutes are within range (0-59)
    !     Hours may exceed 23
    
    ! subroutine header and appropriate declarations
    subroutine addtimes ( tm1, tm2, tmsum )
    type(time), intent(in) :: tm1, tm2
    type(time), intent(out) :: tmsum
    
    ! add the seconds, minutes, hours
        tmsum%seconds = tm1%seconds + tm2%seconds
        tmsum%minutes = tm1%minutes + tm2%minutes
        tmsum%hours = tm1%hours + tm2%hours
    
    ! if seconds > 59, subtract 60 from seconds and add 1 to minutes
        if (tmsum%seconds > 59) then
            tmsum%seconds = tmsum%seconds - 60
            tmsum%minutes = tmsum%minutes + 1
        end if
    
    ! if minutes > 59, subtract 60 from minutes and add 1 to hours
        if (tmsum%minutes > 59) then
            tmsum%minutes = tmsum%minutes - 60
            tmsum%hours = tmsum%hours + 1
        end if
    
        return
    end subroutine addtimes
    
    end program timeSummation
    

    If the program does not work at first, the comments can aid in determining the problem.

    Test/Debug the Program

    For this problem, the testing would involve executing the program and entering a series of various time values to ensure that the results are correct. If the program does not work initially, the functionality of each subroutine should be checked. The times read from the user can be displayed to the screen to ensure they are correct. Once the times are correct, the add times subroutine can be checked. Each of the time sums can be displayed to help determine where the error might be.


    This page titled 16.4: Example One is shared under a CC BY-NC-SA 3.0 license and was authored, remixed, and/or curated by Ed Jorgensen via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.

    • Was this article helpful?