In a brief paper, describe the programs that you have written for this assessmen
ID: 3782712 • Letter: I
Question
In a brief paper, describe the programs that you have written for this assessment. 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>
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.month == 2)){
if (newDate.year % 4 == 0)
{
if( newDate.year%100 == 0)
{
if( newDate.year%400 == 0 && newDate.day > 29)
{
newDate.day -= 29;
newDate.month += 1;
}
}
else if(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;
}