Your program should have 4 functions. isLeapYear should be a bool value-returnin
ID: 3615314 • Letter: Y
Question
Your program should have 4 functions.
isLeapYear should be a bool value-returningfunction that determines whether the year, passed by a valueparameter is a leap year
getDate is a void function that prompts fordate input, checks that the entered date is in the correct formatand has valid numbers, passes the day month and year back usingreference parameters
dayNumber is a void function with valueparameters to pass the day month and year, and a referenceparameter to pass back the calculated day number
outputDay is a void function that outputs theoriginal date and the day number, using value parameters to passthe date and day number information
Write a program that prints the day number of the year, giventhe date in the form month-day-year. For example, if the input is1-1-2006, the day is 1; if the input is 12-25-2006 the day is 359.The program should check for a leap year. A leap year if it isdivisible by 4, but not by 100. For example, 1992 & 2008 aredivisible by 100 is a leap year if it is also divisible by 400. Forexample, 1600 and 200 are divisible by 400. However, 1800 is not aleap year because 1800 is not divisible by 400.
#include <iostream>
using namespace std;
bool isLeapYear (int year);
int main ()
{
int day, month, year;
int dayNumber;
char ch;
cout << "Program that prints theday number: " ;
cout << " Please enter a date(mm-dd-yyyy): " ;
cin >> month;
cin >> day ;
cin >> year;
cin >> ch ;
dayNumber = 0;
while (month > 1)
{
// all numbers come after
switch (month 1)
{
case 2:
dayNumber = 28;
if (isLeapYear (year))
dayNumber++;
break;
case 4:
case 6:
case 9:
case 11:
dayNumber = 30;
break;
case 1 :
case 3 :
case 5 :
case 7 :
case 8 :
case 10:
case 12:
dayNumber = 31;
break;
}
month--;
}
dayNumber = day;
cout<< "The day number is: " << dayNumber;
system("PAUSE");
return0;
}
bool isLeapYear (int year)
{
if (isLeapYear / =4) &&(isLeapYear !/ = 100) ||
(isLeapYear / =100 ) && (isLeapYear / =400)
return true;
return false;
}
Explanation / Answer
#include <iostream> using namespace std; bool isLeapYear (int year); int main () { int i; int day, month, year; int dayNumber; //char ch; cout << "Program that prints the daynumber: " ; cout << " Please enter a date(mm-dd-yyyy): " ; cin >> month; cin >> day ; cin >> year; //cin >> ch ; dayNumber = 0; for(i=1; i<month; i++) { if(i==1||i==3||i==5||i==7||i==8||i==10||i==12) { dayNumber=dayNumber+31; } else if(i==4||i==6||i==9||i==11) { dayNumber=dayNumber+30; } else if(i==2) { if(isLeapYear(year)==1) { dayNumber=dayNumber+29; } else { dayNumber=dayNumber+28; } } } dayNumber =dayNumber+ day; cout << "The day number is: "<< dayNumber<<endl; system("PAUSE"); return 0; } bool isLeapYear (int year) { if ((year%4==0)||(year%400==0)) { return true; } return false; }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.