SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Function Basics
Exercise:
Programming Projects
Question:12 | ISBN:9780132846813 | Edition: 5

Question

Write a program that inputs a date (e.g., July 4, 2008) and outputs the day of the week that corresponds to that date. The following algorithm is from http:// en.wikipedia.org/wiki/Calculating_the_day_of_the_week. The implementation will require several functions:

bool isLeapYear(int year);

This function should return true if year is a leap year and false if it is not. Here is pseudocode to determine a leap year:

leap_year = ((year divisible by 400) or (year divisible by 4 and year not

divisible by 100))

int getCenturyValue(int year);

This function should take the first two digits of the year (i.e., the century), divide by 4, and save the remainder. Subtract the remainder from 3 and return this value multiplied by 2. For example, the year 2008 becomes (20/4) = 5 remainder 0. 3 - 0 = 3. Return 3 * 2 = 6.

int getYearValue(int year);

This function computes a value based on the years since the beginning of the century. First, extract the last two digits of the year. For example, 08 is extracted for 2008. Next, factor in leap years. Divide the value from the previous step by 4 and discard the remainder. Add the two results together and return this value. For example, from 2008 we extract 08. Then (8/4) = 2 remainder 0. Return 2 + 8 = 10.

int getMonthValue( int month, int year);

This function should return a value based on the following table and will require invoking the isLeapYear function:


MONTH RETURN VALUE

January 0 (6 if year is a leap year)

February 3 (2 if year is a leap year)

March 3

April 6

May 1

June 4

July 6

August 2

September 5

October 0

November 3

December 5

Finally, to compute the day of the week, compute the sum of the date’s day plus the values returned by getMonthValue , getYearValue , and getCenturyValue . Divide the sum by 7 and compute the remainder. A remainder of 0 corresponds to Sunday, 1 corresponds to Monday, etc.—up to 6 which corres ponds to Saturday. For example, the date July 4, 2008 should be computed as (day of month) + ( getMonthValue ) + ( getYearValue ) + ( get CenturyValue ) = 4 + 6 + 10 + 6 = 26. 26/7 = 3 remainder 5. The fifth day of the week corresponds to Friday.

Your program should allow the user to enter any date and output the corresponding day of the week in English.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Sorry the answer is not available at the moment…

If you are able to find the answer, please make sure to post it here. So that your Juniors have smile on their lips and feel happy.

Spread the 'tradition of sharing'.