Beginners Task #1

Online C++ programming contests.

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard, raimo

Postby Sky Island » Fri Jun 18, 2004 6:05 am

The challenge sounds good to me I'll write some code as soon as I can.
--------------------------------------------------------
These walls hold the world's knowledge
-Baldur's Gate, Candlekeep
(Its's a big libary)
--------------------------------------------------------
Sky Island
 
Posts: 22
Joined: Thu Jun 17, 2004 9:59 am

Postby Sky Island » Fri Jun 18, 2004 6:16 am

Sorry, I Didn't notice the contest was over never mind.
--------------------------------------------------------
These walls hold the world's knowledge
-Baldur's Gate, Candlekeep
(Its's a big libary)
--------------------------------------------------------
Sky Island
 
Posts: 22
Joined: Thu Jun 17, 2004 9:59 am

Postby Guest » Fri Jun 18, 2004 6:23 am

Sky Island wrote:Sorry, I Didn't notice the contest was over never mind.

It is not over this is a task not contest.
Guest
 

Postby Sky Island » Fri Jun 18, 2004 9:50 am

Cool I'll write some code then
--------------------------------------------------------
These walls hold the world's knowledge
-Baldur's Gate, Candlekeep
(Its's a big libary)
--------------------------------------------------------
Sky Island
 
Posts: 22
Joined: Thu Jun 17, 2004 9:59 am

Postby Guest » Fri Jun 18, 2004 10:28 am

Both the Beginners Tasks were in the beginners forum. They were moved here so that they did not get lost in the beginners forum and so they would be available for anyone who had not yet done them and wanted to. Also anyone can make a Beginners Tasks. You would just have to use the subject Beginners Tasks #3 as the subject and post it in the beginners forum and a moderator will make it sticky for a few weeks usually. So if you have any ideas you might want to kick them around first in the open discussion.
Guest
 

Postby Beer Hunter » Sat Jun 19, 2004 1:32 am

I suddenly decided to give this a go.
Code: Select all
#include <boost/date_time/gregorian/gregorian.hpp>
#include <iostream>

int main()
{
    using namespace boost::gregorian;
   
    date mydate(2003, 12, 31);
    int numdays;
    std::cin >> numdays;
    mydate = mydate + date_duration(numdays);
    std::cout << mydate.day_of_week().as_long_string() << ", "
              << mydate.day().as_number() << " "
              << mydate.month().as_long_string() << " "
              << mydate.year() << std::endl;
}
Mmmm.
User avatar
Beer Hunter
 
Posts: 912
Joined: Sat Dec 13, 2003 7:12 pm
Location: Australia

Postby Sky Island » Mon Jun 21, 2004 8:51 am

//It's late but here it is:

//task1.cpp
#include <iostream>
using namespace std;

int invalid();
int error();

int main()
{
int days;
int T1days = 0;
int T2days = 0;
int T3days = 0;
int T4days = 0;
int T5days = 0;
int T6days = 0;
int T7days = 0;
loop:
cout << "Enter a number(between 1 and 366)\n";
cin >> days;
if (days <= 0 || days > 366){invalid(); goto loop;}
cout << "Calculatating...\n";
// Jan 1st of 2004 was a Tuesday...
// Sun, Mon, Tues, Wens, Thurs, Fri, Sat...
// mouths: 1January, 2Febuary, 3March , 4April, 5May, 6June, 7July, 8August, 9Septeber,
// 10october, 11november, 12december
T1days = 1;
while (T1days < 366)
{
if (T1days == days)
{
cout << " Tuesday ";
}
T1days = T1days + 7;
}
//Test2
T2days = 2;
while (T2days < 366)
{
if (T2days == days)
{
cout << " Wensday ";
}
T2days = T2days + 7;
}
//Test3
T3days = 3;
while (T3days < 366)
{
if (T3days == days)
{
cout << " Thursday ";
}
T3days = T3days + 7;
}
//Test4
T4days = 4;
while (T4days < 366)
{
if (T4days == days)
{
cout << " Friday ";
}
T4days = T4days + 7;
}
while (T5days < 366)
{
if (T5days == days)
{
cout << " Saturday ";
}
T5days = T5days + 7;
}
T6days = 6;
while (T5days < 366)
{
if (T6days == days)
{
cout << " Sunday ";
}
T6days = T6days + 7;
}
T7days = 7;
while (T7days < 366)
{
if (T7days == days)
{
cout << " Monday ";
}
T7days = T7days + 7;
}
cout << "the ";
if (days <= 31 && days >= 1){if (days == 1){cout << "1st";}else cout << days << "rd "; cout << " of " << "January\n";}
if (days <= 60 && days > 31){if (days - 31 == 1){cout << "1st";}else cout << (days - 31) << "rd "; cout << " of " << "Febuary\n";}
if (days <= 91 && days > 60){if (days - 60 == 1){cout << "1st";}else cout << (days - 60) << "rd "; cout << " of " << "March\n";}
if (days <= 121 && days > 91){if (days - 91 == 1){cout << "1st";}else cout << (days - 91) << "rd "; cout << " of " << "April\n";}
if (days <= 152 && days > 121){if (days - 121 == 1){cout << "1st";}else cout << (days - 121) << "rd "; cout << " of " << "May\n";}
if (days <= 182 && days > 152){if (days - 152 == 1){cout << "1st";}else cout << (days - 152) << "rd "; cout << " of " << "June\n";}
if (days <= 213 && days > 182){if (days - 182 == 1){cout << "1st";}else cout << (days - 182) << "rd "; cout << " of " << "July\n";}
if (days <= 244 && days > 213){if (days - 213 == 1){cout << "1st";}else cout << (days - 213) << "rd "; cout << " of " << "August\n";}
if (days <= 274 && days > 244){if (days - 244 == 1){cout << "1st";}else cout << (days - 244) << "rd "; cout << " of " << "September\n";}
if (days <= 305 && days > 274){if (days - 274 == 1){cout << "1st";}else cout << (days - 274) << "rd "; cout << " of " << "October\n";}
if (days <= 335 && days > 305){if (days - 305 == 1){cout << "1st";}else cout << (days - 305) << "rd "; cout << " of " << "November\n";}
if (days <= 366 && days > 335){if (days - 335 == 1){cout << "1st";}else cout << (days - 335) << "rd "; cout << " of " << "December\n";}
system("PAUSE");
return 0;
}

int invalid(){cout << "Invalid number or entry\n";}
int error(){cout << "Something is not working right!\n";}
Last edited by Sky Island on Mon Jun 28, 2004 2:48 pm, edited 1 time in total.
--------------------------------------------------------
These walls hold the world's knowledge
-Baldur's Gate, Candlekeep
(Its's a big libary)
--------------------------------------------------------
Sky Island
 
Posts: 22
Joined: Thu Jun 17, 2004 9:59 am

late

Postby darkmaster » Sat Jun 26, 2004 11:09 pm

Hello

I know it's kinda late but i had nothing to do the other day so i worked on my code a little bit and added a loop that ask you if you wanna repeat the process with another number and also added some code that couts the name of the day :wink:

Code: Select all
#include <iostream>
using namespace std;

void dname(int days)
{   
int day = days%7;
switch(day)
   {
     case(0): cout << "Wednesday "; break;
     case(1): cout << "Thursday ";  break;
     case(2): cout << "Friday "; break;
     case(3): cout << "Saturday "; break;
     case(4): cout << "Sunday "; break;
     case(5): cout << "Monday "; break;   
     default: cout << "Tuesday ";
   }
}

void suffix(int nday)
{
   switch(nday)
   {
      case (1) : case (21) : case (31) : cout << "st"; break;                 
      case (2) : case (22) : cout << "nd"; break;             
      case (3) : case (23) : cout << "rd"; break;               
      default  : cout << "th";               
   }
}

void date(int dayn)   
{
   int month[] = {31 , 60 ,91 , 121 , 152 , 182 , 213 , 244 , 274 , 305 , 335 , 366} ;

     int febd = dayn - month[0];
   int mard = dayn - month[1];
   int aprd = dayn - month[2];
   int mayd = dayn - month[3];
   int jund = dayn - month[4];   
   int juld = dayn - month[5]; 
   int augd = dayn - month[6];
   int sepd = dayn - month[7];
   int octd = dayn - month[8];
   int novd = dayn - month[9];
   int decd = dayn - month[10];   

if (dayn <= month[0]) //January
   {
   cout << dayn;
   suffix(dayn);
   cout << ", January 2004 ";
   }   
else if ((dayn > month[0]) && (dayn <= month[1])) //February
   {
   cout << (febd);
   suffix(febd);
   cout << ", February 2004 ";
   }
else if ((dayn > month[1]) && (dayn <= month[2])) //March
   {
   cout << (mard);
   suffix(mard);
   cout << ", March 2004 ";
   }
else if ((dayn > month[2]) && (dayn <= month[3])) //April
   {
   cout << (aprd);
   suffix(aprd);
   cout << ", April 2004 ";
   }
else if ((dayn > month[3]) && (dayn <= month[4])) //May
   {
   cout << (mayd);
   suffix(mayd);
   cout << ", May 2004 ";
   }
else if ((dayn > month[4]) && (dayn <= month[5])) //June
   {
   cout << (jund);
   suffix(jund);
   cout << ", June 2004 ";
   }
else if ((dayn > month[5]) && (dayn <= month[6])) //July
   {
   cout << (juld);
   suffix(juld);
   cout << ", July 2004 ";
   }
else if ((dayn > month[6]) && (dayn <= month[7])) //August
   {
   cout << (augd);
   suffix(augd);
   cout << ", August 2004 ";
   }
else if ((dayn > month[7]) && (dayn <= month[8])) //September
   {
   cout << (sepd);
   suffix(sepd);
   cout << ", September 2004 ";
   }
else if ((dayn > month[8]) && (dayn <= month[9])) //October
   {
   cout << (octd);
   suffix(octd);
   cout << ", October 2004 ";
   }
else if ((dayn > month[9]) && (dayn <= month[10])) //November
   {
   cout << (novd);
   suffix(novd);
   cout << ", November 2004 ";
   }
else if ( (dayn > month[10]) && (dayn <= month[11]) ) //December
   {
   cout << (decd);
   suffix(decd);
   cout << ", December 2004 ";
   }
}

int main()
{
   
bool loop = false;

   while(loop == false)
   { 
      
   int day;
   cout << "Enter a number between 1 and 366: ";
   cin >> day;
   if ((day >= 1) && (day <= 366))
   {   
   dname(day);
   date(day);   
   }
   else
   {
         cout << "Please enter a valid value";   
   }
 
cout << "\n\nWant to try another number? y/n" << endl;
   
char response;
cin >> response;
switch(response) {
      case 'y' :
                 break;
      default :  cout << "Quitting..." << endl;
                 loop = true;
                 cout << endl;
            }
   }
   return(0);
}
User avatar
darkmaster
 
Posts: 27
Joined: Mon Mar 29, 2004 6:11 pm

my way

Postby samurai_jack » Fri May 27, 2005 8:12 am

Code: Select all
#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
   int year[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};
   int i;
   cout << "current date is 01.01.2004\n";
   cout << "enter i\n";
   cin >> i;
   if(i>366){cout << "bad number!\n" ; return 0;}
   int r;
   for(r=1;r<13;r++)
   {
      if(i-year[r]>0){i=i-year[r];}
      else break;
   }
   cout << "the date you are searching for is:\n" << i << "." << r << "." << "2004\n" ;
   return 0;
}


not a big deal but it works 8)
is This real?
samurai_jack
 
Posts: 2
Joined: Wed Nov 10, 2004 5:23 pm
Location: Bulgaria,Blagoevgrad

My twopenith

Postby Fossil » Thu Oct 06, 2005 12:15 pm

[syntax="cpp"]#include <iostream>

using std::cout; //because i cant type very fast
using std::cin;

int main()
{
int inp=1,day,date,month; //input number,day calc variables

while (inp!=0) //main loop
{
cout<<"Please type in a number from 1 to 366.\n";
cin>>inp;
//got the right number?
if (inp<1 || inp>366) {cout<<"Cant you read!!\n"; continue;}

day=inp%7; //What day is it
switch (day)
{
case 0: cout<<"Wednesday"; break;
case 1: cout<<"Thursday"; break;
case 2: cout<<"Friday"; break;
case 3: cout<<"Saturday"; break;
case 4: cout<<"Sunday"; break;
case 5: cout<<"Monday"; break;
case 6: cout<<"Tuesday"; break;
default : cout<<"error in case calcs!!"; return 0;
}

cout<<" the ";
//month if then routines
if (inp<=31) {date=inp;month=1;}
if (inp>=32 && inp<=60) {date=inp-31;month=2;}
if (inp>=61 && inp<=91) {date=inp-60;month=3;}
if (inp>=92 && inp<=121) {date=inp-91;month=4;}
if (inp>=122 && inp<=152) {date=inp-121;month=5;}
if (inp>=153 && inp<=182) {date=inp-152;month=6;}
if (inp>=183 && inp<=213) {date=inp-182;month=7;}
if (inp>=214 && inp<=244) {date=inp-213;month=8;}
if (inp>=245 && inp<=274) {date=inp-244;month=9;}
if (inp>=275 && inp<=305) {date=inp-274;month=10;}
if (inp>=306 && inp<=335) {date=inp-305;month=11;}
if (inp>=336 && inp<=366) {date=inp-335;month=12;}

cout<<date; //number part of date

if (date==11 || date==12 || date==13) {date=4;}
date=date%10;
switch (date) //sort the 1sts from the 5ths etc
{
case 1: cout<<"st "; break;
case 2: cout<<"nd "; break;
case 3: cout<<"rd "; break;
default : cout<<"th "; break;
}

switch (month) //which month
{
case 1: cout<<"of January "; break;
case 2: cout<<"of February "; break;
case 3: cout<<"of March "; break;
case 4: cout<<"of April "; break;
case 5: cout<<"of May "; break;
case 6: cout<<"of June "; break;
case 7: cout<<"of July "; break;
case 8: cout<<"of August "; break;
case 9: cout<<"of September "; break;
case 10: cout<<"of October "; break;
case 11: cout<<"of November "; break;
case 12: cout<<"of December "; break;
default : cout<<"error in months"; return 0;
}

cout<<"2004.\n";
cout<<"Zero to exit.\n";
} //do again?

return 0;
}[/syntax]

edit by Wizard: added syntax tags
Fossil
 
Posts: 4
Joined: Thu Oct 06, 2005 12:12 pm

Postby asa.rand » Mon Oct 30, 2006 2:16 pm

I decided to delete this and the following entries in favor of the 3rd one. It is the "complete" version.
Last edited by asa.rand on Fri Nov 03, 2006 6:20 am, edited 2 times in total.
asa.rand
 
Posts: 21
Joined: Thu Oct 26, 2006 2:21 pm
Location: California, USA

the latest version

Postby asa.rand » Wed Nov 01, 2006 4:37 pm

Here is revision 1.3. It is the complete version. All calculation errors have been corrected (so far as I can tell).

Code: Select all
// calendar.cpp : main project file.

/* I wrote this using Visual C++ 2005 Express Edition.
   Because I couldn't find the std namespace in VC++ 2005
   I was forced to use the System namespace. This means
   that there was no cout, cin, << or >>. Instead of the
   string data type I used the String class.

   After writing the program, I found that I could use
   <iostream> and namespace std after all. I still don't
   know what caused the error when I first tried to use
   them.

   I have corrected all of the calculation errors in this
   version. The program now correctly identifies the weekday
   and date of the given day number entered.

   I have elected to leave the .NET code in and commented as
   it is easier to copy and paste that way than it is to make
   a separate file and strip out all of the .NET code.
   
   This program is copyright 2006 by Wayne Campbell (asa.rand)
   It may not be sold, resold or included in any collection for
   sale or resale under any circumstances. */

/* Version Data
   
   Version 1.0 .......... October 27, 2006
      Initial Code Release
   Version 1.1 .......... November 1, 2006
      First Revision: Added Data Display
   Version 1.2 .......... November 2, 2006
      Second Revision: Corrected calculations in the Console version
     Replaced localtime reference with localtime_s in the std version
     Added Comments
   Version 1.3 .......... November 3, 2006
      Third Revision: Corrected calculation for last day/ first day
     in a leap year. This fixed all incorrect calculations in both
     System::Console and std versions.
          Added More Comments */

/* System::Console Compliant */
#include "stdafx.h"                  // must be included in my environment
// using namespace System;

/* std Compliant */
#include <string>
#include <iostream>
#include <iomanip>
#include <ctime>                  // doesn't matter if time.h is specified
using namespace std;

/* forward declarations */
bool GetCLS();
int GetYear();
int GetDayNum(bool, int);
void GetDate(bool, int, int);
// int GetEnd();                  // System::Console Compliant
string GetEnd();                  // std Compliant

// int main(array<String ^> ^args)      // System::Console Compliant
int main()                        // std Compliant
{
   bool firstTime = true, clearScreen;
   // int quit;                  // System::Console Compliant
   string quit;                  // std Compliant
   if (firstTime) clearScreen = GetCLS();

   do {
      if (clearScreen) {
         // Console::Clear(); }      // System::Console Compliant
         system("cls"); }         // std Compliant
      else {
         // Console::WriteLine(); }   // System::Console Compliant
         cout << endl; }            // std Compliant

      int targetYear = GetYear();
      int div4 = targetYear%4, div100 = targetYear%100, div400 = targetYear%400;
      bool targetIsLeap = (div4==0) || ((div100==0) && (div400==0));

      /* I comment the following 2 lines to use the for loops below */
      int targetDay = GetDayNum(targetIsLeap, targetYear);
      GetDate(targetIsLeap, targetYear, targetDay);

      /* I put this in to look at a year 10 days at a time
         System::Console Compliant
      for (int targetDay=1;targetDay<366;targetDay++) {
         GetDate(targetIsLeap, targetYear, targetDay);
         if (targetDay%10==0) {
            String^ c = Console::ReadLine(); } } */

      /* I put this in to look at a year 10 days at a time
         std Compliant
      for (int targetDay=1;targetDay<366;targetDay++) {
         GetDate(targetIsLeap, targetYear, targetDay);
         if (targetDay%10==0) {
            char c;
            cin >> c; } } */

      // Console::WriteLine();      // System::Console Compliant
      cout << endl;               // std Compliant
      quit = GetEnd();            // std Compliant
   // } while (quit!=0);            // System::Console Compliant
   } while (quit!="Y" && quit!="y");   // std Compliant
   return 0;
}

/* System::Console compliant
bool GetCLS()
{
   int CLS;
   Console::Write("Clear the screen between displays: 0 (Yes) 1 (No): ");
hereCLS:
   while (!Int32::TryParse(Console::ReadLine(), CLS)) {
      Console::Write("Must be 0 or 1, Clear the screen between displays: 0 (Yes) 1 (No): "); }

   if (CLS<0 || CLS>1) {
      Console::Write("Must be 0 or 1, Clear the screen between displays: 0 (Yes) 1 (No): ");
      goto hereCLS; }

   if (CLS==0) return true;
   else return false;
} */

/* std Compliant */
bool GetCLS()
{
   string CLS;
   cout << "Clear the screen between displays: (Y)es (N)o: ";
   cin >> CLS;

   while (CLS!="Y" && CLS!="y" && CLS!="N" && CLS!="n") {
      cout << "Must be Y/y or N/n, Clear the screen between displays: (Y)es (N)o: ";
      cin >> CLS; }

   if (CLS=="Y" || CLS=="y") return true;
   else return false;
}

/* System::Console compliant
int GetEnd()
{
   int quit;
   Console::Write("Quit: 0 (Yes) 1 (No): ");
hereQuit:
   while (!Int32::TryParse(Console::ReadLine(), quit)) {
      Console::Write("Must be 0 or 1, Quit: 0 (Yes) 1 (No): "); }

   if (quit<0 || quit>1) {
      Console::Write("Must be 0 or 1, quit: 0 (Yes) 1 (No): ");
      goto hereQuit; }

   return quit;
} */

/* std Compliant */
string GetEnd()
{
   string quit;
   cout << "Quit: (Y)es (N)o: ";
   cin >> quit;

   while (quit!="Y" && quit!="y" && quit!="N" && quit!="n") {
      cout << "Must be Y/y or N/n, Quit: (Y)es (N)o: ";
      cin >> quit; }

   return quit;
}

/* System::Console Compliant
int GetYear()
{
   Console::Write("Input a year: ");
   int yearNumber;
   while (!Int32::TryParse(Console::ReadLine(), yearNumber)) {
      Console::Write("Must be an integer, input a year: "); }

   return yearNumber;
} */

/* std Compliant */
int GetYear()
{
   int yearNumber;
   cout << "Input a year: ";
   cin >> yearNumber;

   while (yearNumber<0 || cin.fail()) {
      if (cin.fail()) {
         cin.clear();
         char c;
         cin >> c; }
      cout << "Must be an integer, input a year: ";
      cin >> yearNumber; }

   return yearNumber;
}

/* System::Console Compliant
int GetDayNum(bool targetIsLeap, int targetYear)
{
   Console::Write("Input a day number (1 to 366): ");
   int dayNumber;
hereDayNumber:
   while (!Int32::TryParse(Console::ReadLine(), dayNumber) || (dayNumber<1 || dayNumber>366)) {
      Console::Write("Must be 1 to 366, input a day number: "); }

   if (!targetIsLeap && dayNumber>365) {
      Console::Write(targetYear + " is not a leap year. Please enter a number between 1 and 365: ");
      goto hereDayNumber; }
   else return dayNumber;
} */

/* std Compliant */
int GetDayNum(bool targetIsLeap, int targetYear)
{
   int dayNumber;
   cout << "Input a day number (1 to 366): ";

hereDayNumber:
   cin >> dayNumber;
   while (dayNumber<1 || dayNumber>366 || cin.fail()) {
      if (cin.fail()) {
         cin.clear();
         char c;
         cin >> c; }
      cout << "Must be 1 to 366, input a day number: ";
      cin >> dayNumber; }

   if (!targetIsLeap && dayNumber>365) {
      cout << targetYear << " is not a leap year. Please enter a number between 1 and 365: ";
      goto hereDayNumber; }
   else return dayNumber;
}

void GetDate(bool targetIsLeap, int targetYear, int targetDay)
{
   /* System::Console Compliant
   array<String^> ^weekDayNames = gcnew array<String^> {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
   array<String^> ^monthNames = gcnew array<String^> {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
   array<Int32> ^monthNumbers = gcnew array<Int32> {31,28,31,30,31,30,31,31,30,31,30,31};
   array<Int32> ^monthAddNumbers = gcnew array<Int32> {31,59,90,120,151,181,212,243,273,304,334,365}; */

   /* std Compliant */
   string weekDayNames[7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
   string monthNames[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
   int monthNumbers[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
   int monthAddNumbers[12] = {31,59,90,120,151,181,212,243,273,304,334,365};

   int currentYear, daysCurrentYear, daysTargetYear, daysElapsed, daysBetween, weekdayNumber;
   int yearsBetween, leapsBetween, totalDaysElapsed, currentWeekday, targetWeekday;
   int arrayCounter=0, monthNumber=0, targetDayNumber, totalTargetDays, totalCurrentDays;

   bool past, future, nearPast, nearFuture, currentIsLeap;

   // String^ dayName, ^monthName, ^targetDate;   // System::Console Compliant
   string dayName, monthName, targetDate;         // std Compliant

   /* System::Console Compliant
   DateTime dt = DateTime::Now;

   currentYear = dt.Year;
   currentWeekday = (int)dt.DayOfWeek;
   daysCurrentYear = dt.DayOfYear; */

   /* std Compliant */
   time_t mytime;
   struct tm times;
   time (&mytime);
   errno_t err;

   err = localtime_s(&times, &mytime);
   if (err) {
   cout << "Invalid argument to _localtime_s." << endl; }

   currentYear = 1900 + times.tm_year;
   currentWeekday = times.tm_wday;
   daysCurrentYear = times.tm_yday + 1;

   int div4 = currentYear%4, div100 = currentYear%100, div400 = currentYear%400;
   currentIsLeap = (div4==0) || ((div100==0) && (div400==0));

   if (targetIsLeap) totalTargetDays = 366;
   else if (!targetIsLeap) totalTargetDays = 365;

   if (currentIsLeap) totalCurrentDays = 366;
   else if (!currentIsLeap) totalCurrentDays = 365;

   /* past year */
   if (targetYear<currentYear) {
      yearsBetween = currentYear - targetYear - 1;
      past = true;
      future = false;
      nearPast = false;
      nearFuture = false; }
   /* future year */
   else if (targetYear>currentYear) {
      yearsBetween = targetYear - currentYear - 1;
      past = false;
      future = true;
      nearPast = false;
      nearFuture = false; }
   /* same year */
   else {
      yearsBetween = 0;
      past = false;
      future = false;
      /* after today */
      if (targetDay>daysCurrentYear) {
         nearPast = false;
         nearFuture = true; }
      /* before today */
      else if (targetDay<daysCurrentYear) {
         nearPast = true;
         nearFuture = false; } }

   /* leap years (therefore days) in years between */
   leapsBetween = yearsBetween*.25;

   /* days in years between (including leap days) */
   daysBetween = (yearsBetween * 365) + leapsBetween;

   /* days elapsed in current and target years */
   if (past) {
      daysTargetYear = totalTargetDays - targetDay;
      daysElapsed = daysTargetYear + daysCurrentYear; }
   else if (nearPast) {
      daysTargetYear = targetDay;
      daysElapsed = daysCurrentYear - daysTargetYear; }
   else if (nearFuture) {
      daysTargetYear = targetDay;
      daysElapsed = daysTargetYear - daysCurrentYear; }
   else if (future) {
      daysTargetYear = targetDay;
      daysElapsed = daysTargetYear + (totalCurrentDays - daysCurrentYear); }

   /* total days elapsed between current day and target day */
   totalDaysElapsed = daysElapsed + daysBetween;

   /* number of the weekday of the target day (between 0 and 6) */
   weekdayNumber = totalDaysElapsed%7;

   /* determine the target weekday */
   if (targetIsLeap && (nearPast || past)) {
      targetWeekday = currentWeekday - weekdayNumber;
      if (targetWeekday<0) targetWeekday = targetWeekday + 7; }
   else if (!targetIsLeap && (nearFuture || future)) {
      targetWeekday = currentWeekday + weekdayNumber;
      if (targetWeekday>6) targetWeekday = targetWeekday - 7; }
   if (!targetIsLeap && (nearPast || past)) {
      targetWeekday = currentWeekday - weekdayNumber;
      if (targetWeekday<0) targetWeekday = targetWeekday + 7; }
   else if (targetIsLeap && (nearFuture || future)) {
      targetWeekday = currentWeekday + weekdayNumber;
      if (targetWeekday>6) targetWeekday = targetWeekday - 7; }

   /* weekday name */
   dayName = weekDayNames[targetWeekday];

   /* find appropriate month */
   while (targetDay>monthNumber) {
      monthNumber = monthAddNumbers[arrayCounter];
      arrayCounter++; }

   /* find day of month */
   if (arrayCounter>1) {
      /* if leap year */
      if (targetIsLeap) {
      /* if target day is 60 */
         if (targetDay==60) {
         targetDayNumber = 29;
         arrayCounter = arrayCounter - 1; }
         /* all other days after February */
         else {
            targetDayNumber = monthNumbers[arrayCounter - 1] - (monthNumber - (targetDay - 1));
            if (targetDayNumber==0) {
               targetDayNumber = monthNumbers[arrayCounter - 1];
               arrayCounter = arrayCounter - 1; } } }
      else targetDayNumber = monthNumbers[arrayCounter-1] - (monthNumber - targetDay); }
   /* all other days */
   else targetDayNumber = targetDay;

   /* get month name */
   monthName = monthNames[arrayCounter-1];

   /* System::Console Compliant
   Console::WriteLine();
   Console::Write("{0, 25} {1, 5}", "Total Target days: ", totalTargetDays);
   Console::WriteLine("{0, 25} {1, 5}", "Total Current days: ", totalCurrentDays);
   Console::Write("{0, 25} {1, 5}", "Target Year: ", targetYear);
   Console::WriteLine("{0, 25} {1, 5}", "Current Year: ", currentYear);
   Console::Write("{0, 25} {1, 5}", "Days Target Year: ", daysTargetYear);
   Console::WriteLine("{0, 25} {1, 5}", "Days Current Year: ", daysCurrentYear);
   Console::Write("{0, 25} {1, 5}", "Target Is Leap Year: ", targetIsLeap);
   Console::WriteLine("{0, 25} {1, 5}", "Current Is Leap Year: ", currentIsLeap);
   Console::WriteLine();
   Console::Write("{0, 25} {1, 5}", "Past: ", past);
   Console::WriteLine("{0, 25} {1, 5}", "Future: ", future);
   Console::Write("{0, 25} {1, 5}", "Near Past: ", nearPast);
   Console::WriteLine("{0, 25} {1, 5}", "Near Future: ", nearFuture);
   Console::WriteLine();
   Console::Write("{0, 25} {1, 5}", "Years Between: ", yearsBetween);
   Console::WriteLine("{0, 25} {1, 5}", "Days Between: ", daysBetween);
   Console::Write("{0, 25} {1, 5}", "Days Elapsed: ", daysElapsed);
   Console::WriteLine("{0, 25} {1, 5}", "Total Days Elapsed: ", totalDaysElapsed);
   Console::WriteLine();
   Console::Write("{0, 25} {1, 5}", "Weekday Number: ", weekdayNumber);
   Console::WriteLine("{0, 25} {1, 5}", "Current Weekday: ", currentWeekday);
   Console::Write("{0, 25} {1, 5}", "Target Weekday: ", targetWeekday);
   Console::WriteLine("{0, 25} {1, -9}", "Target Weekday Name: ", dayName); */

   /* std Compliant */
   cout << right << endl;
   cout << setw(25) << "Total Target days: " << setw(5) << totalTargetDays;
   cout << setw(25) << "Total Current days: " << setw(5) << totalCurrentDays << endl;
   cout << setw(25) << "Target Year: " << setw(5) << targetYear;
   cout << setw(25) << "Current Year: " << setw(5) << currentYear << endl;
   cout << setw(25) << "Days Target Year: " << setw(5) << daysTargetYear;
   cout << setw(25) << "Days Current Year: " << setw(5) << daysCurrentYear << endl;
   cout << setw(25) << "Target Is Leap Year: " << setw(5);
   if (targetIsLeap) cout << "True"; else cout << "False";
   cout << setw(25) << "Current Is Leap Year: " << setw(5);
   if (currentIsLeap) cout << "True"; else cout << "False";
   cout << endl << endl;
   cout << setw(25) << "Past: " << setw(5);
   if (past) cout << "True"; else cout << "False";
   cout << setw(25) << "Near Past: " << setw(5);
   if (nearPast) cout << "True"; else cout << "False";
   cout << endl;
   cout << setw(25) << "Future: " << setw(5);
   if (future) cout << "True"; else cout << "False";
   cout << setw(25) << "Near Future: " << setw(5);
   if (nearFuture) cout << "True"; else cout << "False";
   cout << endl << endl;
   cout << setw(25) << "Years Between: " << setw(5) << yearsBetween;
   cout << setw(25) << "Days Between: " << setw(5) << daysBetween << endl;
   cout << setw(25) << "Days Elapsed: " << setw(5) << daysElapsed;
   cout << setw(25) << "Total Days Elapsed: " << setw(5) << totalDaysElapsed << endl;
   cout << setw(25) << "Weekday Number: " << setw(5) << weekdayNumber;
   cout << setw(25) << "Current Weekday: " << setw(5) << currentWeekday << endl;
   cout << setw(25) << "Target Weekday: " << setw(5) << targetWeekday;
   cout << setw(25) << "Target Weekday Name: " << setw(9) << left << dayName << endl;

   // // targetDate = dayName + ", " + targetDayNumber;   // System::Console Compliant
   cout << endl;                  // std Compliant
   cout << dayName << ", " << targetDayNumber;   // std Compliant
   switch (targetDayNumber) {
      case 1:
      case 21:
      // case 31: targetDate = targetDate + "st ";   // System::Console Compliant
      case 31: cout << "st ";         // std Compliant
         break;
      case 2:
      // case 22: targetDate = targetDate + "nd ";   // System::Console Compliant
      case 22: cout << "nd ";         // std Compliant
         break;
      case 3:
      // case 23: targetDate = targetDate + "rd ";   // System::Console Compliant
      case 23: cout << "rd ";         // std Compliant
         break;
      // default: targetDate = targetDate + "th ";   // System::Console Compliant
      default: cout << "th ";         // std Compliant
         break; }

   // targetDate = targetDate + monthName + " " + targetYear;   // System::Console Compliant
   cout << monthName << " " << targetYear;   // std Compliant

   // Console::WriteLine();         // System::Console Compliant
   // Console::WriteLine(targetDate);   // System::Console Compliant
   cout << endl;                  // std Compliant
}
asa.rand
 
Posts: 21
Joined: Thu Oct 26, 2006 2:21 pm
Location: California, USA

Re: Beginners Task #1

Postby frozenmonkey » Mon May 30, 2011 12:55 pm

I am always looking for questions like these to post it on my programming blog. Here is the code.

[code]
#include <iostream>
using namespace std;
void display_month(int);
int main()
{
int num,num1=60,num2=91,monthnum=2,count=1,day;
bool flag=true;
cout<<"Please enter a number between 1 to 366: "<<endl;
cin>>num;
if(num<=0 && num>366)
{
cout<<"Please enter a valid number."<<endl;
}
else
if(num>0 && num<=31)
{
monthnum = 1;
day=num;
}
else
if(num>31 && num<=60)
{
monthnum=2;
day=num-31;
}
else
{
do{
if(num>num1 && num<=num2)
{
monthnum++;
day=num-num1;
flag=false;
break;
}
else
{
if(count%2==0)
{
num1+=30;
num2+=30;
count++;
monthnum++;
//cout<<"Adding 30 to both nums: "<<num1<<" "<<num2<<endl;
}
else
{
num2+=31;
num1+=31;
count++;
monthnum++;
//cout<<"Adding 31 to both nums: "<<num1<<" "<<num2<<endl;
}
}
}while(flag);
}
display_month(monthnum);
cout<<" "<<day<<" 2004";
cin.get();
cin.get();
}
void display_month(int month)
{
switch(month){
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;
default:
cout<<"Could not find month.";
break;

}
}
[/code]
Source : http://www.mycoding.net/2011/05/write-a ... rresponds/
frozenmonkey
 
Posts: 5
Joined: Sun Feb 13, 2011 3:56 am

Previous

Return to Contests

Who is online

Users browsing this forum: No registered users and 2 guests