Juliet C. answered 09/23/24
Multiple semesters of teaching intro to programming courses using C
Attempting to compile the given source code reveals the parts of the missing code based on the compiler errors and/or warnings.
- The first part of the missing code is the inclusion of the standard IO library. This is needed for the printf and scanf function calls.
- The second part of the missing code is the definition of the MonthAndDay structure. Based on the shown use of the result variable, we know that the structure needs to have the members month, which is a pointer and can be used as a string, and day, which can be used as an integer.
- The third part of the missing code is the definition of the isLeapYear function. Based on its use, we can see that it expects an integer parameter and it returns an integer value which is used as a boolean (zero for false, non-zero for true). Based on the given output, we can see that it meant to indicate whether its integer parameter represents a leap year, which means that February has 29 days. It should return a non-zero value if the integer it is given is a leap year. Looking at the output, 1900 and 2023 are not leap years, but 2000 and 2024 are. Doing some additional research shows us that a leap year is one in which the year is divisible by 4 but not divisible by 100 unless it is also divisible by 400.
- The fourth part of the missing code is the definition of the getMonthAndDay function. Based on how it is called, we can see that it takes three integers and two arrays and returns a MonthAndDay structure. The first two integers are for the year and the day in the year. The third integer looks like the size of the arrays, indicating a calendar with twelve months. The first array is for the textual (char*) representation of a month name and the second is for the number of days in the month. Based on the given output of the program, we see that the returned MonthAndDay structure, result, will have the correct month name in the month member and the day of the month in the day member, unless the day in the year exceeds the actual number of days in the year, in which case the month member is a null pointer. The year parameter does not seem to have a use, given that the number of days in February is changed before the function is called. The approach for determining the month and day corresponding to the day in the year is as follows:
- Declare a MonthAndDay structure in which to hold the month and day.
- Declare an integer variable for the month index, starting at zero.
- Loop while the remaining days is greater than 0 and the month index is less than the given number of months:
- If the remaining days is less than or equal to the number of days in the month at the month index, fill in the MonthAndDay structure with the month's name in the month member and the remaining days in the day member and then return the structure.
- Otherwise, decrease the number of remaining days by the number of days in the month at the month index and then increment the month index.
- If nothing was returned during the loop, set the MonthAndDay structure's month member to null to indicate that the given day in the year was not valid for the year and return the structure.
One way to produce a source code file that compiles and runs is to add the following code above the given main function.