Write a function dayNumber that returns the day number of the year (1 to 366) in
ID: 3653514 • Letter: W
Question
Write a function dayNumber that returns the day number of the year (1 to 366) in a year for a date that is provided as input data. your function should accept the month (1 through 12), day, and year as integers. A year is a leap year if it is divisible by 4, except that any year divisible by 100 is a leap year only if it's also divisible by 400.write and use a second function that returns true if its argument, a year, is a leap year. Example: january 1, 1994 is day 1 december 31,1993 is day 365 (leap year) december 31,1996 is day 366 (leap year) Computer Science please put all the answer, i will rate 5 stairs to the best answer!Explanation / Answer
// dayNumInYear.cpp // with input validation #include using namespace std; const int DAYS[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; // with easier to see logic ... // and thus, more likely to see if correct logic inline bool isLeapYear( int y ) { return !(y%4) && (!(y%400) || (y%100)); /* // deal with most probable input first .. // this 1st test will return false for 3/4's of cases if( y%4 ) return false; // if reach here ... y is divided evenly by 4 // BUT ... before returning true ... // deal with special cases ... if( y%400 == 0 ) return true; // if reach here ... and ... if( y%100 == 0 ) return false; // otherwise ... return true; */ } // valid m (1..12) passed in ... and valid y int daysInMonth( int m, int y ) { int num = DAYS[m-1]; if( m == 2 && isLeapYear( y ) ) ++num; return num; } /* int daysBeforeMonth( int m, int y ) ( int sum = 0; for( int i = m - 1; i > 0; --i ) sum += daysInMonth( m, y ); return sum; ) */ int dayNumber( int m, int d, int y ) { int sumDaysBeforeMonth = 0; for( int i = m - 1; i > 0; --i ) sumDaysBeforeMonth += daysInMonth( i, y ); return d + sumDaysBeforeMonth; } // get valid month, day (and year in range 1600..3000) // and return values by reference ... void getValid( int& m, int& d, int& y ) { for( ; ; ) // loop forever ... until break/return { cout d >> y ) // i.e. if no error flag { if( y < 1600 || y > 3000 ) // use suitable limits { coutRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.