The day of the week America was discovered

This is a discussion on The day of the week America was discovered within the C Programming forums, part of the General Programming Boards category; Hi. A few weeks a go a post some code to calculate the day of the week of a particular ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Location
    Mexico City
    Posts
    33

    The day of the week America was discovered

    Hi.

    A few weeks a go a post some code to calculate the day of the week of a particular date. It was just an idea. I had to sit down and code it. Here you have it:

    Code:
    #include <stdio.h>
    
    char *day[] = {"Friday","Saturday","Sunday","Monday","Tuesday","Wednesday",
                    "Thursday"};
    int day_of_month[] = {31,28,31,30,31,30,31,31,30,31,30,31};
    
    int day_of_week(int, int, int);
    int leap (int);
    void instructions(void);
    
    int d, m, a;
    
    
    int main () {
        
        instructions();
        
        
    do {
        printf("\nEnter date -> ");
        scanf("%d%d%d",&d, &m, &a);
        printf("%d %d %d\n", d, m, a);
        printf ("\nThe day of the week is %s \n", day[day_of_week(d,m,a)]);
        
    } while ( (d!=0) && (m!=0) && (a!=0) );
        
    }
    
    int day_of_week(int d, int m, int a) {
    int xa, i;
    int days_counter = 0;
    
        // Compute days from begining of era to december 31 of year a-1
        for (xa = 0; xa < a; xa++) 
            if (leap(xa))
               days_counter += 366;
            else days_counter += 365; 
              
        // Compute days from months before m
        for (i=0; (i+1) < m; i++)
            days_counter += day_of_month[i];
            
        // compute the remaining days
        
        days_counter+= d;
        
        // See if a is leap year and if so, see if date is after february
        // Add 1 if so.
        
        if ( (m > 2) && (leap(a)))
           days_counter += 1;
           
        return ( (int) (days_counter % 7) );  // return the remainder     
    }                                         
    
    int leap(int x) {
        
        if ( (x % 400) == 0 ) // is leap
           return (1);
        else
           if ( (x % 100) == 0 )  // it's not leap
              return (0);
           else if ( (x % 4) == 0 )
                    return (1); // is leap
                else return (0); // it's not leap          
    }
    
    
    void instructions(void) {
         
         printf("This program computes the day of the week of a particular\n");
         printf("date. The date must be entered in the form dd mm yyyy.\n");
         printf("It should be separated by spaces. Example October 12 1492,\n");
         printf("should be entered as : 12 10 1492    .\n");
         printf("1. the program will give an answer even if the date does not exist.\n");
         printf("2. the program only works for dates after Christ.\n");
         printf("3. try the day you were born!\n");
         
    }
    If you want to be happy one hour: take a nap
    if you want to be happy one day: go fishing
    If you want to be happy a year: inherit a fortune
    if you want to be happy for a life time: HELP SOMEBODY
    chinisse say.

  2. #2
    ---
    Join Date
    May 2004
    Posts
    1,379
    Wher is the indentation?

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Enter date -> 12 10 1492
    12 10 1492

    The day of the week is Wednesday
    This calculator comes up with Friday. Calendar dates are strange things.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Good grief...I never realized that it was so complicated.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with day of the week program
    By Punkakitty in forum C++ Programming
    Replies: 10
    Last Post: 01-14-2009, 05:55 PM
  2. day of week
    By s_ny33 in forum C Programming
    Replies: 18
    Last Post: 11-02-2005, 10:56 AM
  3. Force end
    By Redattack34 in forum C++ Programming
    Replies: 9
    Last Post: 09-06-2005, 11:16 PM
  4. debug program
    By new_c in forum C Programming
    Replies: 3
    Last Post: 03-18-2002, 10:50 PM
  5. Simplified code
    By soonerfan in forum C Programming
    Replies: 2
    Last Post: 12-05-2001, 02:50 PM

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