• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

Checking 2d arrays for equality

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.
Code:
bool diff = false;

for(int i = 0; i < 10; i++)
{
    for(int j = 0; j < 10; j++)
    {
        if(array1[i][j] != array2[i][j])
        {
            diff = true;
            break;
        }
    }
}

This is assuming you're using C/C++. If C++, this will run fine. However, if you're using C, you have to define true and false and typedef bool.
 
True (err, pardon the bad pun, I didn't mean it honestly), but you probably shouldn't typedef things as names that exist in proper C++ syntax. Perhaps go with Bool, BOOL, myBool, etc.
 
Or just use the old fashioned 1 and 0. :)

Though its not always a good choice, one could also use preprocessor directives to ensure the typedefs don't interfere with a C++ compiler. For example:
Code:
#ifndef __cplusplus
#define true 1
#define false 0
typedef short bool;
#endif /* __cplusplus */
 
Back