This is somehow surprising the first time you see it and it is interesting to remind it to people from time to time. The question is easy, does this program always return 0? That is, are those summations the same?
If there’s a case when it doesn’t, provide a sample input and explain why it happens.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
float f[argc - 1];
for (int i = 1; i < argc; i++)
f[i - 1] = atof(argv[i]);
float r1 = 0;
for (int i = 0; i < argc - 1; i++)
r1 += f[i];
float r2 = 0;
for (int i = argc - 2; i >= 0; i--)
r2 += f[i];
return r1 != r2;
}
As usual, I’ll post the solution in a couple of days or when someone gets it right (which is always the case :))
— fpereda
Update: correct typo spotted by Snaury.