One of the most frustrating aspects of using the Gregorian c

One of the most frustrating aspects of using the Gregorian calendar is that it is difficult to communicate/compute elapsed time. For example, if I tell you that something is experiencing its 2-month anniversary, you don\'t know the exact number of day that have elapsed unless I tell you which 2 months February 3rd to April 3rd is 59 days (in a non-leap year), while April 3rd to June 3rd is 61 days. For this problem, you need to write a program that will accept two days of the same year (in month-day form) and output the elapsed time between the two days (not including the last day). To keep it simple, the program should first ask the user for the month of the first day, then the date of the first day, then the second day, and lastly the date of the second day (so four total prompts). For example, if you wanted to know the elapsed number of days between September 4th and November 18th, you would enter 9 the first prompt, 4 into the second prompt. 11 into the third prompt, and 18 into the fourth prompt. Assume that it is not a leap year?

Solution

C++ program to find number of days from day 1 to day 2:

#include<iostream.h>
int daysInMonth(int month) //Method to return number of days in month, -1 if invalid month
{
   int days = -1;
   switch(month){
      case 1:
      case 3:
      case 5:
      case 7:
      case 8:
      case 10:
      case 12: days = 31;
           break;
      case 4:
      case 6:
      case 9:
      case 11: days = 30;
           break;
      case 2: days = 28;
          break;
      default: days = -1;
           break;
   }
   return days;
}

int main()
{
   int mon1, mon2, date1, date2;
   int cday1 = 0, cday2 = 0;
   int i;
   // read day1
   cout<<\"Enter month of first day: \";
   cin>>mon1;
   cout<<\"Enter date of first day: \";
   cin>>date1;
   // read day2
   cout<<\"Enter month of second day: \";
   cin>>mon2;
   cout<<\"Ener date of second day: \";
   cin>>date2;
   //validate days
   if (date1<0 || date1>daysInMonth(mon1)
   || date2<0 || date2>daysInMonth(mon2))
   {
      cout<<\"Invalid date\";
   }
   else
   {
      // find cumulative days from previous months for day1
      for(i=1;i<mon1;i++)
      {
   cday1 = cday1 + daysInMonth(i);
      }
      cday1 = cday1 + date1;
      // find cumulative days from previous months for day2
      for (i=1;i<mon2;i++)
      {
   cday2 = cday2 + daysInMonth(i);
      }
      cday2 = cday2 + date2;
      cout<<\"Number of days from day1 to day2 is \" <<cday2-cday1<<\"\ \"; //display difference
   }
   return 0;
}

 One of the most frustrating aspects of using the Gregorian calendar is that it is difficult to communicate/compute elapsed time. For example, if I tell you tha
 One of the most frustrating aspects of using the Gregorian calendar is that it is difficult to communicate/compute elapsed time. For example, if I tell you tha

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site