I have some code. I use Visual Studio to compile, and since it doesn't let you compile an already compiled program, I just remove and replace a semi-colon or something like that so it thinks it's different and lets me recompile.
The code is about Project Euler problem 11 (https://projecteuler.net/problem=11). I compile it and it gives a correct answer, then I recompile and it gives a wrong answer. The wrong answer appears to be like calling an integer in an array out of bounds. And it also only compiles into a broken state when I remove and put a semi-colon in a different place than the last time. I've noticed it only happens on some of the arrays (I pointed them out in code comments). I've tried some other things but they didn't break it.
long long ULEvenify(long long crntNum)
{
if ((crntNum / 10) % 2 != 0) return +10;
return crntNum;
}
template<size_t N>
long long GridGP(long long (&grid)[N]) //Stands for Grid Greatest Product; https://projecteuler.net/problem=11
{
long long gridSize = sizeof(grid) / sizeof(*grid);
long long digitProd = 1; //Digit Product
long long potDigProd = 1; //Potential Digit Product
for (long long numLocation = 0; numLocation < gridSize - 59; numLocation++) //-31 so that it doesn't continue onto the last 3 rows
{
cout << "NEW" << "\n";
cout << numLocation << "\n";
potDigProd = 1;
if (numLocation >= (((numLocation / 10) * 10) + 3)) //Left Diagonal
{
for (long long i = 0; i <= 3; i++) //Times 4 not 3, so that it includes the first number in the series of 4 adjacent numbers
{
cout << numLocation + ((i * 10) - i) << "a" << "\n";
cout << "GRID: " << grid[numLocation + ((i * 20L) - i)] << ", " << grid[numLocation] << "; ";
potDigProd *= grid[numLocation + ((i * 20L) - i)]; //HERE
}
if (potDigProd > digitProd) digitProd = potDigProd;
potDigProd = 1;
}
if (numLocation < ((ULEvenify(numLocation) / 10) * 10) + 7 && numLocation > 59) //Right Diagonal
{
for (long long i = 0; i <= 3; i++)
{
potDigProd *= grid[(numLocation - ((i * 20L)) + i)]; //HERE
}
if (potDigProd > digitProd) digitProd = potDigProd;
potDigProd = 1;
}
for (long long i = 0; i <= 3; i++) //Down, doesn't requre an if statement to check if it's beyond the last 4th row because the for loop doesn't go beyond the last 4th row
{
potDigProd *= grid[numLocation + (i * 20L)]; //HERE
}
if (potDigProd > digitProd) digitProd = potDigProd;
potDigProd = 1;
if (numLocation > 59)
{
for (long long i = 0; i <= 3; i++)
{
potDigProd *= grid[numLocation - (i * 20L)]; //HERE
}
if (potDigProd > digitProd) digitProd = potDigProd;
potDigProd = 1;
}
}
return digitProd;
}
The main()
method just has the grid from Problem 11 as an int array
and a cout << GridGP(grid20x2);
statement.
I just don't understand how this makes any sense! I'm changing nothing of significance, and it only breaks when I "change nothing" in a different place from a set of places than the last time. Why does this happen? What is this? Schrodinger's code? Before compilation, both broken and working, and on compilation collapses to one state?
Note: Yes, I know the code is bad and there's a better way for Problem 11, but that's not the point. The point is, how can the answer be sometimes correct and sometimes wrong?