Write a program named difDays() that calculates and returns the difference betwe
ID: 3814325 • Letter: W
Question
Write a program named difDays() that calculates and returns the difference between two dates. Each date is passed to the function as a structure using the following global type: struct Date { int month; int day; int year;}; The difDays() function should make two calls to the days() function written for Lab9 _p1.
This is the C++ source code that has the days() function:
#include <iostream>
#include <string.h>
#include <cstdlib>
using namespace std;
int days(struct Date, struct Date);
struct Date
{
int month;
int day;
int year;
};
int main()
{
int ans = 0;
Date fixed, temp;
fixed.month = 1;
fixed.day = 1;
fixed.year = 1900;
cout << "Enter the day (ie. 30): ";
cin >> temp.day;
cout << " Enter the month (ie. 04): ";
cin >> temp.month;
cout << " Enter the year (ie. 1999): ";
cin >> temp.year;
ans = days(fixed, temp);
cout << " Number of days since January 1, 1900 is equal to: " << ans << " ";
system("pause");
return 0;
}
int days(Date fixed, Date temp)
{
int a = 0;
a = (temp.year - fixed.year) * 360 + (temp.month - fixed.month) * 30 + (temp.day - fixed.day);
return a;
}
Explanation / Answer
#include <iostream>
#include <string.h>
#include <cstdlib>
using namespace std;
int days(struct Date, struct Date);
int difDays(struct Date, struct Date, struct Date);
struct Date
{
int month;
int day;
int year;
};
int main()
{
int ans = 0;
Date fixed, firstDate, lastDate;
fixed.month = 1;
fixed.day = 1;
fixed.year = 1900;
cout << "Enter the day (ie. 30): ";
cin >> firstDate.day;
cout << " Enter the month (ie. 04): ";
cin >> firstDate.month;
cout << " Enter the year (ie. 1999): ";
cin >> firstDate.year;
ans = days(fixed, firstDate);
cout << " Number of days since January 1, 1900 is equal to: " << ans << " ";
cout << "Enter the day (ie. 30): ";
cin >> lastDate.day;
cout << " Enter the month (ie. 04): ";
cin >> lastDate.month;
cout << " Enter the year (ie. 1999): ";
cin >> lastDate.year;
int diff = difDays(fixed, firstDate, lastDate);
cout << " Number of days difference is "<<diff<<endl;
// system("pause");
return 0;
}
int days(Date fixed, Date temp)
{
int a = 0;
a = (temp.year - fixed.year) * 360 + (temp.month - fixed.month) * 30 + (temp.day - fixed.day);
return a;
}
int difDays(Date fixed, Date firstDate, Date lastDate) {
int lastDateDays = days(fixed, lastDate);
int firstDateDays = days(fixed, firstDate);
return lastDateDays - firstDateDays;
}
Output:
sh-4.2$ g++ -o main *.cpp
sh-4.2$ main
Enter the day (ie. 30): 1
Enter the month (ie. 04): 1
Enter the year (ie. 1999): 1999
Number of days since January 1, 1900 is equal to: 35640
Enter the day (ie. 30): 1
Enter the month (ie. 04): 1
Enter the year (ie. 1999): 2000
Number of days difference is 360
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.