Write a program that prompts the user to enter a month and a year. Generate a ni
ID: 649800 • Letter: W
Question
Write a program that prompts the user to enter a month and a year. Generate a nicely
formatted calendar for this month e.g. (user input is shown underlined here)
Enter month: 5
Enter year: 2018
Su M Tu W Th F Sa
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
Required functions
You must write exactly the following functions and parameters:
void main(void)
main()is the first function in a C source file, where execution of the program begins. Is
kept simple and short, consists largely of function calls that clearly show the steps
required to solve the problem
void getMonthYear(int *month, int *year)
Uses scanf()s to set month and year. Note that since the parameters are already passed
to getMonthYear() by reference, your calls to scanf() do not need the & or * operators
e.g.
void getMonthYear(int *month, int *year)
{
printf("Enter month: ");
scanf("%d", month);// month is pbr, so &, * not required here
. . .
}
int toJulian(int month, int day, int year)
Takes a calendar date and calculates its
Explanation / Answer
#include <stdio.h>
#define MAX 31
#define MID 30
#define MIN 28
#define FOUR 4
#define ONEHUN 100
#define FOHUN 400
#define BEGIN 1900
#define YEAR 365
//Prototypes:
void getMonthYear(int *month, int *year);
int toJulian(int month, int day, int year);
int dayInMonth(int month, int year);
int leapYear(int year);
long yearsToDays(int year);
void printCalendar(int startDay, int numDays);
void printHeader();
void main(void)
{
int day, month, year, startDay, numDays;
startDay = 1;
numDays = 0;
day = 0;
getMonthYear(&month, &year);
toJulian(month, day, year);
yearsToDays(year);
printCalendar(startDay, numDays);
}
//Ask user to input data
void getMonthYear(int *month, int *year)
{
printf("Enter Month: ");
scanf_s("%d", month);
printf("Enter Year: ");
scanf_s("%d", year);
printf(" ");
}
//Takes a calendar date and calculates its julian day within that year
int toJulian(int month, int day, int year)
{
--month;
while(month != 0)
{
day += daysInMonth(month, year);
--month;
}
return day;
}
//Takes a month and year, calculates how many days are in this particular month
int daysInMonth(int month, int year)
{
switch (month)
{
case 1 : case 3 : case 5 : case 7 : case 8 : case 10 : case 12 :
return MAX;
break;
case 4 : case 6 : case 9 : case 11 :
return MID;
break;
case 2 :
return MIN + leapYear(year);
break;
}
}
//Takes a year, returns 1 if this is a leap year, 0 otherwise
int leapYear(int year)
{
if (year % FOUR == 0 && (year % ONEHUN != 0 || year % FOHUN == 0))
return 1;
else
return 0;
}
//Takes a year, returns the number of days from 1/1/1900 to the end of the previous year
long yearsToDays(int year)
{
long days = 0;
while(year != BEGIN)
{
--year;
days += YEAR + leapYear(year);
}
return days;
}
//Outputs the calendar to the screen.
void printCalendar(int startDay, int numDays)
{
printHeader();
}
void printHeader()
{
printf("%4s%4s%4s%4s%4s%4s%4s ", "Su", "M", "Tu", "W", "Th", "F", "Sa");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.