Question

IN C++ In a previous assignment you wrote pseudo code for the calculation of the days...

IN C++
In a previous assignment you wrote pseudo code for the calculation of the days into a year given the date.

Write a function that returns an int. It takes three parameters , all int(s),

int DaysIntoYear ( int Year, int Month, int day);

The return value is the number of days in the year from January 1 to the specified date.  

Year is included so you can determine if this is a leap year ( see below).  

If it is a leap year February will have 29 days not the typical 28.

I suggest using several lower level functions to break up the work.  

Please use this enum :

enum daysInMonth { Jan = 31, Feb = 28, Mar = 31, Apr = 30, May = 31, Jun = 30, Jul = 31, Aug = 31, Sep = 30, Oct = 31, Nov = 30, Dec = 31 };

Typical code that uses the enum elements would be :

switch (month) case 1 : return Jan; // etc.

at least one switch statement in your code.  

Advanced coders: Please do not use arrays in this solution. ( next time )

0 0
Add a comment Improve this question Transcribed image text
Answer #1

SOURCE CODE:

#include<iostream>
using namespace std;
enum daysInMonth {Jan=31,Feb=28,Mar=31,Apr=30,May=31,Jun=30,Jul=31,Aug=31,
                   Sep=30,Oct=31,Nov=30,Dec=31}; //enum that stores days in the month
int getDaysInMonth(int Month) //getDaysInMonth() function for getting days in the respecitve month
{
   switch(Month) //switch-case block
   {
       case 1: //case 1 then returning Jan
           return Jan;
       case 2: //case 2 then returning Feb
           return Feb;
       case 3: //case 3 then returning Mar
           return Mar;
       case 4: //case 4 then returning Apr
           return Apr;
       case 5: //case 5 then returning May
           return May;
       case 6: //case 6 then returning Jun
           return Jun;
       case 7: //case 7 then returning Jul
           return Jul;
       case 8: //case 8 then returning Aug
           return Aug;
       case 9: //case 9 then returning Sep
           return Sep;
       case 10: //case 10 then returning Oct
           return Oct;
       case 11: //case 11 then returning Nov
           return Nov;
       case 12: //case 12 then returning Dec
           return Dec;                                          
   }
}  
int checkLeapYear(int Year) //function for checking a year is leap or not
{
   if(((Year%4==0) && (Year%100!=0)) || (Year%400==0)) //condition for the year to become leap
   {
       return 1; //returning 1
   }
   else
       return 0; //returning 2
}              
int DaysIntoYear(int Year,int Month,int day)
{
   int Count=0; //Count variable to store no of days
   for(int i=1;i<Month;i++) //iterating upto given Month-1
   {
       if(i==1) //if 1 then adding days of Jan to Count
       {
           Count=Count+getDaysInMonth(i);
       }
       else if(i==2) //if 2 then adding days of Feb to Count
       {
           if(checkLeapYear(Year)==1) //checking year is leap or not
           {
               Count=Count+getDaysInMonth(i)+1;
           }
           else
               Count=Count+getDaysInMonth(i);  
       }
       else if(i==3) //if 3 then adding days of Mar to Count
       {
           Count=Count+getDaysInMonth(i);
       }
       else if(i==4) //if 4 then adding days of Apr to Count
       {
           Count=Count+getDaysInMonth(i);
       }
       else if(i==5) //if 5 then adding days of May to Count
       {
           Count=Count+getDaysInMonth(i);
       }
       else if(i==6) //if 6 then adding days of Jun to Count
       {
           Count=Count+getDaysInMonth(i);
       }
       else if(i==7) //if 7 then adding days of Jul to Count
       {
           Count=Count+getDaysInMonth(i);
       }
       else if(i==8) //if 8 then adding days of Aug to Count
       {
           Count=Count+getDaysInMonth(i);
       }
       else if(i==9) //if 9 then adding days of Sep to Count
       {
           Count=Count+getDaysInMonth(i);
       }
       else if(i==10) //if 10 then adding days of Oct to Count
       {
           Count=Count+getDaysInMonth(i);
       }
       else if(i==11) //if 11   then adding days of Nov to Count
       {
           Count=Count+getDaysInMonth(i);
       }
       else if(i==12) //if 12 then adding days of Dec to Count
       {
           Count=Count+getDaysInMonth(i);
       }
   }
   Count=Count+(day-1); //adding days of the current month to count
   return Count; //returning count
}                  
int main()
{
   int Month,day,Year;
   cout<<"Enter Year: ";
   cin>>Year; //reading Year from user
   cout<<"Enter Month: ";
   cin>>Month; //reading Month from user
   cout<<"Enter Day: ";
   cin>>day; //reading day from user
   int NoOfDays=DaysIntoYear(Year,Month,day); //calling DaysIntoYear() function
   cout<<"No of days upto the date "<<Year<<"/"<<Month<<"/"<<day<<" are: "<<NoOfDays<<endl; //displaying no of days
}                  

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
IN C++ In a previous assignment you wrote pseudo code for the calculation of the days...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT