# Thread: Mayan Calander Conversion Issue

1. ## 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?

2. 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