Write in C++ Write a program that prompts the user to enter the year and first d
ID: 3681843 • Letter: W
Question
Write in C++
Write a program that prompts the user to enter the year and first day of the year and display the calendar table for the year on the screen. You should follow the steps described below.
a) Develop an algorithm in pseudo language that specifies the main steps at the top level.
b) Apply the top-down design principle to refine each non-trivial step in the main algorithm into separate algorithms for sub-steps.
c) If there are non-trivial steps in the sub-steps, do refinement to them as necessary.
d) Implement your algorithms in a C++ program. The top level algorithm will be implemented as the “main( )” function, and other algorithms as user defined functions.
e) Answers to a), b), and c) will be put into the program as heading comments for the corresponding functions.
Explanation / Answer
d)
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
void main() {
printf("Enter the year: ");
int year, month, getFirstDay;
scanf("%d",&year);
printf("Enter 1st day of year ( 0 = Sunday, 6 = Satuday ): ");
scanf("%d",&getFirstDay);
for ( month = 1; month <= 12; month++ ){
int days = 0;
char monthName[50] = "";
switch (month) {
case 1: strcpy(monthName, "January");
days = 31;
break;
case 2: strcpy(monthName, "February");
if(((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)){
days = 29;
} else {
days = 28;
}
break;
case 3: strcpy(monthName, "March");
days = 31;
break;
case 4: strcpy(monthName, "April");
days = 30;
break;
case 5: strcpy(monthName, "May");
days = 31;
break;
case 6: strcpy(monthName, "June");
days = 30;
break;
case 7: strcpy(monthName, "July");
days = 31;
break;
case 8: strcpy(monthName, "August");
days = 31;
break;
case 9: strcpy(monthName, "September");
days = 30;
break;
case 10: strcpy(monthName, "October");
days = 31;
break;
case 11: strcpy(monthName, "November");
days = 30;
break;
case 12: strcpy(monthName, "December");
days = 31;
break;
default: printf("Error: this month does not exist");
break;
}
printf(" %s %d",monthName,year);
printf("-----------------------------------");
printf(" Sun Mon Tue Wed Thu Fri Sat");
int i = 0;
int firstDay = 0;
switch(month){
case 1: firstDay=getFirstDay;
break;
case 2: firstDay=getFirstDay+3;
break;
case 3: firstDay=getFirstDay+3;
break;
case 4: firstDay=getFirstDay+6;
break;
case 5: firstDay = getFirstDay + 8;
break;
case 6: firstDay = getFirstDay + 11;
break;
case 7: firstDay = getFirstDay + 13;
break;
case 8: firstDay = getFirstDay + 16;
break;
case 9: firstDay = getFirstDay + 19;
break;
case 10: firstDay = getFirstDay + 21;
break;
case 11: firstDay = getFirstDay + 24;
break;
case 12: firstDay = getFirstDay + 26;
break;
}
if(((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)){
switch(month){
case 1: firstDay=getFirstDay;
break;
case 2: firstDay=getFirstDay+3;
break;
case 3: firstDay=getFirstDay+4;
break;
case 4: firstDay=getFirstDay+7;
break;
case 5: firstDay = getFirstDay + 9;
break;
case 6: firstDay = getFirstDay + 12;
break;
case 7: firstDay = getFirstDay + 14;
break;
case 8: firstDay = getFirstDay + 17;
break;
case 9: firstDay = getFirstDay + 20;
break;
case 10: firstDay = getFirstDay + 22;
break;
case 11: firstDay = getFirstDay + 25;
break;
case 12: firstDay = getFirstDay + 27;
break;
}
}
int dayOfWeek = 0;
if ( (firstDay % 7 ) >= 0 ){
if ( (firstDay % 7 ) == 0 ){
dayOfWeek = 0;
} else if ( (firstDay % 7 ) == 1 ){
dayOfWeek = 1;
printf(" " );
} else if ( (firstDay % 7 ) == 2 ){
dayOfWeek = 2;
printf(" " );
} else if ( (firstDay % 7 ) == 3 ){
dayOfWeek = 3;
printf(" " );
} else if ( (firstDay % 7 ) == 4 ){
dayOfWeek = 4;
printf(" " );
} else if ( (firstDay % 7 ) == 5 ){
dayOfWeek = 5;
printf(" " );
} else if ( (firstDay % 7 ) == 6 ){
dayOfWeek = 6;
printf(" " );
}
}
for ( i = 1; i <= days; i++ ) {
if (i < 10)
printf(" %d",i);
else
printf(" %d",i );
if ((i + firstDay ) % 7 == 0 )
printf(" ");
}
printf(" ");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.