Complete labdays.cpp Write a function dayNumber that returns the day number (1 t
ID: 3827931 • Letter: C
Question
Complete labdays.cpp Write a function dayNumber that returns the day number (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. As an example, January 1, 1994 is day 1. December 31, 1993 is day 365. December 31, 1996 is day 366 since 1996 is a leap year. A year is a leap year if it’s divisible by four, 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 isLeapYear that returns true if its argument, a year, is a leap year. Don’t modify the two function prototypes
//Define the functions here
Explanation / Answer
#include <iostream>
using namespace std;
/*
Write a function dayNumber that returns the day number (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.
As an example, January 1, 1994 is day 1. December 31, 1993 is day 365.
December 31, 1996 is day 366 since 1996 is a leap year. A year is a leap year if it’s divisible by four,
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 isLeapYear that returns true if its argument, a year, is a leap year.
*/
int dayNumber(int,int,int);
//Precondition: Expects three numbers. First number beween 1 and 12 (month). Second being between 1 and 31 (day).
// Third being four digits (year). The three must represent a valid date.
//Postcondition: The day number from 1 to 366 is returned.
bool isLeapYear(int);
//Precondition: Expects a four digit positive number which represents a year
//Postcondition: Returns true if the given year is a leap year (year with 366 days instead of 365), false otherwise
int main() {
cout << "1/1/1994 is day number " << dayNumber(1,1,1994) << endl;
cout << "12/31/1993 is day number " << dayNumber(12,31,1993) << endl;
cout << "12/31/1996 is day number " << dayNumber(12,31,1996) << endl;
cout << "7/30/2010 is day number " << dayNumber(7,30,2010) << endl;
cout << "12/31/1900 is day number " << dayNumber(12,31,1900) << endl;
cout << "12/31/2000 is day number " << dayNumber(12,31,2000) << endl;
cout << "12/31/2010 is day number " << dayNumber(12,31,2010) << endl;
return 0;
}
int dayNumber(int month,int day,int year) {
int days[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
int numdays = 0;
for(int i=0; i<month; i++){
numdays = numdays + days[i];
}
if(isLeapYear(year)) {
numdays++;
}
return numdays-day;
}
bool isLeapYear(int y) {
if (y % 4 != 0) return false;
if (y < 1582) return true;
return (y % 100 != 0) || (y % 400 == 0);
}
Output:
sh-4.2$ g++ -o main *.cpp
sh-4.2$ main
1/1/1994 is day number 30
12/31/1993 is day number 334
12/31/1996 is day number 335
7/30/2010 is day number 182
12/31/1900 is day number 334
12/31/2000 is day number 335
12/31/2010 is day number 334
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.