Fixed: I had to do it with a pointer to call the values(I also changed the formular but I´m sure the old one would also work. The problem was the missing pointer):
sAppointment Calendar[MAXAPPOINTMENTS];
int dayOfWeek(sDate *date){
static int jMonth[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
int d = date->Day;
int m = date->Month;
int y = date->Year;
if(m < 3) y--;
return(y + (y/4) - (y/100) + (y/400) + jMonth[m - 1] + d) % 7;
}
I am writing a C Program which has a datastructure in which the variables of a date can be saved but it does not work for the weekday. It appears an error: "expression must have integral type"
The function is build like this, including a formular to calculate the weekday:
void dayOfWeek(sDate date){
if(date.Month > 3) date.Year = date.Year - 1;
date.WeekDay = ((date.Day + floor (2.6 * ((date.Month + 9) % 12 + 1) - 0.2) + date.Year % 100 + floor (date.Year % 100 / 4) + floor (date.Year / 400) - 2 * floor (date.Year / 100) - 1) % 7 + 7) % 7 + 1;
}
The date datastructure contents int values which looks like this (german language):
typedef enum {So, Mo, Di, Mi, Do, Fr, Sa} eDayofTheWeek;
typedef struct{
int Day;
int Month;
int Year;
eDayofTheWeek WeekDay;
} sDate;
I tried to set most of the formular to a variable which first containts everything before the modulo operator
int w;
w = ((date.Day + floor (2.6 * ((date.Month + 9) % 12 + 1) - 0.2) + date.Year % 100 + floor (date.Year % 100 / 4) + floor (date.Year / 400) - 2 * floor (date.Year / 100) - 1);
date.Weekday =( w % 7 + 7) % 7 + 1;
This only led to the weekday value being 0. Other formulars also let the value of being 0.
OP's code has at least these problems:
No update for caller
void dayOfWeek(sDate date){does not provide any result back to the caller ("This only led to the weekday value being 0"). Instead, pass by reference.%not valid with floating pointBefore applying
%, convert the value to anint. Note: C does not has a modulo operator but a remainder operator.Wrong test
I was unable to fully fix OP's code.
Consider avoiding one-liner code and instead provide more clarity.
Below works for month and day outside the usual range.
Note that
DaysPer400Yearsis a multiple of 7 (for the Gregorian Calendar). Thus we can take short cuts and start withyear %= 400to work with smaller numbers.