What is wrong with the year 2000?

This is a discussion on What is wrong with the year 2000? within the C Programming forums, part of the General Programming Boards category; I have written a program that accepts the year gives the day on which displays the day on which the ...

  1. #1
    Registered User Silverdream's Avatar
    Join Date
    Feb 2002
    Posts
    53

    What is wrong with the year 2000?

    I have written a program that accepts the year gives the day on which displays the day on which the 1st of january falls. The program works for years upto 1999, but not after that. I think it may be a silly mistake. Please help

    Code:
    #include <stdio.h>
    
    int main()
    {
           int temp, given_year, counter=1;
    
    
           printf("Enter the year\n");
           scanf("%4d",&given_year);
    
           for(temp=1900;temp<=given_year;temp++)
           {
    	if((temp-1)%4==0)
    	{
                        if(counter<7)
    	    {
    	         counter=counter+2;
    	    }
    	    else
    	    {
    	         counter=2;
    	    }
    	}
    	else
    	{
                         if(counter<7)
    	     {
    	         counter++;
    	     }
    	     else
    	     {
    	         counter=1;
    	     }
    	}
               }
    
         switch(counter)
          {
    	case 1:
    		printf("Monday");
    		break;
    	case 2:
    		printf("Tuesday");
    		break;
    	case 3:
    		printf("Wednesday");
    		break;
    	case 4:
    		printf("Thursday");
    		break;
    	case 5:
    		printf("Friday");
    		break;
    	case 6:
    		printf("Saturday");
    		break;
    	case 7:
    		printf("Sunday");
    		break;
    	default:
    		exit();
            }
    
          return 0;
    }

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    I don't know what your code does or is supposed to do. Please be a bit more elaborate with the "doesn't work" part next time.

    >if((temp-1)%4==0)


    If this is a check if the year has a Feb. 29, take in account that this is not the full formula. A year has a Feb. 29, if

    a) it's divisable by four
    b) not divisable by 100, exception: divisable by 400.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Registered User Silverdream's Avatar
    Join Date
    Feb 2002
    Posts
    53

    Talking

    Sorry that i wasnt clear!.

    Well here it goes again.

    i have written a program which accepts a year from the user. It then displays the day on which the first of January fell in that year.

    For example

    if the user enters the year 1980.

    the program displays tuesday

    Because January 1st 1980 was a tuesday.

    Now what i want to know is that the program displays the day for all the years upto 1999 but after that for the year 2000 it displays nothing and for the years after 2000 it displays the wrong day. I hope i have made the program clear now.

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    I have posted a full calender app on these boards several times.That calender app uses Zeller's algorithm. Look at the code or search for zeller's algorithm at google. zellers algorithm tells you the day for a given date and is valid for all of the gregorian calender.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    Just had a few problems. I also made the days an array of strings because it looks cooler and is probably better.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
     int temp, given_year, counter=1;
     char theday[7][10] = { "Monday", "Tuesday", "Wednesday", 
                            "Thursday", "Friday", "Saturday", 
                            "Sunday" };
     
     printf("Enter the year\n");
     scanf("%4d",&given_year);   
     if(given_year < 1900)
     {
      printf("Year has to be 1900 or above.");
      return -1;
     }
     for(temp=1900;temp<=given_year;temp++)
     {
      if((temp-1)%4 == 0 && ((temp-1)%100 == 0 || (temp-1)%400 != 0))
      {
       if(counter<7)
        counter+=2;
       else
        counter=2;  
      }
      else
      {
       if(counter<=7)
        counter++;
       else
        counter=1;
      }
     }
     printf("%s",theday[counter-2]);
     return 0;
    }
    Last edited by Brian; 02-17-2002 at 09:52 AM.

  6. #6
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    Guess I was kinda giving you a fysh there, but oh well.

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    You really need to zero-index your days. go 0-6, not 1-7. This is why...
    Code:
    if((temp-1)%4==0)
    	{
                        if(counter<7)
    	    {
    	         counter=counter+2;
    	    }
    	    else
    	    {
    	         counter=2;
    	    }
    	}
    	else
    	{
                         if(counter<7)
    	     {
    	         counter++;
    	     }
    	     else
    	     {
    	         counter=1;
    	     }
    	}
    You'd do much better to replace this code with
    Code:
    if((temp-1)%4==0)
    	{
                      counter = (counter + 2) % 7;
    	}
    	else
    	{
                       counter = (counter + 1) % 7;
    	}
    Your code will always switch to Tuesday when it rolls over on a leap year, whether or not it was supposed to go to Monday or Tuesday. Also, in your program it is possible for your program to end with counter > 7, cinsider the following code
    Code:
    if(counter<7)
    	    {
    	         counter=counter+2;
    	    }
    	    else
    	    {
    	         counter=2;
    	    }
    With counter intiaially valued at 6.
    Callou collei we'll code the way
    Of prime numbers and pings!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What is wrong with this program(data structures)
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 06-06-2002, 12:55 PM
  2. function
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 06-02-2002, 07:38 PM
  3. what is wrong with this code?
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 05-27-2002, 07:27 PM
  4. Simplified code
    By soonerfan in forum C Programming
    Replies: 2
    Last Post: 12-05-2001, 02:50 PM
  5. Help Me Out!! Pls
    By Joanna in forum C++ Programming
    Replies: 5
    Last Post: 10-27-2001, 05:08 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21