Beginners Task #1

Online C++ programming contests.

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

Postby mabufo » Fri May 21, 2004 2:33 pm

I got my program to tell you what month it is...

I'm still working on it though! 8)
pm me if you need something... whatever.

(c++ beginner)
User avatar
mabufo
 
Posts: 1008
Joined: Mon Apr 05, 2004 7:09 pm
Location: Midwestern USA.

Postby mabufo » Fri May 21, 2004 3:51 pm

I need help finding the day....

like it says the 23rd of january....

How do I find the underlined number based on what the user gives me? The answer is probably right under my nose too =/
pm me if you need something... whatever.

(c++ beginner)
User avatar
mabufo
 
Posts: 1008
Joined: Mon Apr 05, 2004 7:09 pm
Location: Midwestern USA.

Postby Johnny T » Fri May 21, 2004 4:12 pm

Well !! My gast is flabbered....

Some superb responses to the first task and some absolutely brilliant solutions...

I haven't had chance to compile them all yet but I will and its great to see that people are learning from this....

We could maybe make this a regular feature.. chuck in a "beginners task" now and again ??

Good on ya lads and lasses, keep up the very good work !

:D
I'm beta testing life...

(C++ Beginner)
User avatar
Johnny T
 
Posts: 138
Joined: Fri Apr 16, 2004 10:42 am
Location: Lancashire, UK

Postby Guest » Sat May 22, 2004 7:29 am

mabufo wrote:I need help finding the day....

like it says the 23rd of january....

How do I find the underlined number based on what the user gives me? The answer is probably right under my nose too =/


You will need to store the last day of every month in a array. Then use the array to subtract from the number the users gives you. That will give you the day but not the 1st, 2nd, 3rd, 4th. I hope this is not to vague but did not want to spoil to much for you.
Guest
 

Postby Guest » Sat May 22, 2004 7:32 am

Johnny T wrote:Well !! My gast is flabbered....

Some superb responses to the first task and some absolutely brilliant solutions...

I haven't had chance to compile them all yet but I will and its great to see that people are learning from this....

We could maybe make this a regular feature.. chuck in a "beginners task" now and again ??

Good on ya lads and lasses, keep up the very good work !

:D

Yes it was a nice challenge for me and enjoyed it.
Thank you
Johnny T
Guest
 

Postby mabufo » Sat May 22, 2004 8:50 am

Alright here's what I got...

Code: Select all
/*Get Date program...
  This program takes the user input and makes it into the
  corresponding date of the year! Example: If the user inputs
  1, the output is: 1st January 2004.
*/


#include <iostream>
#include <stdlib.h>


using namespace std;

struct month {
   int beginning;
   int end;
   int totalDays;
};

int choice;


void getDate(int day)
  {
   int date;
   month January =   { 1, 31, 31 };
   month February =  { 32, 61, 28 };
   month March =     { 62, 93, 31 };
   month April =     { 94, 124, 30 };
   month May =       { 125, 156, 31 };
   month June =      { 157, 187, 30 };
   month July =      { 188, 219, 31 };
   month August =    { 220, 251, 31 };
   month September = { 252, 282, 30 };
   month October =   { 283, 314, 31 };
   month November =  { 315, 345, 30 };
   month December =  { 346, 365, 31 };

        if ((day >= 1) && (day <= 365))
          {
            if((day >= January.beginning) && (day <= January.end)) //month is January
             {
                date = day ;
                cout << date;
                cout << "  Jsnuary 2004 " <<endl;
                cout << " Do you have more to do? "<<endl;
                cin >> choice;
                system("pause");
               
             }
           
            else if((day >= February.beginning) && (day <= February.end)) //month is February
             {
                date = day - 32;
                cout << date;
                cout << " February 2004 " <<endl;
                system("pause");
             }
           
            else if((day >= March.beginning) && (day <= March.end)) //month is March
             { 
                date = day - 62;
                cout << date;
                cout << " March 2004 " <<endl;
                system("pause");
             }
             
            else if((day >= April.beginning) && (day <= April.end)) //month is April
             {
                date = day - 94;
                cout << date;
                cout << " APril 2004 " <<endl;
                system("pause");
             }
           
            else if((day >= May.beginning) && (day <= May.end)) //month is May
             {
                date = day - 125;
                cout << date;
                cout << " May 2004 " <<endl;
                system("pause");
             }
           
            else if((day >= June.beginning) && (day <= June.end)) //month is June
             {
                date = day - 157;
                cout << date;
                cout << " June 2004 " <<endl;
                system("pause");
             }
           
            else if((day >= July.beginning) && (day <= July.end)) //month is JUly
             {
                date = day - 188;
                cout << date;
                cout << " July 2004 " <<endl;
                system("pause");
             }
             
            else if((day >= August.beginning) && (day <= August.end)) //month is August
             {
                date = day - 220;
                cout << date;
                cout << " August 2004 " <<endl;
                system("pause");
             }
             
            else if((day >= September.beginning) && (day <= September.end)) //month is September
             {
                date = day - 252;
                cout << date;
                cout << " September 2004 " <<endl;
                system("pause");
             }
           
            else if((day >= October.beginning) && (day <= October.end)) //month is October
             { 
                date = day - 283;
                cout << date;
                cout << " OCtober 2004 " <<endl;
                system("pause");
             }
           
            else if((day >= November.beginning) && (day <= November.end)) //month is November
             {
                date = day - 315;
                cout <<date;
                cout << " November 2004 " <<endl;
                system("pause");
             }
           
            else if((day >= December.beginning) && (day <= December.end)) //month is December
             {
                date = day - 346;
                cout << date;
                cout << " December 2004 " <<endl;
                system("pause");
             }
           
            else
             {
                cout << "Error!!! Please refine j00 input!" << endl;
                system("pause");
             }
   }
}     

     
               
int main()
{


   int day;
   
   cout << "PLease enter a number between 1 and 365!" << endl;
   cin >> day;
   getDate(day);

   
     return 0;
   
}


From what I know, it works fine.
pm me if you need something... whatever.

(c++ beginner)
User avatar
mabufo
 
Posts: 1008
Joined: Mon Apr 05, 2004 7:09 pm
Location: Midwestern USA.

Postby Guest » Sat May 22, 2004 9:35 am

mabufo wrote:Alright here's what I got...
From what I know, it works fine.

The dates are a little off enter 61 this should be march 1st not feb 29th.
Guest
 

Postby mabufo » Sat May 22, 2004 9:49 am

hmmm, I probably got the start dates and end sdates messed up...

:?
pm me if you need something... whatever.

(c++ beginner)
User avatar
mabufo
 
Posts: 1008
Joined: Mon Apr 05, 2004 7:09 pm
Location: Midwestern USA.

Postby mabufo » Sat May 22, 2004 9:51 am

then again I must have set the math problem that gets the date up incorrectly.

well, back to R&D for me!
pm me if you need something... whatever.

(c++ beginner)
User avatar
mabufo
 
Posts: 1008
Joined: Mon Apr 05, 2004 7:09 pm
Location: Midwestern USA.

Postby Guest » Sat May 22, 2004 4:37 pm

mabufo wrote:then again I must have set the math problem that gets the date up incorrectly.

well, back to R&D for me!

Here are some sample results you can use to test with.
32
1st February 2004 Sunday,
31
31st January 2004 Saturday,
86
26th March 2004 Friday,
45
14th February 2004 Saturday,
78
18th March 2004 Thursday,
56
25th February 2004 Wednesday,
99
8th April 2004 Thursday,
91
31st March 2004 Wednesday

Also this is a leap year so it would be 1 - 366 not 1 - 365.
Guest
 

Re: Here is my solution

Postby Guest » Sat May 22, 2004 4:52 pm

Stuck wrote:It compiles without error under Borland IDE 5.2

This is also a very nice version. Good job.
Guest
 

Postby WaltP » Sat May 22, 2004 8:44 pm

I just took a quick overview of this thread and I have to say this is a very good task. Some interesting decisions were made, some of which I like, others of course could use improvement. I naturally can't comment on each submission, so I'll make a few blanket comments and suggestions. I won't be posting any answers -- I'll leave that up to you to use these ideas or not.

The use of structs was interesting, not something I thought of (you'll see why as I continue). I'll show a struct at the end I might use.

The use of a totals array I personally would not use, especially if using a days/month array. For example:
Code: Select all
daysmonth[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
totaldays[] = { 1, 32, 62, 94, 125,157,188,220,252,283,315,346};

the values in totaldays[] can be computed from daysmonth[] with a minor loop, making it unnecessary for the programmer to do that work.

So with just the daysmonth array and a loop you can compute the the month and the day of the month quite easily using subtraction. Which loop do you think would work best? (for, while, do-while)

Many posts used lots of nested if's, most of which can be replaced by the above-mentioned loop.

Of course a second array can be used for the month names as many did.

The switch statement for the day suffix was a nice way to handle that requirement. The closest I saw to the optimum switch was something like:
Code: Select all
switch(day)
{
    case  1:  cout << "st"; break;
    case  2:  cout << "nd"; break;
    case  3:  cout << "rd"; break;
    case 21:  cout << "st"; break;
    case 22:  cout << "nd"; break;
    case 23:  cout << "rd"; break;
    case 31:  cout << "st"; break;
    default:  cout << "th"; break;
}

My only change to this would be combining the cases:
Code: Select all
switch(day)
{
    case  1: 
    case 21:
    case 31:
        cout << "st";
        break;
etc.

which is perfectly legal.

When adding the leap-year, all you need to do is figure out
1) is it a leap year?
2) is it after Feb 28?
If yes to both, add one to daysmonth[1] (feb) just before going into the loop.

Oh yes, and displaying the day, a simple modulus operator (%) is all that's needed. Remember, Jan 1st was Thursday as per Johnny T's original problem description. No need to do an if for every day of the year...

Now for the struct, as you can see from the above there isn't much to really define from an computational standpoint:
Code: Select all
struct MONTH
{
    char *monthName;
    int numDays;
}

Then create an array of ths struct for each month.
There are only 10 types of people in the world -- those that use binary, and those that don't
WaltP
 
Posts: 1187
Joined: Thu Oct 16, 2003 11:24 am

I Took The Easy Road

Postby Stuck » Sun May 23, 2004 7:47 pm

With mine I used simple subtraction to find the day of the month.
Code: Select all
int DateFunc(int a, int b, int c)
{
     int d;
     
     d = a - b;
     day = c - d;

     return day;
}


where a = number of days in the year
b = user input
c = number of days in the month

Very simple way of returning the day of the month, but also very effective.

It wasnt as elegant as some of teh other solutions, but I really learned alot from a seemingly simple task program.

I look forward to participating in more of these tasks. When is the next one?

I have also been thinking of the day of the week, but a solution for creating an algorithm to solve it escapes me.
Stuck
 

mabufo

Postby Stuck » Mon May 24, 2004 11:46 am

I just noticed this in your code:
Code: Select all
month February =  { 32, 61, 28 };


February has 29 days this year. That would throw you off once you get into anything past 60th day.
Stuck
 

Postby RecursiveS » Mon May 24, 2004 3:00 pm

Looks like y'all had a good task :D

Gonna unsticky now, get ready for the next one.

Johnny T: Let me know when the next one is coming.....
User avatar
RecursiveS
Site Admin
 
Posts: 1236
Joined: Thu Sep 18, 2003 8:33 am
Location: Dorset, UK

PreviousNext

Return to Contests

Who is online

Users browsing this forum: No registered users and 1 guest