178k views
1 vote
Write a function named days() that determines the number of days since January 1, 1900 for any data passed as a structure. Use the Date structurestruct Date{int month;int day;int year;};In writing the days() function, use the convention that all years have 360 days and each month consists of 30 days. The function should return the number of days for any Date structure passed to it. Write a main () function to test your function.

1 Answer

2 votes

Step-by-step explanation:

#include <stdio.h>

#include <stdlib.h>

struct date

{

int month;

int day;

int year;

};

int days(struct date *d)

{

int daysTotal;

// Adding days from date::day

if (d->day > 0 && d->day < 31) // Only execute if day is valid

{

daysTotal = d->day - 1;

}// Adding days from date::month

if (d->month > 0 && d->month < 13) // Only execute if month is valid

{

daysTotal += 30 * (d->month-1);

}// Adding days from date::year

if (d->year > 1899) // Only execute if year is valid

{

daysTotal += 360 * (d->year-1900); // subtracting 1900 years

}

return daysTotal; // This may still be a negative value, but is accepted here for simplicity

}

int main()

{

struct date myDate;

printf("Enter a Month: ");

scanf("%d", &myDate.month);

printf("Enter a Day: ");

scanf("%d", &myDate.day);

printf("Enter a Year: ");

scanf("%d", &myDate.year);

printf("the date you entered = %d days", days(&myDate));

return 0;

}

User Delana
by
6.3k points