<---Back
#include < stdio.h >
#include < iostream.h >

int get_date(int, int, int);
int calc_year(int); //Our new function, it will take one int argument.

int main()
{

int m, day, year; //month, day and year

  cout<< "\nEnter any date after 1900 as three sets of numbers.\n";
  cout<< "(month, day, year). Example: 7 29 1971 for July 29, 1971\n";
  cout<<"Entering invalid dates will exit the program.\n";
  cout<<"Enter date: ";
  cin>>m>>day>>year; //Take in our three variables

while((m != 13)&(day !=32)&(year != 0000)){
   //Invalid dates exit the loop

   get_date(m, day, year); //Our function call
}
  cout<<"\nYou entered an invalid date! End Program!" << endl;

return 0;
}


//get_date function(so far).
int get_date(int the_m, int the_day, int the_year){

//initial values for months of the year
  int jan = 0, febr = 31, mar = 59, apr = 90,
  may = 120, june = 151, july = 181, aug = 212,
  sept = 243, octo = 273, nov = 304, dec = 334;

  int the_month, the_date; //Variables used in calculations

if(3 > the_m){
    if(2 == the_m){
    the_date = febr + the_day;
    }
    else{
      if(1 == the_m){
      the_date = jan + the_day;
      }
    }
    }
    else{

   switch(the_m){
     case 3: the_month = mar; break;
     case 4: the_month = apr; break;
     case 5: the_month = may; break;
     case 6: the_month = june; break;
     case 7: the_month = july; break;
     case 8: the_month = aug; break;
     case 9: the_month = sept; break;
     case 10: the_month = octo; break;
     case 11: the_month = nov; break;
     case 12: the_month = dec; break;
   }
  if((0 == the_year%4) &! (0 == the_year%100)){
    the_date = the_day + (the_month + 1);
  }
    else{
      if(0 == the_year%400){
        the_date = the_day + (the_month + 1);
      }
      else{
        the_date = the_day + the_month;
      }
    }
 }

int first_day;//We declare a new int which will be used later

first_day = calc_year(the_year); /*This is our new function call. We are passing the_year to the new function, and assigning the return value to first_day*/

return 0;
}


//Our new function doesn't do anything yet.
int calc_year(int the_year){

int count_day = 0;

return count_day;
}



Now we're just going to focus on the new calc_year() function which will, when finished, tell us what the first day of a given year was/is.
//Just the new function
int calc_year(int the_year){

int count_day = 0; //start on Monday

//1900 started on a Monday, so don't enter the loop

for(int i = 1901; i <= the_year; i++){
  count_day++;//This is our day of the week.
    if(count_day >= 7){
     count_day = count_day - 7;
  }//reset at the end of each week

 }
return count_day;
}

What have we done here? First of all, we're only going to worry about years after 1899. If you want to alter the program to handle earlier years it is easy to do so. January 1, 1900 fell on a Monday so this is where we start counting from. If the user enters 1900 we don't need to enter the calculation loop because the answer to our question is already there, so we pass that variable back to the previous function. If the year is later, we enter the loop.

Inside the loop we are starting the counter at 1901 and counting up to the year entered. If the user enters 1984, the loop will cycle 83 times.

Then, we increment the day of the week with count_day++ for each year(each time we pass through the loop).

The if( ) statement then sends us back to the begining of the week each time we reach the end of the week.

This is great, except that we need to again adjust for leapyears. We will use pretty much the same method used earlier in the program. Why do we have to make the leapyear adjustment here too? Because each year begins on the week day after the first day of the previous year, except years following leapyears. The first day of the leapyear itself is not changed.

//Just the new function
int calc_year(int the_year){

int count_day = 0; //start on Monday

//1900 started on a Monday, so don't enter the loop

for(int i = 1901; i <= the_year; i++){
  count_day++;//This is our day of the week.
    if(count_day >= 7){
     count_day = count_day - 7;}
  //reset at the end of each week

  if((0 == ((i-1)%4)) &! (0 == ((i-1)%100))){
   count_day = count_day + 1; //adjust for leapyear
   }
   else{
    if(0 == ((i-1)%400)){
     count_day = count_day + 1;}
    }

//Every 4 years is a leapyear, i.e., years divisible by 4.
//Years divisible by 100 are not leapyears, this corrects
//For the fact that a leapday is not actually a full day.
//Years divisible by 400 are leapyears.
//Example: 1900, not a leapyear. 2000 a leapyear

  }//close for loop

return count_day;
}

Our function now makes adjustments for leapyears. We still need to make one more correction because our for( ) loop is not perfect. We start counting the days of the week at 0 for Monday. That means that 6 is Sunday. What happens when we count to 7? 7 would Monday again, but our program doesn't know that.
//Just the new function
int calc_year(int the_year){

int count_day = 0; //start on Monday

//1900 started on a Monday, so don't enter the loop

for(int i = 1901; i <= the_year; i++){
  count_day++;//This is our day of the week.
    if(count_day >= 7){
     count_day = count_day - 7;}
  //reset at the end of each week

  if((0 == ((i-1)%4)) &! (0 == ((i-1)%100))){
   count_day = count_day + 1; //adjust for leapyear
   }
   else{
    if(0 == ((i-1)%400)){
     count_day = count_day + 1;}
    }

//Every 4 years is a leapyear, i.e., years divisible by 4.
//Years divisible by 100 are not leapyears, this corrects
//For the fact that a leapday is not actually a full day.
//Years divisible by 400 are leapyears.
//Example: 1900, not a leapyear. 2000 a leapyear

  }//close for loop

if(count_day == 7){
count_day = 0;} //Make the day after Sunday (6 + 1) become Monday

return count_day;
}

Now we return to our first function to finish it. Our calc_year( ) function returns the day of the week for the first day of a given year. Let's use that number to get our final solution. Once again we will just look at the get_date( ) function
//get_date function(so far).
int get_date(int the_m, int the_day, int the_year){

//initial values for months of the year
  int jan = 0, febr = 31, mar = 59, apr = 90,
  may = 120, june = 151, july = 181, aug = 212,
  sept = 243, octo = 273, nov = 304, dec = 334;

  int the_month, the_date; //Variables used in calculations

if(3 > the_m){
    if(2 == the_m){
    the_date = febr + the_day;
    }
    else{
      if(1 == the_m){
      the_date = jan + the_day;
      }
    }
    }
    else{

   switch(the_m){
     case 3: the_month = mar; break;
     case 4: the_month = apr; break;
     case 5: the_month = may; break;
     case 6: the_month = june; break;
     case 7: the_month = july; break;
     case 8: the_month = aug; break;
     case 9: the_month = sept; break;
     case 10: the_month = octo; break;
     case 11: the_month = nov; break;
     case 12: the_month = dec; break;
   }
  if((0 == the_year%4) &! (0 == the_year%100)){
    the_date = the_day + (the_month + 1);
  }
    else{
      if(0 == the_year%400){
        the_date = the_day + (the_month + 1);
      }
      else{
        the_date = the_day + the_month;
      }
    }
 }

int first_day;

first_day = calc_year(the_year); //Remeber that calc_year returns count_day.

int this_day; //Another int declaration for calculation.
this_day = the_date + first_day; //We add the return value(count_day) to the_date and assign the sum to this_day.

//In this while( ) loop we count backwards by decrementing until we reach the day we're looking for.
while(this_day >= 7){
this_day = this_day - 7;
}

return 0;
}
Like our second function, the calculation is not perfect we need to adjust in the other direction this time. The weekdays are counted from 0, our while( ) loop above sometimes produces negative numbers for Sunday.
//get_date function(so far).
int get_date(int the_m, int the_day, int the_year){

//initial values for months of the year
  int jan = 0, febr = 31, mar = 59, apr = 90,
  may = 120, june = 151, july = 181, aug = 212,
  sept = 243, octo = 273, nov = 304, dec = 334;

  int the_month, the_date; //Variables used in calculations

if(3 > the_m){
    if(2 == the_m){
    the_date = febr + the_day;
    }
    else{
      if(1 == the_m){
      the_date = jan + the_day;
      }
    }
    }
    else{

   switch(the_m){
     case 3: the_month = mar; break;
     case 4: the_month = apr; break;
     case 5: the_month = may; break;
     case 6: the_month = june; break;
     case 7: the_month = july; break;
     case 8: the_month = aug; break;
     case 9: the_month = sept; break;
     case 10: the_month = octo; break;
     case 11: the_month = nov; break;
     case 12: the_month = dec; break;
   }
  if((0 == the_year%4) &! (0 == the_year%100)){
    the_date = the_day + (the_month + 1);
  }
    else{
      if(0 == the_year%400){
        the_date = the_day + (the_month + 1);
      }
      else{
        the_date = the_day + the_month;
      }
    }
 }

int first_day;

first_day = calc_year(the_year); //Remeber that calc_year returns count_day.

int this_day; //Another int declaration for calculation.
this_day = the_date + first_day; //We add the return value(count_day) to the_date and assign the sum to this_day.

//In this while( ) loop we count backwards by decrementing until we reach the day we're looking for.
while(this_day >= 7){
this_day = this_day - 7;
}

this_day = this_day - 1;
if(this_day == -1){
  this_day = 6;
}//Make the day before Monday (0 - 1) be Sunday

return this_day;
}
We're done! Let's look at the final program with some notes and output polishing.

Finished Program

#include
#include


int get_date(int, int, int);
int calc_year(int);


int main(){

int m, day, year, our_day;

cout<<"\nEnter any date after 1900 as three sets of numbers.\n";
cout<<"(month, day, year). Example: 7 29 1971 for July 29, 1971\n";
cout<<"Entering invalid dates will exit the program.\n";
cout<<"Enter date: ";
cin>>m>>day>>year;

while((m != 13)&(day !=32)&(year != 0000)){
//Invalid dates exits the loop

our_day = get_date(m, day, year);
//Passes the three values to get_date function


//Turns a numberical month into a nice
//named month when printed

switch(m){
case 1: cout<<"January "; break;
case 2: cout<<"February "; break;
case 3: cout<<"March "; break;
case 4: cout<<"April "; break;
case 5: cout<<"May "; break;
case 6: cout<<"June "; break;
case 7: cout<<"July "; break;
case 8: cout<<"August "; break;
case 9: cout<<"September "; break;
case 10: cout<<"October "; break;
case 11: cout<<"November "; break;
case 12: cout<<"December "; break;
}


cout<<" "<
switch(our_day){
case 0: cout<<"Monday\n"; break;
case 1: cout<<"Tuesday\n"; break;
case 2: cout<<"Wednesday\n"; break;
case 3: cout<<"Thursday\n"; break;
case 4: cout<<"Friday\n"; break;
case 5: cout<<"Saturday\n"; break;
case 6: cout<<"Sunday\n"; break;
}

cout<<"\n\nEnter date: ";
cin>>m>>day>>year;
}
cout<<"\nYou entered an invalid date! End Program!"< return 0;
}


//-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
//GET_DATE function. This function takes in the month, day and year
//and using the month and day, fugures out the numerical value of the
//day, i.e., January 1 is day 1, December 31 is day 365.
//The function uses the year to adjust for leapyears, adding a day
//on leapyears if the the date is after February.

int get_date(int the_m, int the_day, int the_year){

//initial values for months of the year

int jan = 0, febr = 31, mar = 59, apr = 90,
may = 120, june = 151, july = 181, aug = 212,
sept = 243, octo = 273, nov = 304, dec = 334;

int the_month, the_date;

if(3 > the_m){

if(2 == the_m){
the_date = febr + the_day;
}
else{
if(1 == the_m){
the_date = jan + the_day;
}
}
}
else{
switch(the_m){
case 3: the_month = mar; break;
case 4: the_month = apr; break;
case 5: the_month = may; break;
case 6: the_month = june; break;
case 7: the_month = july; break;
case 8: the_month = aug; break;
case 9: the_month = sept; break;
case 10: the_month = octo; break;
case 11: the_month = nov; break;
case 12: the_month = dec; break;
}

if((0 == the_year%4) &! (0 == the_year%100)){
the_date = the_day + (the_month + 1);
}
else{
if(0 == the_year%400){
the_date = the_day + (the_month + 1);
}
else{
the_date = the_day + the_month;
}
}
}


int first_day;
first_day = calc_year(the_year);

int this_day;
this_day = the_date + first_day;

while(this_day >= 7){
this_day = this_day - 7;
}

this_day = this_day - 1;
if(this_day == -1){
this_day = 6;
}//Make the day before Monday (0 - 1) be Sunday


return this_day;
}




//-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-
//CALC_YEAR figures out the first day of each year
//and reports it back

int calc_year(int the_year){

int count_day = 0; //start on Monday

//1900 started on a Monday, so don't enter the loop

for(int i = 1901; i <= the_year; i++){
count_day++;
if(count_day >= 7){
count_day = count_day - 7;
}//reset at the end of each week

if((0 == ((i-1)%4)) &! (0 == ((i-1)%100))){
count_day = count_day + 1; //adjust for leapyear
}
else{
if(0 == ((i-1)%400)){
count_day = count_day + 1;}
}

//Every 4 years is a leapyear, i.e., years divisible by 4.
//Years divisible by 100 are not leapyears, this corrects
//For the fact that a leapday is not actually a full day.
//Years divisible by 400 are leapyears.
//Example: 1900, not a leapyear. 2000 a leapyear

}//close for loop

if(count_day == 7){
count_day = 0;} //Make the day after Sunday (6 + 1) become Monday


return count_day;
}