Second Request. Please read entire post. In a brief paper, describe the programs
ID: 3782774 • Letter: S
Question
Second Request. Please read entire post.
In a brief paper, describe the programs below. State the requirements for the program and discuss the logical process your program uses to meet those requirements. Please be as detailed as possible.
#include <stdio.h>
struct date {
int month;
int day;
int year;
};
// function adds 7 days to the date
struct date addSevenDays(struct date newDate)
{
// add 7 days
newDate.day += 7;
// months having 31 days
if ((newDate.month == 1 || newDate.month == 3 || newDate.month == 5 || newDate.month == 7 ||
newDate.month == 8 || newDate.month == 10 || newDate.month == 12) && newDate.day > 31)
{
// new month
newDate.day -= 31;
newDate.month += 1;
}
// months having 30 days
else if ((newDate.month == 4 || newDate.month == 6 || newDate.month == 9 || newDate.month == 11) && newDate.day > 30)
{
newDate.day -= 30;
// new month
newDate.month += 1;
}
// February
else
{
if (newDate.year % 4 == 0 && newDate.day > 29)
{
newDate.day -= 29;
newDate.month += 1;
}
else if (newDate.day > 28)
{
newDate.day -= 28;
newDate.month += 1;
}
}
// year changed
if (newDate.month > 12)
{
newDate.month = 1;
newDate.year += 1;
}
return newDate;
}
int main() {
struct date originalDate;
struct date newDate;
// prompt to read the date
printf("Enter a date in mm/dd/yyyy format: ");
scanf("%d/%d/%d", &originalDate.month, &originalDate.day, &originalDate.year);
// call function to add 7 days and assign it to newDate
newDate = addSevenDays(originalDate);
printf(" Original Date: %d/%d/%d ", originalDate.month, originalDate.day, originalDate.year);
printf(" New Date after adding 7 days: %d/%d/%d ", newDate.month, newDate.day, newDate.year);
return 0;
}
Explanation / Answer
#include <stdio.h>
Explanation: Header file for standard input and output
struct date
{
int month;
int day;
int year;
};
Explanation: Creates a structure named as date to store month, day and year
// Function adds 7 days to the date
Explanation: Remark statement
struct date addSevenDays(struct date newDate)
Explanation: Function header which takes a structure type object and returns a structure object
struct date newDate
Explanation: Creates an object of type date structure
struct date
Explanation: Return type of function structure object
newDate.day += 7;
Explanation:
newDate.day += 7; This is equivalent to newDate.day = newDate.day + 7
if ((newDate.month == 1 || newDate.month == 3 || newDate.month == 5 || newDate.month == 7 ||
newDate.month == 8 || newDate.month == 10 || newDate.month == 12) && newDate.day > 31)
Explanation: Checks whether the months having 31 days
newDate.month
Explanation: Extracts data member month data from the structure date
newDate.day
Explanation: Extracts data member day data from the structure date
newDate.month == 1 || newDate.month == 3 || newDate.month == 5 || newDate.month == 7 ||
newDate.month == 8 || newDate.month == 10 || newDate.month == 12) && newDate.day > 31
Explanation: Checks if the month is equal to 1 or 3 or 5 or 7 or 8 or 10 or 12
And the day is more than 31 then do the following operation given between opening ‘{’ and ‘}’ closing curly bracket.
newDate.day -= 31;
Explanation: Subtract 31 from the new day, because day cannot be more than 31. If so then it is next month.
newDate.month += 1;
Explanation: Add 1 to the new month
else if ((newDate.month == 4 || newDate.month == 6 || newDate.month == 9 || newDate.month == 11) && newDate.day > 30)
Explanation: If the if condition given above is false then else if will be executed.
If the month is not 31 days then check for 30 days
(newDate.month == 4 || newDate.month == 6 || newDate.month == 9 || newDate.month == 11) && newDate.day > 30
Explanation: If the month is either equal to 4 or 6 or 9 or 11 and the day is greater than 30 then do the following operation given within the curly bracket
{
newDate.day -= 30;
Explanation: Subtract 30 from the day and store it new the new day, because the above mentioned month day cannot be more than 30 days.
newDate.month += 1;
Explanation: Increase the month by one
}
Explanation: If both if and else if condition is false then else part will be executed.
else
{
if (newDate.year % 4 == 0 && newDate.day > 29)
Explanation: Checks for the leap year and new day is greater than 29 days then do the following operation given within curly brackets.
{
newDate.day -= 29;
Explanation: Subtract 29 from the new day and store the result in the new day.
newDate.month += 1;
Explanation: Increase the month by 1.
}
Explanation: If it is not leap year.
else if (newDate.day > 28)
Explanation: Checks if the new day is greater than 28 days then the do the following operation given within the curly bracket.
{
newDate.day -= 28;
Explanation: Subtract 28 from the new date and store in new day.
newDate.month += 1;
Explanation: Increase the month by 1
}
}
Explanation: End of else
Explanation: To change year
if (newDate.month > 12)
Explanation: Checks if the new month is greater than 12 then do the following operation given within the curly bracket, because month cannot be greater than 12.
{
newDate.month = 1;
Explanation: Assign 1 to month
newDate.year += 1;
Explanation: Increase the year by 1.
}
return newDate;
Explanation: Return the new date.
}
Explanation: End of function
int main()
Explanation: Main method definition from where program execution begins.
{
struct date originalDate;
Explanation: Creates an object of the structure to store the original date.
struct date newDate;
Explanation: Creates an object of the structure to store the new date.
printf("Enter a date in mm/dd/yyyy format: ");
Explanation: Displays message to accept month, date and year in the mm dd and yyyy format.
printf function is used to display message and data with a variable.
scanf("%d/%d/%d", &originalDate.month, &originalDate.day, &originalDate.year);
Explanation: Accept data from the user.
scanf function is used to accept data from the user.
%d to accept integer type data
newDate = addSevenDays(originalDate);
Explanation: Calls the function addSevenDays by passing parameter originalDate structure object.
newDate =
Explanation: Stores the returned structure object in newDate structure object.
printf(" Original Date: %d/%d/%d ", originalDate.month, originalDate.day, originalDate.year);
Explanation: Displays the original month, day and year
printf(" New Date after adding 7 days: %d/%d/%d ", newDate.month, newDate.day, newDate.year);
Explanation: Displays the new month, day and year after adding 7 days.
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.