C++ code Instructions: Write a program that asks a user to enter a date in month
ID: 3604954 • Letter: C
Question
C++ code
Instructions: Write a program that asks a user to enter a date in month day year format (for example 10 12 2016) and displays the day of the week for that date in this case, Wednesday Be sure to validate month and day to be sure they are in range. To validate 2/29/yyy, you will have to call the daLeapyeAK function. You will require several functions bool isteapearlint year); This function will return true if the year passed is a leap year and false if not. The pseudocode to determine if a given year is a leap year is: evenly divisible by 100)) int getCentuxyValuelint year) leapVear- ((year evenly divisible by 400) or {year evenly divisible by 4 and year NOT This function should take the first two digits of the year ( the century), divide by 4 and save the remainder. Subtract the remainder from 3, multiply the result by 2 and return that value. For example the year 2013 becomes (20/4): 5, remainder 0, 3-0 3 return 3 * 2- 6. Hint, to get the century divide by 100. This function computes a value based on the years since the beginning of the century First, extract the last two digits of the year (year%100), in this case 13, Next, factor in leap years. Divide the last two digits by 4 and add that back to the result and return that Eg (13/4) - 3. 3-13 16 int getMonthValue.(int month, int year) This function should return a value based on the table below and will require calling the isleapYear function.Explanation / Answer
The main() is the driver for this program. Different functions are implemented as said in the question.
isValidDate() checks if the date entered by the user is valid or not example months with 30 days should be entered with days<= 30 or February with leap year can have 29 days.
getCenturyValue()
getYearValue()
getMonthValue()
All these methods are implemented as given in the question.
The values of these methods are added with the day value to find the sum as:
int month= getMonthValue(mm,yy);
int year = getYearValue(yy);
int century = getCenturyValue(yy);
int sum = dd+month+year+century;
int week = sum%7;
Now, week==0 is sunday, week==1 is Monday and so on....
The output screens have been provided.
If you have any doubts kindly you can comment. I'll reply as soon as possible.
Kindly rate the answer.All the best. :)
*********************************************************************
Code:
#include <iostream>
using namespace std;
bool isLeapYear(int year)
{
if (year % 4 == 0)
{
if (year % 100 == 0)
{
if (year % 400 == 0)
return true;
else
return false;
}
else
return true;
}
else
return false;
}
int getCenturyValue(int year)
{
int digits=year/100;
int remainder=digits%4;
int result = 3 - remainder;
result*=2;
return result;
}
int getYearValue(int year)
{
int digits=year%100;
int divide=digits/4;
int result = divide + digits;
}
int getMonthValue(int month, int year)
{
if(isLeapYear(year))
{
if(month==1)
return 6;
else if(month==2)
return 2;
else if(month==3)
return 3;
else if(month==4)
return 6;
else if(month==5)
return 1;
else if(month==6)
return 4;
else if(month==7)
return 6;
else if(month==8)
return 2;
else if(month==9)
return 5;
else if(month==10)
return 0;
else if(month==11)
return 3;
else
return 5;
}
else
{
if(month==1)
return 0;
else if(month==2)
return 3;
else if(month==3)
return 3;
else if(month==4)
return 6;
else if(month==5)
return 1;
else if(month==6)
return 4;
else if(month==7)
return 6;
else if(month==8)
return 2;
else if(month==9)
return 5;
else if(month==10)
return 0;
else if(month==11)
return 3;
else
return 5;
}
}
int isValidDate(int dd, int mm, int yy)
{
if(yy>0)
{
if(mm>=1 && mm<=12)
{
if((dd>=1 && dd<=31) && (mm==1 || mm==3 || mm==5 || mm==7 || mm==8 || mm==10 || mm==12))
{}
else if((dd>=1 && dd<=30) && (mm==4 || mm==6 || mm==9 || mm==11))
{}
else if((dd>=1 && dd<=28) && (mm==2))
{}
else if(dd==29 && mm==2 && (isLeapYear(yy)==1))
{}
else{
printf("Day is invalid. ");
return -1;
}
}
else
{
printf("Month is not valid. ");
return -1;
}
}
else
{
printf("Year is not valid. ");
return -1;
}
return 1;
}
int main()
{
int dd,mm,yy;
printf("Enter date (MM DD YYYY format): ");
scanf("%d %d %d",&mm,&dd,&yy);
int checkValid=isValidDate(dd,mm,yy);
if(checkValid==1)
{
int month= getMonthValue(mm,yy);
int year = getYearValue(yy);
int century = getCenturyValue(yy);
int sum = dd+month+year+century;
int week = sum%7;
if(week==0)
printf(" Sunday");
else if(week==1)
printf(" Monday");
else if(week==1)
printf(" Tuesday");
else if(week==1)
printf(" Wednesday");
else if(week==1)
printf(" Thursday");
else if(week==1)
printf(" Friday");
else
printf(" Saturday");
}
}
*********************************************************************
Sample output 1:
Sample output 2:
***********************************************************************
I hope this helps you.
If you find my answer helpful,
Kindly rate the answer.
All the best :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.