patterncMinor
Displaying days of the week for all of 2015
Viewed 0 times
2015thealldisplayingweekfordays
Problem
This program is supposed to determine the day of the week for 2015. Could someone tell me how I could shorten my code?
#include
#include
void findDay(int , int);
int main(void)
{
int jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec;
for(jan=1; jan<=31; ++jan)
{
printf(" 01/%d/2015 is", jan);
findDay(jan, 0);
}
for(feb=1; feb<=28; ++feb)
{
printf("02/%d/2015 is", feb);
findDay(feb, 3);
}
for(mar=1; mar<=31; ++mar)
{
printf("03/%d/2015 is", mar);
findDay(mar, 3);
}
for(apr=1; apr<=30; ++apr)
{
printf("04/%d/2015 is", apr);
findDay(apr, 6);
}
for(may=1; may<=31; ++may)
{
printf("05/%d/2015 is", may);
findDay(may, 1);
}
for(jun=1; jun<=30; ++jun)
{
printf("06/%d/2015 is", jun);
findDay(jun, 4);
}
for(jul=1; jul<=31; ++jul)
{
printf("07/%d/2015 is", jul);
findDay(jul, 6);
}
for(aug=1; aug<=31; ++aug)
{
printf("08/%d/2015 is", aug);
findDay(aug, 2);
}
for(sep=1; sep<=30; ++sep)
{
printf("09/%d/2015 is", sep);
findDay(sep, 5);
}
for(oct=1; oct<=31; ++oct)
{
printf("10/%d/2015 is", oct);
findDay(oct, 0);
}
for(nov=1; nov<=30; ++nov)
{
printf("11/%d/2015 is", nov);
findDay(nov, 3);
}
for(dec=1; dec<=31; ++dec)
{
printf("12/%d/2015 is", dec);
findDay(dec, 5);
}
getch();
return 0;
}
void findDay(int dow, int num)
{
int x;
x=(6+15+3+dow+num)%7;//a formula to calculate the day of the week
switch (x)
{
case 0:
printf(" Sunday\n");
break;
case 1:
printf(" Monday\n");
break;
case 2:
printf(" Tuesday\n");
break;
case 3:
printf(" Wednesday\n");
break;
case 4:
printf(" Thursday\n");
break;
case 5:
printf(" Friday\n");
break;
case 6:
printf(" Saturday\n");
break;
default:
printf("Invalid Input");
}
}Solution
Critique
The way to generalize your for-loops is to determine what varies — namely, the number of days in each month and the day of the week that each month starts on. Then, you want to extract the magic numbers into arrays so that your code is data-directed.
The second argument to
Similarly,
Suggested solution
The way to generalize your for-loops is to determine what varies — namely, the number of days in each month and the day of the week that each month starts on. Then, you want to extract the magic numbers into arrays so that your code is data-directed.
The second argument to
findDay() does not make obvious sense. Considering that January 2015 starts on a Thursday, why would you call findDay(jan, 0)? I would expect something like findDay(jan, 4), give or take 1 depending on your numbering convention. Better yet, make your numbering convention clearer by defining an enum.Similarly,
findDay() would be better implemented using a lookup table instead of a switch.findDay() is misnamed and poorly designed. It doesn't just return the result: it also prints it (with a leading space and a trailing newline!). Printing the result limits your ability to ever reuse that function for anything else in the future. (Normally, returning a string from a C function could be a tricky proposition. In this case, you'll be returning constant strings, which is fine.)Suggested solution
#include
typedef enum {
SUN, MON, TUE, WED, THU, FRI, SAT
} Weekday;
static const char *DAY_NAMES[] = {
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
};
static const int MONTH_LENGTHS_NON_LEAP_YEAR[] = {
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
const char *dayOfWeekIn2015(int month, int dayOfMonth) {
static const Weekday DAY0[] = {
WED, // 2014-12-31
SAT, // 2015-01-31
SAT, // 2015-02-28
TUE, // 2015-03-31
THU, // 2015-04-30
SUN, // 2015-05-31
TUE, // 2015-06-30
FRI, // 2015-07-31
MON, // 2015-08-31
WED, // 2015-09-30
SAT, // 2015-10-31
MON // 2015-11-30
};
return DAY_NAMES[(DAY0[month - 1] + dayOfMonth) % 7];
}
int main(void) {
for (int month = 1; month <= 12; month++) {
for (int dayOfMonth = 1; dayOfMonth <= MONTH_LENGTHS_NON_LEAP_YEAR[month - 1]; dayOfMonth++) {
printf("%02d/%02d/2015 is a %s\n",
month, dayOfMonth, dayOfWeekIn2015(month, dayOfMonth));
}
}
}Code Snippets
#include <stdio.h>
typedef enum {
SUN, MON, TUE, WED, THU, FRI, SAT
} Weekday;
static const char *DAY_NAMES[] = {
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
};
static const int MONTH_LENGTHS_NON_LEAP_YEAR[] = {
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
const char *dayOfWeekIn2015(int month, int dayOfMonth) {
static const Weekday DAY0[] = {
WED, // 2014-12-31
SAT, // 2015-01-31
SAT, // 2015-02-28
TUE, // 2015-03-31
THU, // 2015-04-30
SUN, // 2015-05-31
TUE, // 2015-06-30
FRI, // 2015-07-31
MON, // 2015-08-31
WED, // 2015-09-30
SAT, // 2015-10-31
MON // 2015-11-30
};
return DAY_NAMES[(DAY0[month - 1] + dayOfMonth) % 7];
}
int main(void) {
for (int month = 1; month <= 12; month++) {
for (int dayOfMonth = 1; dayOfMonth <= MONTH_LENGTHS_NON_LEAP_YEAR[month - 1]; dayOfMonth++) {
printf("%02d/%02d/2015 is a %s\n",
month, dayOfMonth, dayOfWeekIn2015(month, dayOfMonth));
}
}
}Context
StackExchange Code Review Q#102646, answer score: 2
Revisions (0)
No revisions yet.