Monday, May 24, 2010

Please help with my C++ program...PLEASE?

I cant figure out how to properly output the days of the month, I need help with the first day of the year for the year inputed by the user? Help, please...





#include %26lt;iostream%26gt;


#include %26lt;iomanip%26gt;


#include %26lt;cmath%26gt;


using namespace std;





//function prototypes





int First_Day_Of_Month(int y, int m = 1);


int Number_Days_Of_Month(int y, int m);


bool IsLeapYear(int y);


void Print_Version();


void Print_Head(int y);


void Print_Month(int y, int m);


void Print_Month_Head(int m);





// Declare all variables


int firstday, number_days;


int year;


int Leapyear;





void main ()


{


Print_Version();


cin %26gt;%26gt; year;


Print_Head(year);





for(int i=1; i%26lt;=12; i++)


{


Print_Month(year, i);


}


cout %26lt;%26lt; "\nGoodbye! \n";


}





//Some Void Functions





void Print_Version()


{


cout %26lt;%26lt; "Please Enter any Year After 1753 \n";


}


void Print_Head(int y)


{


cout %26lt;%26lt; " " %26lt;%26lt; y %26lt;%26lt; endl;


}


void Print_Month(int y, int m)


{


Print_Month_Head(m);


firstday = First_Day_Of_Month(y,m);


number_days = Number_Days_Of_Month(y,m);


cout %26lt;%26lt; " ";





for (int k=0; k%26lt;firstday; k++)


cout %26lt;%26lt; " ";


for (int i = 1; i%26lt;=number_days; i++)


{


cout %26lt;%26lt; setw(5) %26lt;%26lt; i;


if ((i + firstday)%7 == 0)


{


cout %26lt;%26lt; endl;


cout %26lt;%26lt; " ";


}


}


}


bool IsLeapYear(int y)


{


if (y%400 == 0)


{


return 1;


}


else if (y%4 == 0 %26amp;%26amp; y%100 != 0)


{


return 1;


}


else


return 0;


}


int First_Day_Of_Month(int y, int m)


{


firstday = (3/2 + (year) + (year / 4) -


(year / 100) + (year / 400) + 1) % 7;


return 2;


}


int Number_Days_Of_Month(int y, int m)


{


Leapyear = IsLeapYear(y);


if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)


{


return 31;


}


else if (m == 4 || m == 6 || m == 9 || m == 11)


{


return 30;


}


else if (Leapyear == 1)


{


return 29;


}


else


{


return 28;


}


}


void Print_Month_Head(int m)


{


if(m==1)


{


cout%26lt;%26lt;"\n\n========================...


cout%26lt;%26lt;"\nJanuary\nSun Mon Tue Wed Thu Fri Sat\n";


}


else if(m==2)


{


cout%26lt;%26lt;"\n\n========================...


cout%26lt;%26lt;"\nFebruary\nSun Mon Tue Wed Thu Fri Sat\n";


}


else if(m==3)


{


cout%26lt;%26lt;"\n\n========================...


cout%26lt;%26lt;"\nMarch\nSun Mon Tue Wed Thu Fri Sat\n";


}


else if(m==4)


{


cout%26lt;%26lt;"\n\n========================...


cout%26lt;%26lt;"\nApril\nSun Mon Tue Wed Thu Fri Sat\n";


}


else if(m==5)


{


cout%26lt;%26lt;"\n\n========================...


cout%26lt;%26lt;"\nMay\nSun Mon Tue Wed Thu Fri Sat\n";


}


else if(m==6)


{


cout%26lt;%26lt;"\n\n========================...


cout%26lt;%26lt;"\nJune\nSun Mon Tue Wed Thu Fri Sat\n";


}


else if(m==7)


{


cout%26lt;%26lt;"\n\n========================...


cout%26lt;%26lt;"\nJuly\nSun Mon Tue Wed Thu Fri Sat\n";


}


else if(m==8)


{


cout%26lt;%26lt;"\n\n========================...


cout%26lt;%26lt;"\nAugust\nSun Mon Tue Wed Thu Fri Sat\n";


}


else if(m==9)


{


cout%26lt;%26lt;"\n\n========================...


cout%26lt;%26lt;"\nSeptember\nSun Mon Tue Wed Thu Fri Sat\n";


}


else if(m==10)


{


cout%26lt;%26lt;"\n\n========================...


cout%26lt;%26lt;"\nOctober\nSun Mon Tue Wed Thu Fri Sat\n";


}


else if(m==11)


{


cout%26lt;%26lt;"\n\n========================...


cout%26lt;%26lt;"\nNovember\nSun Mon Tue Wed Thu Fri Sat\n";


}


else


{


cout%26lt;%26lt;"\n\n========================...


cout%26lt;%26lt;"\nDecember\nSun Mon Tue Wed Thu Fri Sat\n";


}


}

Please help with my C++ program...PLEASE?
Does your program even run? I copied and pasted it into my Dev C++ compiler and got lots of compilation errors:





int main(), not void main()





most of your text strings in your prints are not terminated. This might be due to the way Yahoo answers works. I do know that sometimes lines are truncated.
Reply:You need an array of generic 1st days of each month to start:





int day1month = [1,4,4,0,2,5,0,3,6,1,4,6];





there's a better algorithm, but I can only remember the parlor trick version so for dates 1900-2099 in pseudo code:


delyear = year-1900


datecycle = delyear % 7 (mod 7 that is)


leapdays = floor(delyear/4)


day1 = day1month[month-1] (if you use 1-12)


if year is a leap year and month is March or later add 1 to day1


first day of month = (datecycle+leapdays+day1)%7


0 = Sunday through 6=Saturday





I think this is off by 1 for each non-leap year century (1899 or after Feb 2100).





There is a more complete description and better method in one of Knuth's algorithm books, but it's at work. Also, you may want to return the value found as opposed to '2' as your First_Day_Of_Month function.


No comments:

Post a Comment