DECLARE IN MAIN an array of days per month (element 0 will be ignored, element 1 will have 31 for January, element 2 will have 28 for February, etc.) AND two other array of ints (called daysArray and yearsArray here). The program will be converting between the "Day of the year" with year and a calendar date (MM DD YYYY). In main do the following:
Call a function (see 1. below) to read Calendar Dates from a text file, but store only the Day-of-year and the year in corresponding elements of the daysArray and yearsArray. Assign the return value (number of elements) to a local variable (like numElems).
Call a function (see 2. below) to print numElems elements of the daysArray and yearsArray to another text file. Make sure you read into the arrays first (if you didn't, don't call this function).
You must have the following in your program:
function (call from main) to fill the daysArray and yearsArray using the converted data from an input file (use Prog1Input.txt input file on the G: drive for turning in). You will be opening and closing the file in this function (if it doesn't open, print an error message and return 0 and don't try to read anything). Each input line in the file will be a date (3 ints) of the form MM DD YYYY. Convert each date to a day-of-year (an int) using another function (see 3. below) and if it's a valid date, store in an element of the daysArray, and store the year in the corresponding element of the yearsArray. Stop when you get to the end of file or the end of the array. Return the number of elements in a return statement.
function (call from main) to write the array and the day-of-year with year converted back to a calendar date (use another function, see 4. below) to a text file. The output file will be opened and closed in this function (if it doesn't open, print an error message and return and don't try to write anything). In this function, do the following:
print headers in the output file for "Day of Year", and "Calendar Date"
For each of the elements used in daysArray and yearsArray, print an element of daysArray, call function #4 (see 4. below) to get the calendar date, print the Calendar date in the form MM/DD/YYYY
function (call from function #1) to do the following:
check if month is 1 through 12, day is 1 through daysOfMonth[month], or day is 1 through daysOfMonth [month]+1 if year is a leap year (use function 5. below) and year is 1000 through 9999, return –1 in a return statement if NOT
calculate the Day-of-year date given the Calendar date USING THE ARRAY OF DAYS PER MONTH. Remember to add 1 to the Day-of-year date if the date is after Feb. 28 and the year is a leap year (leap year function specs below – see 5. below). Return the Day-of-year date in a return statement.
function (call from function #2) that calculates the Calendar date given a Day-of-year and a year USING THE ARRAY OF DAYS PER MONTH. YOU MUST traverse the ARRAY OF DAYS PER MONTH to find the correct month! The year will be used to determine if it's a leap year (call 5. below), so you'll add 1 to the days for February if it is a leap year. Assign to "reference parameters" (pointer parameters in C) the month and day.
function (call from function #3, and function #4) that returns if a year (given as a parameter) is a leap year or not. The year is a leap year if: the year is divisible by 4 and NOT divisible by 100, OR the year is divisible by 400. Use the modulus (%) operator to determine if a number is divisible by another number!! Return (in a return statement) a non-zero int if the year is a leap year, 0 if not.
DO NOT USE ANY EXTERNAL VARIABLES!! You'll have to pass the arrays (declared in main) to other functions. Also, main should be mostly function and "subroutine" calls.
Include in your program:
Arrays of ints (size of 13 for daysOfMonth array, size of 100 for the other arrays)
#defines for all constants except for 0 or 1 (unless it makes sense for 0 or 1)
Functions with pointer parameters (see 4. above)
Comments for program, variables and functions (use textbook's style of function comments)
NOTE: Do not change the declared size of the array when you change input files!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference
and should not be submitted as is. We are not held liable for any misuse of the solutions.
Please see the frequently asked questions page
for further questions and inquiries.
Kindly complete the form.
Please provide a valid email address and we will get back to you within 24 hours.
Payment is through PayPal, Buy me a Coffee
or Cryptocurrency.
We are a nonprofit organization however we need funds to keep this organization operating
and to be able to complete our research and development projects.