Homework 4

20 points - Due session 4

Goal

The purpose of this assignment is to help you gain experience using conditional statements, loops, arrays, and pointers in C++.

Assignment

Part 1 - (5 points)

Implement a function with the following signature to return the number of days in the passed-in month argument.

int GetDaysIn(int nMonth, int nYear);

The function should return -1 if any of the arguments is invalid. Note that years evenly divided by 400 are leap years, unlike years otherwise evenly divided by 100. Your function must use at least one switch statement and at least one if statement to perform its work.

Implement the main() function for this program. This function must call GetDaysIn() repeatedly to fill an array of integers with values representing the number of days in each month of a particular year. The process of repeatedly calling GetDaysIn() to fill the array must be implemented using a while loop. Next, use a separate for loop to output the contents of the array to the screen. Do this for enough different test cases to ensure that GetDaysIn() always returns the correct results.

Part 2 - (5 points)

More fun with calendars...

Calculating Easter

Choose any year of the Gregorian calendar and call it x.  To determine the date of Easter, carry out the following 10 calculations (it’s easy to program them on a computer):

  1. Divide x by 19 to get a quotient (which we ignore) and a remainder A.  
    This is the year’s position in the 19-year lunar cycle.  (A+1 is the year’s Golden Number.)

  2. Divide x by 100 to get a quotient B and a remainder C.

  3. Divide B by 4 to get a quotient D and a remainder E.

  4. Divide 8B + 13 by 25 to get a quotient G and a remainder (which we ignore).

  5. Divide 19A + B – D – G + 15 by 30 to get a quotient (which we ignore) and a remainder H.  
    (The year’s Epact is 23 – H when H is less than 24 and 53 – H otherwise.)

  6. Divide A + 11H by 319 to get a quotient M and a remainder (which we ignore).

  7. Divide C by 4 to get a quotient J and a remainder K.

  8. Divide 2E + 2J – K – H + M + 32 by 7 to get a quotient (which we ignore) and a remainder L.

  9. Divide H – M + L + 90 by 25 to get a quotient N and a remainder (which we ignore).

  10. Divide H – M + L + N + 19 by 32 to get a quotient (which we ignore) and a remainder P.

Easter Sunday is the Pth day of the Nth month (N can be either 3 for March or 4 for April).  The year’s dominical letter can be found by dividing 2E + 2J – K by 7 and taking the remainder (a remainder of 0 is equivalent to the letter A, 1 is equivalent to B, and so on).

(from Mathematical Recreations, by Ian Stewart, Scientific American, March 2001, p. 82)

Write a function that uses this formula to calculate and print the date of Easter for any given year.  Check to make sure that you get the right answer for 2002 (March 31).

Part 3 - (5 points)

Using function pointers:

Does this look like anything you might be able to use later???

Part 4 (5 points)

Consider the following code fragment:

int nVal = 7;
int* pVal1 = NULL;
int* pVal2 = NULL;

Assume that the following set of assignment operations are executed in sequence. Which of these assignment operations are safe? Which are not? Explain each answer.

pVal1 = nVal;
pVal1 = &nVal;
pVal2 = pVal1;
nVal = pVal2;
pVal1 = 20;
*pVal1 = 20;
nVal = 20;

Note that you do not need to define any classes for this assignment!

Deliverables

Put your program files for Parts 1 and 2 and a file named part3.txt containing your answers to Part 3 into an archive file named hw3.zip or hw3.tar.Z.  Save this archive file in your AISYG 240 course directory (Courses/AISYG 240) on ftp.coe.neu.edu.