Solve FORTRAN 77 Programming Problem: Date Validation & Conversion

  • Thread starter BrownianMan
  • Start date
  • Tags
    Programming
In summary, the course is difficult and the way it is taught is not adequate. We don't have any practice in the computer lab and the only time we get to use the material is during the 1 lab per week. Many people are struggling.
  • #1
BrownianMan
134
0
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:
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:
Physics news on Phys.org
  • #2
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.
 
  • #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?
 
  • #4
BrownianMan said:
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.
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:
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.
 
  • #5
When I compile it, it tells me that checkFormat is a function, but it's declared as a subroutine in the mainprogram.
 
  • #6
Sorry, I didn't notice that you had called it in your main routine. Here is the header written as a subroutine.
Code:
subroutine checkFormat(dateString, day, month, year, ok)
character*(*) dateString
character*2 day, month
character*4 year
logical ok
 

1) What is FORTRAN 77?

FORTRAN 77 is a programming language designed for scientific and engineering applications. It was released in 1977 and is still used today for legacy systems and numerical computing.

2) What is date validation?

Date validation is the process of checking if a given date is valid according to a set of rules. This includes checking for the correct number of days in a month, accounting for leap years, and ensuring the date falls within a specified range.

3) How do you validate a date in FORTRAN 77?

In FORTRAN 77, date validation can be achieved by using built-in functions such as DATE_AND_TIME and DATE_VALUES. These functions allow you to extract and manipulate date components and compare them to the given date to determine its validity.

4) What is date conversion?

Date conversion is the process of converting a date from one format to another. This can include converting from a string representation to a numerical representation, or converting between different date formats such as MM/DD/YYYY to DD/MM/YYYY.

5) How do you convert a date in FORTRAN 77?

In FORTRAN 77, date conversion can be achieved by using the DATE_AND_TIME and DATE_VALUES functions to extract the date components, and then using WRITE statements to format the date in the desired format.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Programming and Computer Science
Replies
6
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
0
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Programming and Computer Science
Replies
10
Views
2K
Back
Top