Syvum Home Page

Home > Discussions > For all activities

Discussion topic: Contributed Answer/Explanation to Q. 3

Viewing replies        View entire discussion

To go to the homepage of this topic, click here.
Home: rushilpaulOriginal MessageReply  
Subject: Contributed Answer/Explanation to Q. 1
   
   #include <iostream>
   #include <sstream>
   using namespace std;
   
   bool checkleap(int y);
   string printdate(int n, int y);
   
   bool checkleap(int y)
   {
       return y % 400 == 0 || (y % 4 == 0 && y % 100 != 0);
   }
   
   string printdate(int n, int y)
   {
       int year = y, month=1, days=n;
       y = checkleap(y) ? 1 : 0;
               
       while( n > 0)
       {
           if(month >= 13)
           {
               y = checkleap(++year) ? 1 : 0;
               month = 1;
           }
           days = n;
           if(month == 2) if(y==1) n -= 29;
                          else n -= 28;
           else if(month <= 7) if(month % 2 == 1) n -= 31;
                               else n -= 30;
           else
           {
               if(month % 2 == 0) n -= 31;
               else n -= 30;
           }
           month++;
       }
       string months[] = { "January", "February",
   "March", "April", "May", "June",
   "July", "August", "September",
   "October", "November", "December" };
       string app = "th";
       if( days % 10 == 1) app = "st";
       else if(days % 10 == 2) app = "nd";
       else if(days % 10 == 3) app = "rd";
       stringstream val;
       val << days << app << " " <<
   months[month-2] << " " << year;
       return val.str();
   }
   
   int main()
   {
       int n,y,after;
       cout << "Enter the number of days, year and additional
   number of days: ";
       cin >> n >> y >> after;
       bool err = n < 1 || n > 366;
       if(!checkleap(y)) if( n > 365) err = true;
       if(err) { cout << "Incorrect input." << endl;
   return 1; }
       cout << "Current Date: \t" << printdate(n,y)
   << endl << "Date after " << after <<
   " days: " << printdate(n+after,y) << endl;
       return 0;
   }

Posted at: Tue Jan 17 20:04:33 2012 (GMT)

Page 1 of 1
From: rushilpaulReply 1 of 3Reply
Subject: Contributed Answer/Explanation to Q. 1
   
   
   #include 
   #include 
   using namespace std;
   
   bool checkleap(int y);
   string printdate(int n, int y);
   
   bool checkleap(int y)
   {
   return y % 400 == 0 || (y % 4 == 0 && y % 100 != 0);
   }
   
   string printdate(int n, int y)
   {
   int year = y, month=1, days=n;
   y = checkleap(y) ? 1 : 0;
   
   while( n > 0)
   {
   if(month > 12)
   {
   y = checkleap(++year) ? 1 : 0;
   month = 1;
   }
   days = n;
   if(month == 2) if(y==1) n -= 29;
   else n -= 28;
   else if(month <= 7) if(month % 2 == 1) n -= 31;
   else n -= 30;
   else
   {
   if(month % 2 == 0) n -= 31;
   else n -= 30;
   }
   month++;
   }
   string months[] = { "January", "February",
   "March", "April", "May", 
   "June", "July", "August",
   "September", "October", "November",
   "December"
   };
   string app = "th";
   if( days % 10 == 1) app = "st";
   else if(days % 10 == 2) app = "nd";
   else if(days % 10 == 3) app = "rd";
   stringstream val;
   val << days << app << " " <<
   months[month-2] << " " << year;
   return val.str();
   }
   
   int main()
   {
   int n,y,after;
   cout << "Enter the number of days, year and additional number of
   days: ";
   cin >> n >> y >> after;
   bool err = n < 1 || n > 366;
   if(!checkleap(y)) if( n > 365) err = true;
   if(err) { cout << "Incorrect input." << endl; return
   1; }
   cout << "Current Date: \t" << printdate(n,y) <<
   endl 
   << "Date after " << after << " days: "
   << 
   printdate(n+after,y) << endl;
   return 0;
   }  // end of program 
   
   
    
   
   
   -----------------------------ALGORITHM--------------------------------------
   --------
   
   
   The first seven months of the year have 31 and 30 days alternately with an
   exception of February.
   
   
   That means for all m <= 7, if m is divisble by 2, then no. of days is 30,
   else 31. and if m = 2, then check for leap year.
   
   
   This is very simple: (y is divisble by 4 but not by 100) or (y is divisible
   by 400).
   
   
   Continuing, months from 8 to 12  have 31 and 30 days alternately. Again 
   check if m is divisible by 2. If yes then its 31 else 30. (this time it 
   gets reversed since Jul and Aug, both have 31 days)
   
   
   Keep on subtracting the number of days in each month from variable days 
   (which was the input), and check whether the number of remaining days 
   are <= 0.
   
   
   If yes, then break out of the loop and the last positive value of 
   variable "days" will be the date. (assign another variable the
   value of n
   just before changing value of "days")
   
   
   For months, keep a counter which starts from 1.
   
   
   As soon as months reaches a value > 12, assign it 1, increment year, and
   check for leap year again. 
   
   
    
   
   
   Put all of this in a function and call it twice.
   
   
   The second time, call it giving number of days = earlier no. of days + 
   second inputted no. of days. i.e. printdate( days + after )

Posted at: Tue Jan 17 20:52:21 2012 (GMT)

From: rushilpaulReply 2 of 3Reply
Subject: Contributed Answer/Explanation to Q. 1
   
   
   #include <iostream>
   #include <sstream>
   using namespace std;
   
   bool checkleap(int y);
   string printdate(int n, int y);
   
   bool checkleap(int y)
   {
   return y % 400 == 0 || (y % 4 == 0 && y % 100 != 0);
   }
   
   string printdate(int n, int y)
   {
   int year = y, month=1, days=n;
   y = checkleap(y) ? 1 : 0;
   
   while( n > 0)
   {
   if(month > 12)
   {
   y = checkleap(++year) ? 1 : 0;
   month = 1;
   }
   days = n;
   if(month == 2) if(y==1) n -= 29;
   else n -= 28;
   else if(month <= 7) if(month % 2 == 1) n -= 31;
   else n -= 30;
   else
   {
   if(month % 2 == 0) n -= 31;
   else n -= 30;
   }
   month++;
   }
   string months[] = { "January", "February",
   "March", "April", "May", 
   "June", "July", "August",
   "September", "October", "November",
   "December"
   };
   string app = "th";
   if( days % 10 == 1) app = "st";
   else if(days % 10 == 2) app = "nd";
   else if(days % 10 == 3) app = "rd";
   stringstream val;
   val << days << app << " " <<
   months[month-2] << " " << year;
   return val.str();
   }
   
   int main()
   {
   int n,y,after;
   cout << "Enter the number of days, year and additional number of
   days: ";
   cin >> n >> y >> after;
   bool err = n < 1 || n > 366;
   if(!checkleap(y)) if( n > 365) err = true;
   if(err) { cout << "Incorrect input." << endl; return
   1; }
   cout << "Current Date: \t" << printdate(n,y) <<
   endl 
   << "Date after " << after << " days: "
   << 
   printdate(n+after,y) << endl;
   return 0;
   }  // end of program 
   
   
    
   
   
   -----------------------------ALGORITHM--------------------------------------
   --------
   
   
   The first seven months of the year have 31 and 30 days alternately with an
   exception of February.
   
   
   That means for all m <= 7, if m is divisble by 2, then no. of days is 30,
   else 31. and if m = 2, then check for leap year.
   
   
   This is very simple: (y is divisble by 4 but not by 100) or (y is divisible
   by 400).
   
   
   Continuing, months from 8 to 12  have 31 and 30 days alternately. Again 
   check if m is divisible by 2. If yes then its 31 else 30. (this time it 
   gets reversed since Jul and Aug, both have 31 days)
   
   
   Keep on subtracting the number of days in each month from variable days 
   (which was the input), and check whether the number of remaining days 
   are <= 0.
   
   
   If yes, then break out of the loop and the last positive value of 
   variable "days" will be the date. (assign another variable the
   value of n
   just before changing value of "days")
   
   
   For months, keep a counter which starts from 1.
   
   
   As soon as months reaches a value > 12, assign it 1, increment year, and
   check for leap year again. 
   
   
    
   
   
   Put all of this in a function and call it twice.
   
   
   The second time, call it giving number of days = earlier no. of days + 
   second inputted no. of days. i.e. printdate( days + after )

Posted at: Tue Jan 17 20:55:23 2012 (GMT)

From: rushilpaulReply 3 of 3Reply
Subject: Contributed Answer/Explanation to Q. 1
   
   
   #include "iostream"
   
   
   #include "sstream"
   using namespace std;
   
   bool checkleap(int y);
   string printdate(int n, int y);
   
   bool checkleap(int y)
   {
   return y % 400 == 0 || (y % 4 == 0 && y % 100 != 0);
   }
   
   string printdate(int n, int y)
   {
   int year = y, month=1, days=n;
   y = checkleap(y) ? 1 : 0;
   
   while( n > 0)
   {
   if(month > 12)
   {
   y = checkleap(++year) ? 1 : 0;
   month = 1;
   }
   days = n;
   if(month == 2) if(y==1) n -= 29;
   else n -= 28;
   else if(month <= 7) if(month % 2 == 1) n -= 31;
   else n -= 30;
   else
   {
   if(month % 2 == 0) n -= 31;
   else n -= 30;
   }
   month++;
   }
   string months[] = { "January", "February",
   "March", "April", "May", 
   "June", "July", "August",
   "September", "October", "November",
   "December"
   };
   string app = "th";
   if( days % 10 == 1) app = "st";
   else if(days % 10 == 2) app = "nd";
   else if(days % 10 == 3) app = "rd";
   stringstream val;
   val << days << app << " " <<
   months[month-2] << " " << year;
   return val.str();
   }
   
   int main()
   {
   int n,y,after;
   cout << "Enter the number of days, year and additional number of
   days: ";
   cin >> n >> y >> after;
   bool err = n < 1 || n > 366;
   if(!checkleap(y)) if( n > 365) err = true;
   if(err) { cout << "Incorrect input." << endl; return
   1; }
   cout << "Current Date: \t" << printdate(n,y) <<
   endl 
   << "Date after " << after << " days: "
   << 
   printdate(n+after,y) << endl;
   return 0;
   }  // end of program 
   
   
    
   
   
   -----------------------------ALGORITHM--------------------------------------
   --------
   
   
   The first seven months of the year have 31 and 30 days alternately with an
   exception of February.
   
   
   That means for all m <= 7, if m is divisble by 2, then no. of days is 30,
   else 31. and if m = 2, then check for leap year.
   
   
   This is very simple: (y is divisble by 4 but not by 100) or (y is divisible
   by 400).
   
   
   Continuing, months from 8 to 12  have 31 and 30 days alternately. Again 
   check if m is divisible by 2. If yes then its 31 else 30. (this time it 
   gets reversed since Jul and Aug, both have 31 days)
   
   
   Keep on subtracting the number of days in each month from variable days 
   (which was the input), and check whether the number of remaining days 
   are <= 0.
   
   
   If yes, then break out of the loop and the last positive value of 
   variable "days" will be the date. (assign another variable the
   value of n
   just before changing value of "days")
   
   
   For months, keep a counter which starts from 1.
   
   
   As soon as months reaches a value > 12, assign it 1, increment year, and
   check for leap year again. 
   
   
    
   
   
   Put all of this in a function and call it twice.
   
   
   The second time, call it giving number of days = earlier no. of days + 
   second inputted no. of days. i.e. printdate( days + after )

Posted at: Tue Jan 17 20:56:49 2012 (GMT)

Page 1 of 1

To post to this forum, you must be signed in as a Syvum member. Please sign in / register as a member.

Contact Info © 1999-2024 Syvum Technologies Inc. Privacy Policy Disclaimer and Copyright