Overview In this program you will use the functions (getdaycode(), printheader()
ID: 3653767 • Letter: O
Question
Overview In this program you will use the functions (getdaycode(), printheader(), findndim()) that you created in a previous assignment. Unlike the first assignment, you will write the main program. You may also write other functions such as genmonth(), discussed below, if you choose to. Problem statement: Prompt the user for a starting month/year, and the number of months (all on one line). Print out a calendar for the starting month, and then each month following until the correct number of months has been printed. The program should then again prompt the user for a starting month/year and the number of months. This should repeat until 0 is entered for the number of months. Several example runs are at the end of this handout. General Hints: If you could create a "magic" function to generate one month (like genmonth()), you could call that function repeatedly however many times were required (based on number of months requested. Each time the function was called you would need to pass a different month and maybe year to it (when the month was 13, you would need to reset it to 1, and increment the year). Regarding generating a single month, there are five main considerations: 1. Parametersoffunction-probablymonthandyearareneeded. 2. Printing the month name and year - you have a function which does this...use it. It is perfectly ok (and highly useful) for a function to call another function. 3. Startingday-tostartonthecorrectday,youneedtoskipsomenumberof spaces (four spaces for each day). I.e. if the first day of the month is on a Wednesday (daycode is 3) you need to skip 3 sets of four spaces each; you can put a printf(" "); in a loop and loop the needed number of times. 4. Correct number of days in month - you have a function to determine how many days are in each month...use it! 5. Determine when week ends - There are two ways to check this. The first way is to use the getdaycode() function, and if the daycode is 6, print a " ". The other way to do it is to start some kind of a counter when you are first printing leading spaces (to start the month). Whenever that counter is a multiple of 7, print a " ". You are welcome to do it either way.Explanation / Answer
#include bool LeapYear (int year) // "year" must be between 1900 and 2999. // Returns true if "year" is a leap year. { // true, if year is divisible by 4, and ... // ... either not divisible by 100, or divisible by 400. return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)); } //************************************** const int daysInMonth[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; // Days from the beginning of the year // to the beginning of the month: const int daysToMonth[12] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 }; const char *dayName[7] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; const int DAY0 = 1; // Day of week for 01-01-1900 is Monday = 1. //************************************** bool ValidDate (int month, int day, int year) // Returns true if month-day-year is a valid date between // 01-01-1900 and 12-31-2999. { bool valid = false; // First assume the date is invalid. int days; // If year and month have valid values: if (year >= 1900 && year = 1 && month = 1 && day 2) days++; // Add days since the beginning of the month: days += day - 1; return days; } //************************************** int DayOfWeek (int month, int day, int year) // Returns the day of the week for a given date: // 0 -- Sunday, 1 -- Monday, etc. { return int((DAY0 + DaysSince1900(month, day, year)) % 7); } int main() { int month, day = 1, year, days, j; int weekday; cout > month >> year; if (!ValidDate(month, day, year)) { coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.