In: Computer Science
Write a function that takes the current date and corrects the number of days, if it's wrong. The function must return true if the date passed to it is a valid date and false, if not. The main function uses the returned value to either print "Date validated", if a valid date was entered or "Invalid date entered. Changed to ", followed by the modified date.
For example: if given 11/31/2020, it will produce 12/1/2020. If given 2/29/2021, it will make it 3/1/2021. But, if 11/29/2020 is entered, it will not change it.
To check the date, the function uses the fact that months 4, 6, 9, 11 have 30 days, month 2 has 28 days in non-leap years and 29 in leap years and the remaining months have 31 days.
A year is a leap year if it's divisible by 400 or if not, it's divisible by 4, but not 100.
Example interaction between the user and the program:
Enter a date: 2/31/2021
Invalid date entered. Changed to 3/3/2021
Another example:
Enter a date: 12/32/2020
Invalid date entered. Changed to 1/1/2021
Another example:
Enter a date: 10/28/2020
Date validated.
Press any key to continue.
// C program to correct number of days
# include <stdio.h>
#include <stdbool.h>
// function prototype
bool Datecorrect(int *m,int *d,int *year);
int main()
{
int d,m, year;
int *p=&m;
int *q=&d;
int *r=&year;
// asking user to Enter date
printf("Enter date (mm/dd/yy): ");
scanf("%d/%d/%d", &m,&d,&year);
if(Datecorrect(p,q,r))
printf("Date Validated .");
else
printf("Invalid date entered. Changed to %d/%d/%d",m,d,year);
}
// function for date correction
bool Datecorrect(int *m,int *d,int *year)
{
if((*year%400==0 || *year%4==0 ) && *year%100!=0) //
checking leap year or not
{
// yes leap year
if(*m==1||*m==3||*m==5||*m==7||*m==8||*m==10||*m==12 ) // checking
month
{
// if month is anyof these
if(*d>31)
{
// if days are morethan 31 means error normally there are only
31
if(*m==12)
{
// if month is december next month is jan means month "1" and
weshould change year also
*m=1;
*d=*d-31;
*year=*year+1;
}
else // otherwise we just increment month and change days
{
*m++;
*d=*d-31;
}
return false;
}
return true;
}
if(*m==2) // if month is february
{
if(*d>29) // days are morethan 29 means error
{
*m++;
*d=*d-29;
return false;
}
return true;
}
else // if remaining months
{
if(*d>30) // if month has morethan 30 days we just increment
month and change days
{
*m=*m+1;
*d=*d-30;
return false;
}
return true;
}
}
else // If it is not yeap year
{
if(*m==4||*m==6||*m==9||*m==11)
{
if(*d>30) // if month has morethan 30 days we just increment
month and change days
{
*m=*m+1;
*d=*d-30;
return false;
}
return true;
}
if(*m==2) // if month is february
{
if(*d>28) // days are morethan 28 means error
{
*m=*m+1;
*d=*d-28;
return false;
}
return true;
}
else // if remaining months
{
if(*d>31)
{
// if days are morethan 31 means error normally there are only
31
if(*m==12)
{
// if month is december next month is jan means month "1" and
weshould change year also
*m=1;
*d=*d-31;
*year=*year+1;
}
else // otherwise we just increment month and change days
{
*m++;
*d=*d-31;
}
return false;
}
return true;
}
}
}
output1:
output2:
output3: