I've use a hybrid of the two:
Code:
// This function assumes that month and year are correct values.
// Month is 1..12!
int daysInMonth(int month, int year)
{
static const int dayArray[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int days = dayArray[month-1];
if (month == 2 && leapYear(year))
days = 29;
return days;
}
The reason for the hybrid is that the extra check of month == 2 is probably no bigger than the overhead of the second array, and you still need an if-statement anyways.
Edit: Fix up bug with off-by-one.