N
Nachy
Good (evening, morning, afternoon)
I have the following construct in my program. Some may remember this
from the first time I posted on C.L.C. a coupla years ago.
Effectively, it's a calendar program. This function takes as arguments
the day of the week of the beginning of the year - in my case, the
upcoming Jewish New Year - and has to add a certain number of days to
get the day of the week on which the first day of any other month
falls. (Sorry, I know that's a bit garbled.)
I have two ways of doing it.
1) A switch statement:
int new_month(int month, int new_year, int leap)
{
switch (month) {
case DECEMBER: new_year += 2; /* Fall through all */
case NOVEMBER: new_year += 3;
case OCTOBER: new_year += 2;
case SEPTEMBER: new_year += 3;
case AUGUST: new_year += 3;
case JULY: new_year += 2;
case JUNE: new_year += 3;
case MAY: new_year += 2;
case APRIL: new_year += 3;
case MARCH: new_year += 0;
case FEBRUARY: new_year += 3;
case JANUARY : new_year += 0;
}
if (month > FEBRUARY && leap == true) new_year++;
return new_year % 7;
}
2) An array:
int new_month(int month, int new_year, int leap)
{
int mo_array[12] = {0, 3, 3, 6, 8, 11, 13, 16, 19, 21, 24, 26} ;
new_year += mo_array[month]);
if (month > FEBRUARY && leap == true) new_year++;
return new_year % 7;
}
The first example may cause larger code (or will it?) but doesn't have
the same kind of magic numbers the second example has. Also, the
second example has local data - the array is automatic, and must be
copied from data each time the function is run, I think, which may be
slower. On the other hand, a switch statement can be slow if it
compiles as an if/else construct, or can a call table be used?
My question, in effect, is: which would you advise? Especially taking
into account (those that do understand) that for a Jewish calendar the
leap year and months work somewhat differently - but that is beyond
the scope of the question here.
For those seeking to pick nits: imaging an enum months {JANUARY, ...
DECEMBER} in scope.
Thanks, and I hope I wasn't too unclear.
-- Nachy
"Conspiracy Creationist Insists Everything Part of God's Plot" -- The
Onion
I have the following construct in my program. Some may remember this
from the first time I posted on C.L.C. a coupla years ago.
Effectively, it's a calendar program. This function takes as arguments
the day of the week of the beginning of the year - in my case, the
upcoming Jewish New Year - and has to add a certain number of days to
get the day of the week on which the first day of any other month
falls. (Sorry, I know that's a bit garbled.)
I have two ways of doing it.
1) A switch statement:
int new_month(int month, int new_year, int leap)
{
switch (month) {
case DECEMBER: new_year += 2; /* Fall through all */
case NOVEMBER: new_year += 3;
case OCTOBER: new_year += 2;
case SEPTEMBER: new_year += 3;
case AUGUST: new_year += 3;
case JULY: new_year += 2;
case JUNE: new_year += 3;
case MAY: new_year += 2;
case APRIL: new_year += 3;
case MARCH: new_year += 0;
case FEBRUARY: new_year += 3;
case JANUARY : new_year += 0;
}
if (month > FEBRUARY && leap == true) new_year++;
return new_year % 7;
}
2) An array:
int new_month(int month, int new_year, int leap)
{
int mo_array[12] = {0, 3, 3, 6, 8, 11, 13, 16, 19, 21, 24, 26} ;
new_year += mo_array[month]);
if (month > FEBRUARY && leap == true) new_year++;
return new_year % 7;
}
The first example may cause larger code (or will it?) but doesn't have
the same kind of magic numbers the second example has. Also, the
second example has local data - the array is automatic, and must be
copied from data each time the function is run, I think, which may be
slower. On the other hand, a switch statement can be slow if it
compiles as an if/else construct, or can a call table be used?
My question, in effect, is: which would you advise? Especially taking
into account (those that do understand) that for a Jewish calendar the
leap year and months work somewhat differently - but that is beyond
the scope of the question here.
For those seeking to pick nits: imaging an enum months {JANUARY, ...
DECEMBER} in scope.
Thanks, and I hope I wasn't too unclear.
-- Nachy
"Conspiracy Creationist Insists Everything Part of God's Plot" -- The
Onion