Question

In: Computer Science

Write a function that takes the current date and corrects the number of days, if it's...

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.

Solutions

Expert Solution

// 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:


Related Solutions

Write a function that takes a number as input, and returns the character A if the...
Write a function that takes a number as input, and returns the character A if the input is 90 and above, B if it’s 80 and above but less than 90, C if it’s at least 70 but less than 80, D if it’s at least 60 but less than 70, and F if it’s less than 60. If the input is not a number or is negative, the function should exit 1 with an error (by calling the Matlab...
PYTHON: Write a function insertInOrder that takes in a list and a number. This function should...
PYTHON: Write a function insertInOrder that takes in a list and a number. This function should assume that the list is already in ascending order. The function should insert the number into the correct position of the list so that the list stays in ascending order. It should modify the list, not build a new list. It does not need to return the list, because it is modifying it.   Hint: Use a whlie loop and list methods lst = [1,3,5,7]...
Write a function DIVISORS in MIPS. This function takes a positive number from the register a0...
Write a function DIVISORS in MIPS. This function takes a positive number from the register a0 and prints in a column all of its divisors including 1 and the number itself. Don't forget that printing a number and printing a character requires two different system calls. Here is an example. If the number in a0 is 6 the following output appears: 1 2 3 6
Match a student number: Write a function MatchStudentNumber that takes a student number from the StudentNumber...
Match a student number: Write a function MatchStudentNumber that takes a student number from the StudentNumber column in the reference table and determine if there is a match with a student number from the StudentNumber column in the second table. The output is a logical flag that is true if at least one match was found, and integer array that points to rows in table where a match occurred. Note that the function should find all matches. function [iflag, matchPosition]...
Write a function named findIndex that takes an array of integers, the number of elements in...
Write a function named findIndex that takes an array of integers, the number of elements in the array, and two variables, such that it changes the value of the first to be the index of the smallest element in the array, and changes the value of the second to be the index of the largest element in the array. Please complete this in C++, using pass by reference
Write a function named findIndex that takes an array of integers, the number of elements in...
Write a function named findIndex that takes an array of integers, the number of elements in the array, and two variables, such that it changes the value of the first to be the index of the smallest element in the array, and changes the value of the second to be the index of the largest element in the array. Please complete this in C++
Write a function num_day that takes a number in the range of 1 through 7 as...
Write a function num_day that takes a number in the range of 1 through 7 as a parameter and returns a string representing the corresponding day of the week, where 1=Monday, 2 =Tuesday, 3=Wednesday, 4 = Thursday, 5 = Friday, 6 = Saturday, and 7 = Sunday. The function should return the string "Error" if the parameter is that is outside the range of 1 through 7. You may assume that the parameter will be only numbers. Save the function...
Write a function named "characters" that takes a string as a parameter and returns the number...
Write a function named "characters" that takes a string as a parameter and returns the number of characters in the input string
Write a function in Matlab that takes as input the number n and a symmetric tridiagonal...
Write a function in Matlab that takes as input the number n and a symmetric tridiagonal matrix given as two vectors: n×1 vector v representing the main diagonal and (n−1)×1 vector w representing the upper diagonal. Have this function output the Cholesky factor of the matrix as a vector for the main diagonal and a vector for the upper diagonal and output the number of flops and, separately, the number of square roots used as well. Use only basic programming....
Write a MATLAB function named numberWords() that takes a whole number as an argument and returns...
Write a MATLAB function named numberWords() that takes a whole number as an argument and returns a string containing the number word for the whole numbers 0 - 999. For example:  numberWords(234) would return 'two hundred thirty-four' If the input value is not a whole number between 0 - 999 then the function should return a string equivalent to 'ERROR'.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT