I want to post solutions for all, but I won't. The leap year one is very, very easy, though! What is unique about a leap year? It is divisible by four exactly, so if:
I will contain myself now...Code:if(year % 4 == 0) // we have a leap year! else // we don't have a leap year.![]()
The catch being that if it is divisible by 100, it must also be divisible by 400 to be a leap year.
Indeed, Winged. This is why I gave a hint to look into Wikipedia. Mathematix, you earned some -rep for yourself. Giving wrong (unconfirmed) intel is as good as kicking someone's face.
See some pseudocode: Leap year - Wikipedia, the free encyclopedia
proudly presenting my personal website and game website: F1Simulation. a thrilling Managed DirectX racing game... also my Ask Me
look at my tutorials about cropping images and Mono: bundling Mono with programs and lambda expressions
Here it is. Is this what you would like to see? Honestly, I prefer the DateTime class to do the job. Just between you and me, I do not like to give people cooked-ready solutions. I prefer just to give clues. Oh, forgot to add that this is C#.
Code:static void Main(string[] args)
{
for (int year = 1980; year < 2009; year++)
{
Console.WriteLine("IsYearLeap says {0} when DateTime says {1}",
IsYearLeap(year), DateTime.IsLeapYear(year));
}
}
private static bool IsYearLeap(int year)
{
if (year % 400 == 0)
return true;
if (year % 100 == 0)
return false;
if (year % 4 == 0)
return true;
return false;
}
Last edited by ArekBulski; 06-26-2009 at 08:07 AM.
proudly presenting my personal website and game website: F1Simulation. a thrilling Managed DirectX racing game... also my Ask Me
look at my tutorials about cropping images and Mono: bundling Mono with programs and lambda expressions
At last I had some time to knock up some code. I used VC++ 2008 to build as those who are curious can build the code without having to purchase a nice IDE!
I've left it monolithic so that it's easier to work through. given that this is a homework assignment I've made the commenting rather scant forcing the OP to do some significant analysis. Am happy to take questions, though.Code:#include <stdio.h> #include <string.h> #include <malloc.h> #define GEORGIAN_LEAP 4 #define NOT_LEAP_YEAR 100 #define IS_LEAP_YEAR 400 #define HAS_DAYS_31 31 #define HAS_DAYS_30 30 #define HAS_DAYS_28 28 #define HAS_DAYS_29 29 const char* months[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; struct monthDays { char* mths; int numdays; }; int main() { // We initialise all months apart from Febuary until we determine a leap year! monthDays yearMonths[12]; for(int i = 0; i < 12; i++) { yearMonths[i].mths = (char*)months[i]; if(i == 1) // DON'T set February number of days yet! { continue; } // Fill the months and associated days! if(strstr(yearMonths[i].mths, months[0]) || // January strstr(yearMonths[i].mths, months[2]) || // March strstr(yearMonths[i].mths, months[4]) || // May strstr(yearMonths[i].mths, months[6]) || // July strstr(yearMonths[i].mths, months[7]) || // August strstr(yearMonths[i].mths, months[9]) || // October strstr(yearMonths[i].mths, months[11])) // December { yearMonths[i].numdays = HAS_DAYS_31; continue; } if(strstr(yearMonths[i].mths, months[3]) || // April strstr(yearMonths[i].mths, months[5]) || // June strstr(yearMonths[i].mths, months[8]) || // September strstr(yearMonths[i].mths, months[10])) // November { yearMonths[i].numdays = HAS_DAYS_30; continue; } printf("%s has %d days.\n", yearMonths[i].mths, yearMonths[i].numdays); } // seed the year and days entered by user. int year = 0, days = 0; puts("Enter a year: "); scanf("%i", &year); puts("Enter the days: "); scanf("%i", &days); int daysSum = 0; int exactDate = 0; char* currentMonth = (char *) malloc(sizeof(char) * 30); if((year % GEORGIAN_LEAP) == 0) // We've fulfilled the condition for a 'Georgian leap' { if(((year % NOT_LEAP_YEAR) != 0) || ((year % IS_LEAP_YEAR) == 0)) // Other conditions fulfilled. { yearMonths[1].numdays = HAS_DAYS_29; // Feb has 29 days in a leap year. printf("We have a leap year! (%d)\n", year); // Work out the date. int i = 0; while(i < 11) { if((daysSum + yearMonths[i].numdays) >= days) { break; } daysSum += yearMonths[i].numdays; i++; } currentMonth = yearMonths[i].mths; if((days - daysSum) == 0) exactDate = 31; else exactDate = days - daysSum; printf("It is %s %d\n", currentMonth, exactDate); } } else { yearMonths[1].numdays = HAS_DAYS_28; // Feb has 28 days in a common year. printf("We do not have a leap year, alas... (%d)\n", year); // Work out the date. int i = 0; while(i < 12) { if((daysSum + yearMonths[i].numdays) >= days) { break; } daysSum += yearMonths[i].numdays; if(i < 11) i++; } currentMonth = yearMonths[i].mths; if((days - daysSum) == 0) exactDate = 31; else exactDate = days - daysSum; printf("It is %s %d\n", currentMonth, exactDate); } }
Also feel free to point out any bugs and I might fix them.
He he, actually none of our solutions is exactly an answer. You see, this is a Managed C++ section and neither of our code is in it.
proudly presenting my personal website and game website: F1Simulation. a thrilling Managed DirectX racing game... also my Ask Me
look at my tutorials about cropping images and Mono: bundling Mono with programs and lambda expressions
I beg to differ. If the OP can't handle such problems, I doubt they know the difference between managed and non-managed C++ and have actually posted on the wrong forum.
Secondly given the it was mentioned at the end that they can only use loops, arrays and if-statements, none of those are specific to managed code.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks