Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,392 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,253 people online right now. Registration is fast and FREE... Join Now!




understanding calendar code

 
Reply to this topicStart new topic

understanding calendar code

catch_arnnie
31 Jan, 2008 - 02:42 AM
Post #1

New D.I.C Head
*

Joined: 31 Jan, 2008
Posts: 6

can sumbody please explain me the following calendar code(i got this from here only)????


CODE
#include<iostream.h>
#include<conio.h>
int leapyr(int k);
void display(int p,int q, int r);
void main()
{clrscr();
int  y,m,j,a[13]={0,3,6,7,3,5,1,3,6,2,4,7,2};
yr:  cout<<"enter any yr btw 1980 and infinity(in no.s)"<<endl;
cin>>y;
if(y<1980)
goto yr;
mn: cout<<"enter its month"<<endl;
cin>>m;
if(m<1||m>12)
goto mn;
if(m<3)
for(j=1981;j<=y;j++)
  {a[m]=a[m]+leapyr(j-1)+1;
   if(a[m]>7)
   a[m]=a[m]-7;
  }
else
for(j=1981;j<=y;j++)
{a[m]=a[m]+leapyr(j)+1;
   if(a[m]>7)
   a[m]=a[m]-7;
}
cout<<a[m];
display(y,m,a[m]);
getch();
}
int leapyr(int k)
{ if(k%400==0)
   return 1;
  else
    if(k%100==0)
     return 0;
    else
     if(k%4==0)
       return 1;
     else
      return 0;
   }
void display(int p,int q,int r)
{int a[6][7],b,i,j,n;
clrscr();
if(q==1||q==3||q==5||q==7||q==8||q==10||q==12)
b=31;
if(q==4||q==6||q==9||q==11)
b=30;
if(q==2)
b=28+leapyr(p);
cout<<"   s   m   t   w   th  fr  sa"<<'\n';
n=2-r;
for(i=0;i<6;i++)
{for(j=0;j<7;j++)
{a[i][j]=n;
n++;
gotoxy((j+1)*4,i+2);
if(a[i][j]<1||a[i][j]>b)
cout<<" ";
else
cout<<a[i][j];
}}
}



plz explain all the processing... i'll be really grateful...

User is offlineProfile CardPM
+Quote Post

catch_arnnie
RE: Understanding Calendar Code
31 Jan, 2008 - 03:00 AM
Post #2

New D.I.C Head
*

Joined: 31 Jan, 2008
Posts: 6

can sombody kindly explain the basic logic behind this calendar code...???
User is offlineProfile CardPM
+Quote Post

Bench
RE: Understanding Calendar Code
31 Jan, 2008 - 03:04 AM
Post #3

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 683



Thanked: 24 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
Where exactly did you find this? the usage of goto jumps in that code are horrible, and the overall formatting is pretty dire too. If you wish to trace it through, then feel free, but I strongly suggest you go and find a better example which doesn't insist on using goto loops. You'll probably learn nothing useful by understanding the code you have there.
User is offlineProfile CardPM
+Quote Post

catch_arnnie
RE: Understanding Calendar Code
31 Jan, 2008 - 03:09 AM
Post #4

New D.I.C Head
*

Joined: 31 Jan, 2008
Posts: 6

hey bench!! could you please kindly consider this code(ignoring the goto loops) & please explain me the basic logic & also why the array a[13] is taken ???
User is offlineProfile CardPM
+Quote Post

Bench
RE: Understanding Calendar Code
31 Jan, 2008 - 03:26 AM
Post #5

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 683



Thanked: 24 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
Whoever wrote this code didn't have readability in mind, so some of this is guesswork based on the few clues left by the person who wrote it.

An array of size 13 would be indexed 0 through to 12. Being that there are 12 months in a year, I'm guessing the idea is that he/she wanted to map months to their usual numerical values, eg, Jan = 1, Feb = 2, Mar = 3, etc. instead of starting out with Jan = 0, Feb = 1.

The code uses an awful lot of "magic" numbers littered around without any clear indication as to what they mean, and the variable names are equally as unhelpful. Its not clear at all why the array starts out populated with {0,3,6,7,3,5,1,3,6,2,4,7,2}; . If I were to take a guess, it would be that these numbers somehow relate to the first day of each month (with the zero'th month being invalid), but beyond that, the code is too cryptic to tell.


In all seriousness, If someone were to pass this across my desk at work, and tell me to fix it, extend it, or anything else, the first key I'd press would be 'delete', and start over again, since my time would be better spent writing it from scratch than spending 3 hours understanding it.
User is offlineProfile CardPM
+Quote Post

catch_arnnie
RE: Understanding Calendar Code
31 Jan, 2008 - 03:39 AM
Post #6

New D.I.C Head
*

Joined: 31 Jan, 2008
Posts: 6

i got this code from here .

het bench!! thanks 4 ur consideration of my query....

maybe sumbody else who understands this can explain...??

This post has been edited by catch_arnnie: 31 Jan, 2008 - 03:41 AM
User is offlineProfile CardPM
+Quote Post

Bench
RE: Understanding Calendar Code
31 Jan, 2008 - 03:58 AM
Post #7

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 683



Thanked: 24 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
I doubt that anyone else will have any luck either - that's not the kind of code which one can quickly understand.
You could try sending the author of that snippet a message. He/she is the most likely person to be able to explain it to you. Otherwise, your best bet is to seek out a better example - if you find one which is well written, then much of the code should explain itself to you.
User is offlineProfile CardPM
+Quote Post

catch_arnnie
RE: Understanding Calendar Code
31 Jan, 2008 - 04:06 AM
Post #8

New D.I.C Head
*

Joined: 31 Jan, 2008
Posts: 6

once again, i'm thankful 4 ur help(consideration for my query) bench, but how do i message the author??(i'm not able to PM him)...

This post has been edited by catch_arnnie: 31 Jan, 2008 - 04:07 AM
User is offlineProfile CardPM
+Quote Post

Bench
RE: Understanding Calendar Code
31 Jan, 2008 - 07:23 AM
Post #9

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 683



Thanked: 24 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
QUOTE(catch_arnnie @ 31 Jan, 2008 - 12:06 PM) *

once again, i'm thankful 4 ur help(consideration for my query) bench, but how do i message the author??(i'm not able to PM him)...

Oh! I think </d.i.c.> needs you to have posted a certain number of messages to the forums before you can send PM's, I think its 10, but I can't remember. Does the 'send email' link not work either?

Post a couple more messages and i'm sure it'll work smile.gif
User is offlineProfile CardPM
+Quote Post

catch_arnnie
RE: Understanding Calendar Code
6 Feb, 2008 - 12:34 AM
Post #10

New D.I.C Head
*

Joined: 31 Jan, 2008
Posts: 6

hey bench!! thanks a lot for ur help!! i was finally able to understand the calendar code...thanks again...
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 10:08AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month