Thread: Mayan Calander Conversion Issue

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    26

    Mayan Calander Conversion Issue

    Hi guys,
    I'm doing a past question to practice programming. The question is here: http://www.olympiad.org.uk/papers/2004/bio/q1.html

    Its basically a calender conversion from a Mayan calender to the Gregorian calender. Heres what I have done with comments.

    Code:
    #include <stdio.h>
    
    int main()
    {
        int baktun, katun, tun, uinal, kin, mdays, diffrence, ydiffrence, months, day;
         
        printf("Enter the number of Baktuns:");
        scanf("%d",&baktun);
        printf("Enter the number of Katuns:");
        scanf("%d",&katun);                             /*Enter the Mayan Date*/
        printf("Enter the number of Tun:");
        scanf("%d",&tun);
        printf("Enter the number of Uinal:");
        scanf("%d",&uinal);
        printf("Enter the number of Kin:");
        scanf("%d",&kin);
        baktun = baktun * 20 * 20 * 18 * 20;
        katun = katun * 20 * 18 * 20;
        tun = tun * 18 * 20;                            /*Convert to Kin (Days)*/
        uinal = uinal * 20;
        mdays = baktun + katun + tun + uinal + kin;
        diffrence = mdays - 2018843;                    /*Subtract the date in days from Jan 01*/
    
        ydiffrence = diffrence / 365;                   /*Divide the answer by 365 to get diffrence in years*/
        ydiffrence = ydiffrence + 2000;                 /*Add 2000 to get to Mayan Base date and years */
        
        day = diffrence % 365;                          /*Days = the remainder when divided by 365*/
        
        if (diffrence > 365)
    {
        months = diffrence - 365;                       /*Find the Month*/
                                                        
        printf ("January ");                            
    }
        else if (months <= 59) 
    {
        printf ("Febuary ");
    }
        else if (months <= 90)
    {
        printf ("March ");
    }
        else if (months <= 120)
    {
        printf ("April ");
    }
        else if (months <= 151)
    {
        printf ("May ");
    }
        else if (months <= 181)
    {
        printf ("June ");
    }
        else if (months <= 212)
    {
        printf ("July ");
    }
        else if (months <= 243)
    {
        printf ("August ");
    }
        else if (months <= 274)
    {
        printf ("September ");
    }
        else if (months <= 304)
    {
        printf ("October ");
    }
        else if (months <= 334)
    {
        printf ("November ");
    }
        else if (months <= 365)
    {
        printf ("December ");
    }
    
    
    if (months <= 31)
    {
        printf("%d ",day);  /*Find the Day*/
        
    }   
    
        else if (months <= 59)
    {
        day = day - 31;
        printf("%d ",day);
       
    } 
        else if (months <= 90)
    {
        day = day - 59;
        printf("%d ",day);
        
    } 
        else if (months <= 120)
    {
        day = day - 90;
        printf("%d ",day);
        
    } 
        else if (months <= 151)
    {
        day = day - 120;
        printf("%d ",day);
        
    } 
        else if (months <= 181)
    {
        day = day - 151;
        printf("%d ",day);
        
    } 
        else if (months <= 212)
    {
        day = day - 181;
        printf("%d ",day);
        
    } 
        else if (months <= 243)
    {
        day = day - 212;
        printf("%d ",day);
       
    } 
        else if (months <= 274)
    {
        day = day - 243;
        printf("%d ",day);
       
    } 
        else if (months <= 304)
    {
        day = day - 274;
      
        printf("%d ",day);
      
    } 
        else if (months <= 334)
    {
        day = day - 304;
        
        printf("%d ",day);
        
    } 
        else if (months <= 365)
    {
        day = day - 334;
        
        printf("%d ",day);
        
    } 
        else if  (months <= 90)
    {
        day = day - 59;
        
        printf("%d ",day);
       
    } 
        printf("%d",ydiffrence);
    
        getchar();
        getchar();
        return 0;
    }
    If I enter the test data: 13,20,9,29 the date should be 22nd March 2001 however I get 22nd Jan 2001 and entering other values closes the program. Any ideas?


    Thanks in Advance

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Step 1 is learn how to indent, or set your editor to only use say 4 spaces rather than one real TAB character. A tab character will get messed up when printed / posted / sent / edited with another editor.

    Eg
    Code:
    #include <stdio.h>
    
    int main()
    {
        int baktun, katun, tun, uinal, kin, mdays, diffrence, ydiffrence,
            months, day;
    
        printf("Enter the number of Baktuns:");
        scanf("%d", &baktun);
        printf("Enter the number of Katuns:");
        scanf("%d", &katun);        /*Enter the Mayan Date */
        printf("Enter the number of Tun:");
        scanf("%d", &tun);
        printf("Enter the number of Uinal:");
        scanf("%d", &uinal);
        printf("Enter the number of Kin:");
        scanf("%d", &kin);
        baktun = baktun * 20 * 20 * 18 * 20;
        katun = katun * 20 * 18 * 20;
        tun = tun * 18 * 20;        /*Convert to Kin (Days) */
        uinal = uinal * 20;
        mdays = baktun + katun + tun + uinal + kin;
        diffrence = mdays - 2018843;  /*Subtract the date in days from Jan 01 */
    
        ydiffrence = diffrence / 365; /*Divide the answer by 365 to get diffrence in years */
        ydiffrence = ydiffrence + 2000; /*Add 2000 to get to Mayan Base date and years */
    
        day = diffrence % 365;      /*Days = the remainder when divided by 365 */
    
        if (diffrence > 365) {
            months = diffrence - 365; /*Find the Month */
            printf("January ");
        } else if (months <= 59) {
            printf("Febuary ");
        } else if (months <= 90) {
            printf("March ");
        } else if (months <= 120) {
            printf("April ");
        } else if (months <= 151) {
            printf("May ");
        } else if (months <= 181) {
            printf("June ");
        } else if (months <= 212) {
            printf("July ");
        } else if (months <= 243) {
            printf("August ");
        } else if (months <= 274) {
            printf("September ");
        } else if (months <= 304) {
            printf("October ");
        } else if (months <= 334) {
            printf("November ");
        } else if (months <= 365) {
            printf("December ");
        }
    
    
        if (months <= 31) {
            printf("%d ", day);     /*Find the Day */
        }
        else if (months <= 59) {
            day = day - 31;
            printf("%d ", day);
        } else if (months <= 90) {
            day = day - 59;
            printf("%d ", day);
        } else if (months <= 120) {
            day = day - 90;
            printf("%d ", day);
        } else if (months <= 151) {
            day = day - 120;
            printf("%d ", day);
        } else if (months <= 181) {
            day = day - 151;
            printf("%d ", day);
        } else if (months <= 212) {
            day = day - 181;
            printf("%d ", day);
        } else if (months <= 243) {
            day = day - 212;
            printf("%d ", day);
        } else if (months <= 274) {
            day = day - 243;
            printf("%d ", day);
        } else if (months <= 304) {
            day = day - 274;
            printf("%d ", day);
        } else if (months <= 334) {
            day = day - 304;
            printf("%d ", day);
        } else if (months <= 365) {
            day = day - 334;
            printf("%d ", day);
        } else if (months <= 90) {
            day = day - 59;
            printf("%d ", day);
        }
        printf("%d", ydiffrence);
    
        getchar();
        getchar();
        return 0;
    }
    As for the other problem, analyse your use of months
    $ gcc -W -Wall -ansi -pedantic -O2 foo.c
    foo.c: In function `main':
    foo.c:5: warning: 'months' might be used uninitialized in this function
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 11:31 PM
  4. Do I have a scanf problem?
    By AQWst in forum C Programming
    Replies: 2
    Last Post: 11-26-2004, 06:18 PM
  5. Creation of Menu problem
    By AQWst in forum C Programming
    Replies: 8
    Last Post: 11-24-2004, 09:44 PM