[Solved] C++ total beginner needs guidance [closed]


I have not spent effort in trying to understand your algorithm, but at first glance it looks more complicated than it should be.

From my understanding of the problem, there are 3 possibilities:

  • the totals of the upper halves and the lower halves are already even (so nothing needs to be done)
  • the totals of the upper halves and the lower halves cannot be made even (so no solution exists)
  • just one Domino needs to be rotated to get the totals of the upper halves and the lower halves to be even (so the time needed is 1 second)

I base this on the fact that adding only even numbers always gives an even result, and adding an even number of odd numbers also always gives an even result.

Based on this, instead of having a 2-dimensional array like in your code, I would maintain 2 distinct arrays – one for the upper half numbers and the other for the lower half numbers. In addition, I would write the following two helper functions:

  1. oddNumCount – takes an array as input; simply returns the number of odd numbers in the array.
  2. oddAndEvenTileExists – takes 2 arrays as input; returns the index of the first tile with an odd+even number combination, -1 if no such tile exists.

Then the meat of my algorithm would be:

if (((oddNumCount(upper_half_array) % 2) == 0) && ((oddNumCount(lower_half_array) % 2) == 0))
{
    // nothing needs to be done

    result = 0;
}
else if (((oddNumCount(upper_half_array) - oddNumCount(lower_half_array)) % 2) == 0)
{
    // The difference between the number of odd numbers in the two halves is even, which means a solution may exist.
    // A solution really exists only if there exists a tile in which one number is even and the other is odd.

    result = (oddAndEvenTileExists(upper_half_array, lower_half_array) >= 0) ? 1 : -1;
}
else
{
    // no solution exists.

    result = -1;
}

If you wanted to point out exactly which tile needs to be rotated, then you can save the index that “oddAndEvenTileExists” function returns.

You can write the actual code yourself to test if this works. Even if it doesn’t, you would have written some code that hopefully takes you a little above “total beginner”.

2

solved C++ total beginner needs guidance [closed]