?
Solved

Calendar Program using loops

Posted on 2003-03-05
9
Medium Priority
?
197 Views
Last Modified: 2012-05-04
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
Comment
Question by:dmuran
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
9 Comments
 
LVL 6

Expert Comment

by:gj62
ID: 8075760
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
 

Author Comment

by:dmuran
ID: 8075895
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
 
LVL 6

Expert Comment

by:gj62
ID: 8075919
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 6

Expert Comment

by:gj62
ID: 8075943
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
 
LVL 6

Expert Comment

by:gj62
ID: 8075957
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
 
LVL 6

Accepted Solution

by:
gj62 earned 200 total points
ID: 8075972
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
 

Author Comment

by:dmuran
ID: 8076003
Thanks again, this might help me get through it....

DM
0
 
LVL 6

Expert Comment

by:gj62
ID: 8076059
If you get stuck, let me know -

Thanks...
0
 

Author Comment

by:dmuran
ID: 8076089
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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question