Create a project called Daily27. Add a C source file to the project named daily2
ID: 3571183 • Letter: C
Question
Create a project called Daily27. Add a C source file to the project named daily27.c. In this exercise, you will write a program that helps you to calculate yesterday's date for any day in year 2016. Your program should first ask the user for a month (1~12) and a valid day in that month from the keyboard as today's date, e.g., month 11 and day 30 represents Nov. 30. You may assign 2016 to the year in your program directly without asking the user. Store today's date in a Structure type variable as we learned in the class. You may assume that the user always enters a valid month (i.e., 1~12) and a valid date in that month. The structure type Date should be defined as a global name, so all functions in this program can use it. The program then will call a function to print this date (as an argument) using the format month/day/year (e.g., 11/30/2016) on the screen. The function will only print the date itself and does nothing else. Next, your program will call another function, e.g., calculateYesterday, to help you to calculate yesterday's date based on today's date. The function takes today's date as an argument and return yesterday's date to the main function (a structure type variable yesterday should be declared in main function to store the returned value). After calling the function, call the print date function again to print yesterday's date on the screen. Make sure that the calculation function work for any day m the year of 2016 (including the first day of the year, and remember 2016 is a leap year). You may use the array below (or a similar one) to help your calculation. Each element in the array represents the number of days in each month from Jan. to Dec respectively, in 2016. const int daysInMonth(12) = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; Your program output should look something like the following: Please enter a month (1~12): 11 Please enter a valid day in the month: 30 Today is: 11/30/2016 Yesterday is: 11/29/2016 Please enter a month (1~12): 12 Please enter a valid day in the month: 1 Today is: 12/1/2016 Yesterday is: 11/30/2016Explanation / Answer
#include<stdio.h>
//declare strucure Date with there variables as day , month and year
typedef struct
{
int day;
int month;
int year;
}Date;
Date date;
//function declaration
void todayDate();
Date calculateYesterday(Date d);
int main()
{
Date d;
//take inputs for month and day
printf("Please enter a month(1-12): ");
scanf("%d",&date.month);
printf("Please enter a valid date in the month: ");
scanf("%d",&date.day);
//assume year is 2016
date.year = 2016;
//call todayDate to display todays date
todayDate();
//call calculateYesterday to calculate yday date
d = calculateYesterday(date);
//print yday date
printf("Yestrday is : %0d/%0d/%0d ",d.month,d.day,d.year);
}
void todayDate()
{
printf("Today is : %0d/%0d/%0d ",date.month,date.day,date.year);
}
Date calculateYesterday(Date d)
{
//declare array of months to hold number of days in a month
const int daysInMonth[12]={31,29,31,30,31,30,31,31,30,31,30,31};
Date yday;
if( d.day == 1)
{
//12th days will be stored in 11th index
yday.day = daysInMonth[d.month -1-1];
yday.month = d.month -1;
yday.year = d.year;
}
else
{
yday.day = d.day - 1;
yday.month = d.month;
yday.year = d.year;
}
return yday;
}
-------------------------------------------------------
output1:
Please enter a month(1-12): 11
Please enter a valid date in the month: 30
Today is : 11/30/2016
Yestrday is : 11/29/2016
outpt2:
Please enter a month(1-12): 12
Please enter a valid date in the month: 1
Today is : 12/1/2016
Yestrday is : 11/30/2016
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.