Write a program that prompts the user to enter the year and the first day of the
ID: 3776341 • Letter: W
Question
Write a program that prompts the user to enter the year and the first day of the year and output the calendar table for the year to a text file. You should follow the steps described below. Develop an algorithm in pseudo language that specifies the main steps at the top level. Apply the top-down design principle to refine non-trivial steps in the main algorithm into separate algorithms for sub-steps. If there are still non-trivial steps in the sub-steps, do further refinement to them as necessary. Implement your algorithm in a C++ program. The top-level algorithm will be implemented as the "main()" function, and other algorithms as user defined functions. Answers to a), b) and c) will be put in the program as heading comments for the corresponding functions. A sample output is illustrated in the textbook on pages 198 to 199.Explanation / Answer
//Anser Code.
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
int get_day_code (int year);
int get_leap_year (int year);
void print_calendar (FILE *fout, int year, int day_code, int leap_year);
int get_year (void);
main()
{
int year, day_code, leap_year;
FILE *fout;
fout = fopen ("calendar.txt", "w");
year = get_year();
day_code = get_day_code (year);
leap_year = get_leap_year (year);
print_calendar(fout, year, day_code, leap_year);
printf("Open up 'calendar.txt' to see your calendar... ");
system("pause");
}
void print_calendar (FILE *fout, int year, int day_code, int leap_year) //function header
{
int days_in_month, /* number of days in month currently
being printed */
day, /* counter for day of month */
month; /* month = 1 is Jan, month = 2 is Feb, etc. */
fprintf (fout," %d", year);
for ( month = 1; month <= 12; month++ ) {
switch ( month ) { /* print name and set days_in_month */
case 1:
fprintf(fout," January" );
days_in_month = 31;
break;
case 2:
fprintf(fout," February" );
days_in_month = leap_year ? 29 : 28;
break;
case 3:
fprintf(fout, " March" );
days_in_month = 31;
break;
case 4:
fprintf(fout," April" );
days_in_month = 30;
break;
case 5:
fprintf(fout," May" );
days_in_month = 31;
break;
case 6:
fprintf(fout," June" );
days_in_month = 30;
break;
case 7:
fprintf(fout," July" );
days_in_month = 31;
break;
case 8:
fprintf(fout," August" );
days_in_month = 31;
break;
case 9:
fprintf(fout," September" );
days_in_month = 30;
break;
case 10:
fprintf(fout," October" );
days_in_month = 31;
break;
case 11:
fprintf(fout," November" );
days_in_month = 30;
break;
case 12:
fprintf(fout," December" );
days_in_month = 31;
break;
}
fprintf(fout," Sun Mon Tue Wed Thu Fri Sat " );
/* advance printer to correct position for first date */
for ( day = 1; day <= 1 + day_code * 5; day++ )
fprintf(fout," " );
/* print the dates for one month */
for ( day = 1; day <= days_in_month; day++ ) {
fprintf(fout,"%2d", day );
if ( ( day + day_code ) % 7 > 0 ) /* before Sat? */
/* move to next day in same week */
fprintf(fout," " );
else /* skip to next line to start with Sun */
fprintf(fout, " " );
}
/* set day_code for next month to begin */
day_code = ( day_code + days_in_month ) % 7;
}
}
int get_day_code (int year)
{
int day_code;
int x1, x2, x3;
x1 = (year - 1.)/ 4.0;
x2 = (year - 1.)/ 100.;
x3 = (year - 1.)/ 400.;
day_code = (year + x1 - x2 + x3) %7;
return day_code;
}
int get_year (void)
{
int year;
printf ("Enter a year: ");
scanf ("%d", &year);
return year;
}
int get_leap_year (int year)
{
//if((year% 4) == 0 );
if(year% 4==0 && year%100 != 0 || year%400==0)
return TRUE;
else return FALSE;
}
/* The codes are: day_code (0 = Sun, 1 = Mon, etc.)
leap_year (0 = no leap year, 1 = leap year) */
Thank you,
Happy Chegging
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.