Question
Write these 7 functions into C++ code as functions to fullfill the int main() function as a full program that is seen in the sample
Write these 7 functions into C++ code as functions to fullfill the int main() function as a full program that is seen in the sample run below.
Functions:
1. void testMenu();
Post condition: the test menu is displayed for choice.
2. bool isLeapYear(int year);
This function should return true if year is a leap year and false if it is not. Here is the pseudocode to determine a leap year:
leapYear = (year divisible by 400) or (year divisible by 4 and year not divisible by 100)
3. int getCenturyValue(int year);
This function should take the first two digits of the year (that is, 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 with a remainder of 0. 3 - 0 = 3. Return 3 * 2 = 6.
4. 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 with a remainder of 0. Return 2 + 8 = 10.
5. getMonthValue(int month, int year);
This function should return a value based on the table below 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
6. std::string dayOfWeek(int day);
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 corresponds to Saturday. For example, the date July 4, 2008 should be computed as (day of month) + (getMonthValue) + (getYearValue) + (getCenturyValue) = 4 + 6 + 10 + 6 = 26. 26/7 = 3 with a remainder of 5. The fifth day of the week corresponds to to Friday.
Your program should allow the user to enter any date and output the corresponding day of the week in English.
This program should include a void function named getInput that prompts the user for the date and returns the month, day, and year using pass-by-reference parameters. You may choose to have the user enter the date's month as either a number (1-12) or a month name.
7. int dayOfWeek(int month, int day, int year);
The function should encapsulate the necessary logic to return the dayof the week of the specified date as an in (Sunday = 0, Monday = 1, etc.) You should add validation code to the function that tests if any of the inpus are valid. If so, the function should return -1 as the day of the week. In your main function write a test driver that checks if dayOfWeek is returning the correct values. Your set of test cases should include at least two cases with invalid inputs.
int main() function:
For any functions where the pre-condition and post-condition are missing, they should be
added. The main function is given as follows:
int main()
{
using namespace std;
int choice;
int day, month, year;
{
testMenu();
cout << "Please choose from menu: ";
cin >> choice;
swtich (choice)
{
case 1: // check if a given year is leap year
cout << "Please enter a year: ";
cin >> year;
if (isLeapYear(year))
cout << "Year" << year << " is a leap year" << endl;
else
cout << "Year " << year << is NOT a leap year" << endl;
break;
case 2: // calculate the century value of a given year
cout << "Please enter a year: ";
cin >> year;
cout << "The centruy value is: " << getCenturyValue(year) << endl;
break;
case 3: // calculate the year value of a given year
cout << "Please enter a year: ";
cin >> year;
cout << "The year value is: " << getYearValue(year) << endl;
break;
case 4: // calculate the month value of a given month in a given year
cout << "Please enter a year and month: ";
cin >> year >> month;
cout << "The month value is: " << getMonthValue(month, year) << endl;
break;
case 5: // calculate the day of week of a given date
cout << "Please enter a year, a month, and a day : ";
cin >> year>> month >> day;
cout << "The day of the week is: " << dayOfWeek(month, day , year) <<
endl;
break;
case 6: // print out the name of a given day of week
cout << "Please enter a day of week (0 for Sunday, 1 for Monday, etc): ";
cin >> day;
cout << "The name of the day of the week is : " << dayOfWeek(day) << endl;
break;
case 7:
cout << "Did you test all functions yet ? If not, please re-run the program ";
break;
default:
cout << "Wrong option. Please choose from menu ";
break;
}
system("pause");
} while (choice != 7);
}
system("pause");
Sample run:
Test Menu
1. isLeapYear
2. getCenturyValue
3. getYearValue
4. getMonthValue
5. dayOfWeek(month, day, year)
6. dayOfWeek(day)
7. Quit
Please choose from menu: 1
Please enter a year: 1900
Year 1900 is NOT a leap year
Test Menu
1. isLeapYear
2. getCenturyValue
3. getYearValue
4. getMonthValue
5. dayOfWeek(month, day, year)
6. dayOfWeek(day)
7. Quit
Please choose from menu: 1
Please enter a year: 2000
Year 2000 is a leap year
Test Menu
1. isLeapYear
2. getCenturyValue
3. getYearValue
4. getMonthValue
5. dayOfWeek(month, day, year)
6. dayOfWeek(day)
7. Quit
Please choose from menu: 2
Please enter a year: 2000
The century value is: 6
Test Menu
1. isLeapYear
2. getCenturyValue
3. getYearValue
4. getMonthValue
5. dayOfWeek(month, day, year)
6. dayOfWeek(day)
7. Quit
Please choose from menu: 3
Please enter a year: 2008
The year value is: 10
Test Menu
1. isLeapYear
2. getCenturyValue
3. getYearValue
4. getMonthValue
5. dayOfWeek(month, day, year)
6. dayOfWeek(day)
7. Quit
Please choose from menu: 4
Please enter a year and month: 2016 10
The month value is: 0
Test Menu
1. isLeapYear
2. getCenturyValue
3. getYearValue
4. getMonthValue
5. dayOfWeek(month, day, year)
6. dayOfWeek(day)
7. Quit
Pleas choose from menu: 5
Please enter a year, a month, and a day: 2016 10 12
The day of the week is: 3
Test Menu
1. isLeapYear
2. getCenturyValue
3. getYearValue
4. getMonthValue
5. dayOfWeek(month, day, year)
6. dayOfWeek(day)
7. Quit
Please choose from menu: 6
Please enter a day of week (0 for Sunday, 1 for Monday, etc): 3
The name of the day of the week is: WEDNESDAY
Test Menu
1. isLeapYear
2. getCentruyValue
3. getYearValue
4. getMonthValue
5. dayOfWeek(month, day, year)
6. dayOfWeek(day)
7. Quit
Please choose from menu: 7
Did you test all of the functions yet? If not, please re-run the program.
Program ended with exit code: 0
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started