Assignment 9.1 [15 points] C++ This assignment will not be graded for style issu
ID: 3857907 • Letter: A
Question
Assignment 9.1 [15 points] C++
This assignment will not be graded for style issues. If your code works correctly, you'll get 15 points. Style will be graded next week.
No documentation required for this assignment
Placing your output in a comment at the bottom of your file is not documentation, and is still required. No other comments are required. We'll get to that in the next assignment.
Write a program that prints a calendar for one year, given the day of the week that January 1 falls on. Each successive month starts on the day of the week that follows the last day of the preceding month. Days of the week will be numbered 0 through 6 for Sunday through Saturday.
This is the first part of a two part assignment using incremental development. The output will not look much like a calendar in part 1.
Do not use classes, arrays, or structs in this project!! (If you don't know what they are, don't worry, you aren't using them.)
Ultimately, after your next assignment, Your output should look exactly like the one in this sample screen output:
However, for this assignment, you should just start every month on the same day (the day entered by the user), and just print all of the dates out on a single line. So, the output for this assignment will look like this:
Additional Requirements and Hints:
Your program must determine whether a year is a leap year exactly, not just by checking for divisibility by 4. Also remember that you are free to steal the isLeapYear function from the lessons.
The number of days in each month and the numbers in the leap year formula are not good candidates for using global constants. You should just use the literals directly in your code.
It is extremely important that you use stepwise refinement when designing this program. Insufficient decomposition may result in a grade of 0.
Required! You MUST have a single function named "printMonth()" that prints out ANY month, given (1) the year, (2) the month number, and (3) the day of week that the month starts on. (Think about why the printMonth() function will need to know the year.) For example, don't create 12 separate functions, one named "printJanuary", one named "printFebruary," and so on. In addition, you must have only ONE call to this printMonth function, in a loop that calls the function 12 times. This printMonth() function must print the entire month, including the month name and header.
Let me describe my solution to this problem, not so that you can try to match it exactly, but so that you'll know if you are going way off into the wrong direction. I had 8 functions all together including main. 4 of them were 5 lines or less. 2 of them were fairly long functions but only because they were switch statements with 12 branches -- hard if not impossible to decompose further. That leaves 2 functions which were 8 - 10 lines long. One of these was the printMonth function.
When I count lines I count only the body of the function and I don't count declaration statements or blank lines or comments, but I do count a line if it has only a } on it.
Make sure each of your functions performs a single logical task (not two). If you find yourself trying to name a function with the word AND, it probably means the function should be divided into two.
Printing out the month names: They don't need to be centered. Each month name should start right above the letter "T", as illustrated in the sample output.
Explanation / Answer
#include <iostream.h>
#include <stdlib.h>
#include <iomanip.h>
typedef int Bool;
// converts 0 to 'S', 1 to 'M', etc
char whatDay (int);
// not very good check for leap years
Bool isLeapYear (int);
// takes the number of the month, a flag saying whether year is leap
int numOfDaysInMonth (int,Bool);
void printHeader (int);
// takes the number of the month, and the first day, prints, and updates
// the first day of the next month
void printMonth (int, int&);
// prints the specified amount of spaces
void skip (int);
// prints leading spaces in monthly calendar
void skipToDay (int);
// terminates program in case of unrecoverable errors
void disaster ();
int main () {
int year, firstDayInCurrentMonth;
Bool leap;
int currentMonth = 1; // start at Jan
int numDays;
cout << "What year do you want a calendar for? ";
cin >> year;
cout << "What day of the week does January 1 fall on?" << endl;
cout << "(Enter 0 for Sunday, 1 for Monday, etc.) ";
cin >> firstDayInCurrentMonth;
leap = isLeapYear(year);
skip(9); cout << year << endl;
while (currentMonth <= 12) {
numDays = numOfDaysInMonth(currentMonth,leap);
printHeader(currentMonth);
printMonth(numDays, firstDayInCurrentMonth);
cout << endl << endl << endl;
currentMonth = currentMonth + 1;
}
cout << endl;
return 0;
}
void disaster () {
cout << "Disaster! Exiting ..." << endl;
exit (-1);
}
void skip (int i) {
while (i > 0) {
cout << " ";
i = i - 1;
}
}
char whatDay (int d) {
if (d == 0) return('S');
else if (d == 1) return('M');
else if (d == 2) return('T');
else if (d == 3) return('W');
else if (d == 4) return('T');
else if (d == 5) return('F');
else if (d == 6) return('S');
else disaster();
}
Bool isLeapYear (int y) {
return ((y % 4) == 0); // simplified
}
void printHeader (int m) {
if (m == 1) { skip(7); cout << "January" << endl; }
else if (m == 2) { skip(7); cout << "February" << endl; }
else if (m == 3) { skip(7); cout << "March" << endl; }
else if (m == 4) { skip(7); cout << "April" << endl; }
else if (m == 5) { skip(7); cout << "May" << endl; }
else if (m == 6) { skip(7); cout << "June" << endl; }
else if (m == 7) { skip(7); cout << "July" << endl; }
else if (m == 8) { skip(7); cout << "August" << endl; }
else if (m == 9) { skip(7); cout << "September" << endl; }
else if (m == 10) { skip(7); cout << "October" << endl; }
else if (m == 11) { skip(7); cout << "November" << endl; }
else if (m == 12) { skip(7); cout << "December" << endl; }
else disaster();
cout << " S M T W T F S" << endl;
cout << "____________________" << endl;
}
int numOfDaysInMonth (int m, Bool leap) {
if (m == 1) return(31);
else if (m == 2) if (leap) return(29); else return(28);
else if (m == 3) return(31);
else if (m == 4) return(30);
else if (m == 5) return(31);
else if (m == 6) return(30);
else if (m == 7) return(31);
else if (m == 8) return(31);
else if (m == 9) return(30);
else if (m == 10) return(31);
else if (m == 11) return(30);
else if (m == 12) return(31);
else disaster();
}
void skipToDay (int d) {
skip(3*d);
}
void printMonth (int numDays, int& weekDay) {
int day = 1;
skipToDay(weekDay);
while (day <= numDays) {
cout << setw(2) << day << " ";
if (weekDay == 6) {
cout << endl;
weekDay = 0;
}
else weekDay = weekDay + 1;
day = day + 1;
}
}