You want a program that can take 2 dates represented by (m1, d1, y1) and (m2, d2
ID: 3537745 • Letter: Y
Question
You want a program that can take 2 dates represented by (m1, d1, y1) and (m2, d2, y2) Note that m1 and m2 are month numbers (1-12) , d1 and d2 are days in the month, and y1 and y2 are year numbers. You will create a function (deltaDays) which will compute the number of days between the 2 dates. The function deltaDays will call some helper functions to do some of the work: isLeapYear, daysInMonth, isLessThanOrEqual. To do this, copy the following starting code and replace the comments describing the algorithm with real code. #include <iostream> #include <iomanip> using namespace std; bool isLeapYear(int y) { // Write code to // return true if "y" is a leap year, otherwise return false // LEAP YEAR DEFINITION: //If a year is not evenly divisible by 100, then a leap year is // any year that is evenly divisible by 4, otherwise we don't have a leap year //If a year is evenly divisible by 100, then it is not a leap year unless // the year is evenly divisible by 400. } int daysInMonth(int m, int y) { // Write code to // return the number of days in a month (the month number is 1-12 where 1 represents January) // Note that April, June, September and November have 30 days // February has 29 days if "y" is a leap year, otherwise February has 28 days // All other months have 31 days } bool isLessThanOrEqual(int m1, int d1, int y1, int m2, int d2, int y2) { // Write code to return a true if the date represented by // (m1, d1, y1) is less than or equal to the date represented by (m2, d2, y2) // HINT: if y1 != y2 then you know the answer without going any further // if y1 == y2 then compare m1 and m2 to determine the answer // if y1 == y2 and m1 == m2, then you will need to look at d1 and d2 } int deltaDays(int m1, int d1, int y1, int m2, int d2, int y2) { // Return the number of days to go from the date represented by // (m1, d1, y1) to the date represented by (m2, d2, y2) // Note this answer can be negative. //Recommended approach: int delta; if (isLessThanOrEqual(m1,d1, y1, m2, d2, y2)) { delta = d2 -d1; // if (m1, y1) is not equal to (m2, y2) then // add to delta "daysInMonth(m, y)" for every // month (m) and year(y) needed to go from // (m1, y1) to (m2, d2) -- This will be some kind of while loop // As an example, suppose that // (m1, d1, y1) = (3, 10, 2012) // (m2, d2, y2) = (5, 25, 2012) // delta =d2 -d1 = 15; // there are 2 months that need to be added // for the months 3, and 4 which have 31 and 30 days respectively // So the total answer is 76 } else delta = -deltaDays (m2, d2, y2, m1, d1, y1); //Note: deltaDays can call deltaDays return delta; } // Leave this routine alone void test(int m1, int d1, int y1, int m2, int d2, int y2) { int delta = deltaDays(m1, d1, y1, m2, d2, y2); cout << delta << " days between " << m1 << "/" << d1 << "/" << y1 << " and " << m2 << "/" << d2 << "/" << y2 << endl; } // Run at least the following tests. You can add more at the end if you like int main() { test(11, 20, 2001, 11, 28, 2001); // In the same month test(11, 20, 2001, 12, 2, 2001); // different month, same year test(11, 20, 2001, 1, 2, 2002); // different year test(2, 4, 2001, 2, 28, 2001); // test leap year logic test(2 , 4, 2001, 3 , 5, 2001); test(2 , 4, 2004, 3 , 5, 2004); test(2 , 4, 2000, 3 , 5, 2000); test(2 , 4, 1900, 3 , 5, 1900); // Testing a span that is over a year test (2, 4, 2012, 4, 4, 2013); test (4, 4, 2013, 2, 4, 2012); // Negative answer on this one }
what is the source code? and the output?
Explanation / Answer
#include <iostream>
#include <iomanip>
using namespace std;
bool isLeapYear(int y)
{
return ((y % 4 == 0) && (y% 100 != 0) || (y % 400 == 0));
}
int daysInMonth(int m, int y)
{
switch(m)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12: return 31; break;
case 4:
case 6:
case 9:
case 11: return 30; break;
case 2: if(isLeapYear(y)) return 29; else return 28; break;
}
return 0;
}
bool isLessThanOrEqual(int m1, int d1, int y1, int m2, int d2, int y2)
{
if(y1!=y2)
{
return (y1<=y2);
}
else
{
if(m1!=m2)
{
return (m1<=m2);
}
else
{
return (d1<=d2);
}
}
return true;
}
int deltaDays(int m1, int d1, int y1, int m2, int d2, int y2)
{
int delta=0;
if (isLessThanOrEqual(m1,d1, y1, m2, d2, y2))
{
delta = d2 -d1;
int month = m1;
int year = y1;
while((year<=y2))
{
if((year==y2) && (month == m2)) break;
delta = delta + daysInMonth(month,year);
if(month==12)
{
year++;
month = 0; // initialized to zero here because below statement will increase by 1.
}
month++;
}
}
else
delta = -deltaDays (m2, d2, y2, m1, d1, y1); //Note: deltaDays can call deltaDays
return delta;
}
// Leave this routine alone
void test(int m1, int d1, int y1, int m2, int d2, int y2)
{
int delta = deltaDays(m1, d1, y1, m2, d2, y2);
cout << delta << " days between " <<
m1 << "/" << d1 << "/" << y1 << " and " <<
m2 << "/" << d2 << "/" << y2 << endl;
}
// Run at least the following tests. You can add more at the end if you like
int main()
{
test(11, 20, 2001, 11, 28, 2001); // In the same month
test(11, 20, 2001, 12, 2, 2001); // different month, same year
test(11, 20, 2001, 1, 2, 2002); // different year
test(2, 4, 2001, 2, 28, 2001);
// test leap year logic
test(2 , 4, 2001, 3 , 5, 2001);
test(2 , 4, 2004, 3 , 5, 2004);
test(2 , 4, 2000, 3 , 5, 2000);
test(2 , 4, 1900, 3 , 5, 1900);
// Testing a span that is over a year
test (2, 4, 2012, 4, 4, 2013);
test (4, 4, 2013, 2, 4, 2012); // Negative answer on this one
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.