• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 201
  • Last Modified:

Calendar Program using loops

Hello guys,
I'm a new to program so the program I need help with its pretty easy, can anyone please help me...

I need to write a program that will ask a user to key in the year and it will print the calendar of the whole year month after month.

I also need to focus on using loops so if u decide to help me please help me with loops...

Thanks in advance,

DM
0
dmuran
Asked:
dmuran
  • 6
  • 3
1 Solution
 
gj62Commented:
This sounds like homework - the board rules are that we can't do homework for you...

Of course, nothing is stopping you from typing Calendar Source Code into google...
0
 
dmuranAuthor Commented:
I dont want the whole code....
I just need to know how to calcualte the last day of the previous year...

Thanks for replay, it is homework but I'm stuck and that's all I need...

Thanks a lot

DM  
0
 
gj62Commented:
Calculate the last day of the previous year -

The last day is ALWAYS Dec 31st...

I assume you want the day of the week - see below for how to calculate...

To find the day of the week for any date between Jan 1, 1901 and Dec 31 2099

      1.  Find the day number using the table of days before the first of the month

      2.  Calculate first day as follows

          a = (year - 1901) modulo 28
          b = floor (a/4)
          first day = (2 + a + b) modulo 7 + 1

      3.  Day of the Week = ((day number - 1) + (first day - 1)) modulo 7 + 1



--------------------------------------------------------------------------------

Addendum
1.   This algorithm only works for the dates between 1901 and 2100. The fact that  2100 is not a leap year  prohibits the 28 year cycle for obtaining the first day from carrying over into the 22nd century.

2.   The algorithm presented makes use of modular arithmetic with its use of modulo 28 and modulo 7 calculations. In modular arithmetic it's easier to starting counting at 0 instead of 1. Consequently the algorithm could be simplified if we made the following changes

      a.  Number the days from 0 to 6 with Sunday being day 0 and Saturday being day 6

      b.  Number the days of the year from 0 to 364 (or 365 for leap year) with January 1 being day 0 etc.



--------------------------------------------------------------------------------

Alternate Algorithm
Number the days of the week 0 - 6 and the days of the year 0 - 364 (or 365 for leap year)

1.  Find the day number using the table of days before the first of the month except subtract 1 from  this value. This would number the day of the year from 0 to 364 (or 365 for leap years)

2.  Calculate first day as follows

        a = (year - 1901) modulo 28
        b = floor (a/4)
        first day = (2 + a + b) modulo 7

We note that Tuesday January 1, 1901 is day 2 under the new numbering

3.  Day of the Week = (day number  + first day) modulo 7

Example : Find the Day of the Year for March 21, 2000

      1.  March 21, 2000 is day 60 + 21 - 1 = 80

      2.  a = (2000 - 1901) modulo 28 = 15
           b = floor (15/4) = 3
           first day = (2 + 15 + 3) modulo 7 = 6 (Saturday!)

      3.  Day of the Week - (6 + 80) modulo 7 = 2 (Tuesday)

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
gj62Commented:
Oh, never mind...

Here's the code out of the C FAQ - required reading for people just learning..

dayofweek(y, m, d)     /* 0 = Sunday */
int y, m, d;          /* 1 <= m <= 12,  y > 1752 or so */
{
     static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
     y -= m < 3;
     return (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
}



Find the C Faq at http://www.eskimo.com/~scs/C-faq/top.html

Lots of useful stuff...
0
 
gj62Commented:
Of course, all the above assumes the Gregorian calendar...

Here's how to find leap year...

if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))
  printf("Year %d is a leap year",year);

0
 
gj62Commented:
Of course, if you use that code directly, you'd better be able to explain how/why it works - at least if you were my student - read the FAQ - it will tell you where to look for other methods you might be able to explain easier...
0
 
dmuranAuthor Commented:
Thanks again, this might help me get through it....

DM
0
 
gj62Commented:
If you get stuck, let me know -

Thanks...
0
 
dmuranAuthor Commented:
Thanks a lot, I really appreciate your help...
I think I should be ok for now, I have to go over loops and I'll try to put all this together....


Thanks again,

DM
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now