NOTE: Must use user-defined functions Problem: In recent years the start of spri
ID: 3674128 • Letter: N
Question
NOTE: Must use user-defined functions
Problem: In recent years the start of spring break has taken place 9 weeks after the start of the spring semester. Given a month, day, and year determine the date that occurs 9 weeks later.
Example Execution #1 (this school year): Enter month number: 1 Enter day number: 11 Enter the year: 2016 Original Date: 1/11/2016 Future Date: 3/14/2016
Example Execution #2 (next school year): Enter month number: 1 Enter day number: 9 Enter the year: 2017 Original Date: 1/9/2017 Future Date: 3/13/2017
Example Execution #3 (dates can span years): Enter month number: 12 Enter day number: 31 Enter the year: 2015 Original Date: 12/31/2015 Future Date: 3/3/2016
Example Execution #4: Enter month number: 10 Enter day number: 30 Enter the year: 2014 Original Date: 10/30/2014 Future Date: 1/1/2015
Explanation / Answer
#include <iostream>
using namespace std;
static int days_in_month[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int day, month, year;
unsigned short day_counter;
int is_leap(int y) {
return ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0);
}
void next_day()
{
day += 1; day_counter++;
if (day > days_in_month[month]) {
day = 1;
month += 1;
if (month > 12) {
month = 1;
year += 1;
if (is_leap(year)) {
days_in_month[2] = 29;
} else {
days_in_month[2] = 28;
}
}
}
}
void set_date(int d, int m, int y)
{
m < 1 ? m = 1 : 0;
m > 12 ? m = 12 : 0;
d < 1 ? d = 1 : 0;
d > days_in_month[m] ? d = days_in_month[m] : 0;
if (is_leap(y)){
days_in_month[2] = 29;
}
else {
days_in_month[2] = 28;
}
day = d;
month = m;
year = y;
}
void skip_days(int x)
{
int i;
for (i=0;i<x;i++)
next_day();
}
void print_date()
{
cout <<" Future Date:"<< month<<"/"<<day<<"/"<< year;
}
int main()
{
int d,m,y;
cout <<"Enter month number";
cin >> m;
cout <<"Enter day number";
cin >> d;
cout <<"Enter year number";
cin >> y;
cout<<"Original Date: "<<m<<"/"<<d<<"/"<<y;
set_date(d, m, y);
skip_days(63);
day_counter = 0;
/* after this call next_day each day */
print_date();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.