1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Programming question

  1. Dec 11, 2009 #1
    I'm currently taking my first programming course (1st year student - using FORTRAN 77) and the way the course is taught is atrocious. We listen to the prof give his lecture (3/4 of which is him going off on tangents). We spend no time in a computer lab. The only "practice" we get is 1 lab per week (for which he gives us 80% of the answer) - which isn't really suficient for understanding the material well enough to apply it to different scenarios. Needless to say, a lot of people are struggling.

    I have a question, and any help is appreciated. Having trouble with this lab.

    Date Validation & Conversion
    Given a date like 20/2/2002, we like to convert it to: Wed. Feb 20, 2002. The process involves validating the input to ensure it has the proper format and then extracting from it the day, month, and year as strings. Assuming the extracted fields are indeed numeric, we convert them to integers and then verify that the date exists; e.g. it is not the 31st of September or the 29th of February in a non-leap year. Once properly validated, we employ the Zeller algorithm to find the day of the week (dow) and then output the result looking up the month name and the day name.

    Here is how the sought system should behave:


    Enter the date ...
    test
    Invalid format!
    Enter the date ...
    test/
    Invalid format!
    Enter the date ...
    t/mm/yyyy
    Non-numeric values!
    Enter the date ...
    12/45/98
    None existent date!
    Enter the date ...
    10/2/1983
    Thu. Feb 10, 1983

    This is what I have so far:

    Code (Text):
    program LongDate
    implicit none

    character*10 date, day, month, year, readDate
    integer d, m, y
    logical ok

    do
       ok = .true.
       date = readDate()
       call checkFormat(date, day, month, year, ok)
       if (.not. ok) then
          print*, "Invalid format!"
       else
          call checkValue(day, month, year, d, m, y, ok)
          if (.not. ok) then
             print*, "Non-numeric values!"
          else
            call existDMY(d, m, y, ok)
            if (.not. ok) then
               print*, "Non-numeric date!"
            else
                call printDate(d, m, y)
            end if
          end if
        end if
        if (ok) exit
    end do

    stop
    end

    subroutine checkValue(day, month, year, d, m, y, ok)

    character*(*) day, month, year
    integer d, m, y
    logical ok, isDigits
    integer s2n_

    ok = isDigits(day)
    if (.not. ok) return
    d = s2n_(day)

    ok = isDigits(month)
    if (.not. ok) return
    m = s2n_(month)

    ok = isDigits(year)
    if (.not. ok) return
    y = s2n_(year)

    return
    end

    logical function isDigits (str)

    character*(*) str
    character*11 digits /" 0123456789"/
    integer i

    do i = 1, len(str)
       if (index(digits, str(i:i)) .eq. 0) then
          isDigits = .false.
          return
       end if
    end do
    isDigits = .true.
    return

    end

    integer function s2n_ (str)

    character*(*) str
    character*10 digits /"0123456789"/

    integer i, nextDigit

    s2n_ = 0
    do i = 1, len(str)
       nextDigit = index(digits, str(i:i))
       if (nextDigit .gt. 0) then
          s2n_ = 10*s2n_ + nextDigit - 1
       end if
    end do

    return
    end
    I have to create the following subroutines:

    readDate()
    prompt for a date, read it as a string, return it to the caller.

    checkFormat
    verify that the date string has two slashes and extract and return the three substrings delimited by these slashes. Return false if the extraction was not possible. Note that this sub does not care about the content of the three strings, only that they exist; e.g. they can contain non-digits but it still returns true.

    existDMY
    verify that the passed integers do indeed represent a date that exists (month between 1 and 12 and day between 1 and 28 or 29 or 30 or 31 depending on the month and year). If not, return false.

    isLeap
    verify that the passed year is a leap year. If not, return false.

    printDate
    Output the date by translating Zeller's dow code into Sun or Mon, or ... Sat and outputting this day name followed by a dot. The month number is also translated into a month name (Jan Feb Mar ... Dec). The final format is: ddd. mmm dd, yyyy (where ddd is the day name and mmm is the month name).

    dow
    determine the day of the week and return it as 0 for Sunday, 1 for Monday, ... and 6 for Saturday. This sub uses an algorithm due to Zeller.
     
    Last edited: Dec 11, 2009
  2. jcsd
  3. Dec 11, 2009 #2

    Mark44

    Staff: Mentor

    The logic of what you have seems reasonable on a quick glance. What you can do now is put in stubs for your remaining subroutines. For example, your stub for readDate could just return the same string all the time, checkFormat could return three valid substrings, and so on. That way you can build your program incrementally and you always have a working program, but probably with bugs.
     
  4. Dec 11, 2009 #3
    Most of what I had was given to us by the prof. That's what makes it so difficult to actually learn anything - he gives us 95% of the answers for the labs and expects us to learn the material based on that. Then when I actually have to write a program (or subroutine) I'm literally stuck.

    Any suggestions on how to get started for checkFormat for example?
     
  5. Dec 12, 2009 #4

    Mark44

    Staff: Mentor

    A place to start is with the information that the checkFormat function needs (date string), the strings it should return (the three substrings in the date string), and the actual return value (true if there were three substrings in the date string, and false otherwise).
    From this information it can be seen that there need to be four parameters in the parameter list, and the the function should return a boolean value. The header for checkFormat therefore should look like this:
    Code (Text):

    logical function checkFormat(dateString, day, month, year)
    character*(*) dateString
    character*2 day, month
    character*4 year
     
    To parse the dateString you need to have a loop go through the entire string looking for '/' characters. If there are not two of these characters, return false. If there are two '/' characters, then you can continue by pulling the characters that precede the first '/' character, putting them in the day variable. Put the characters between the two '/' characters in the month variable, and put the characters following the second '/' character into the year variable. After the three variables are extracted, return true.
     
  6. Dec 12, 2009 #5
    When I compile it, it tells me that checkFormat is a function, but it's declared as a subroutine in the mainprogram.
     
  7. Dec 14, 2009 #6

    Mark44

    Staff: Mentor

    Sorry, I didn't notice that you had called it in your main routine. Here is the header written as a subroutine.
    Code (Text):

    subroutine checkFormat(dateString, day, month, year, ok)
    character*(*) dateString
    character*2 day, month
    character*4 year
    logical ok
     
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Programming question
  1. Programming question (Replies: 6)

Loading...