ICodeNow - 10 months ago 50

C Question

I am halfway done with converting this function from c to c# but I am totally lost as to what the middle computational line does.

`y -= m < 3;`

C Version

`int DayOfWeek(int d, int m, int y)`

{

static int t[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };

y -= m < 3;

return (y + y / 4 - y / 100 + y / 400 + t[m - 1] + d) % 7;

}

C# Version

`int DayOfWeek(int d, int m, int y)`

{

int[] t = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };

y -= m < 3;

return (y + y / 4 - y / 100 + y / 400 + t[m - 1] + d) % 7;

}

1) I understand y -= m is equivalent to y = y - m but what does "<" do? Is it an expression?

2) How do I convert the rest of this to c#?

Thank you everyone, for clarification this is for a degree program.

Answer Source

```
y -= (m < 3) ? 1 : 0;
```

or more readibly

```
if (m < 3) y -= 1;
```

or even

```
if (m < 3) --y;
```

It's some C code using the fact that conditionals return 1 or 0 as a value. Fix as shown.

Edit: I now see the comments saying this code is obfuscated. I guess I'm such an old C hat I found it immediately obvious on reading it, not that I recommend writing it that way.

Or we can do this thing the really ovbious way:

```
int DayOfWeek(int d, int m, int y) => (new DateTime(y, m, d)).DayOfWeek;
```