1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
#include <cstdlib>
#include <iostream>
using namespace std;
bool check( int* a, int* b, int n );
int main()
{
int a[] = {1,2,3,4,5};
// int b[] = {1,2,3,4,5,6}; // try the different variants
// int b[] = {1,2,3,4,5};
int b[] = {5,4,3,2,1};
// Length check
int na = sizeof( a )/ sizeof( a[0] );
int nb = sizeof( b )/ sizeof( b[0] );
if ( na != nb )
{
cout << "The arrays are unequal length\n";
return 0;
}
int *p = a;
int *q = b;
cout << "Arrays are equal? " << boolalpha << check( p, q, na ) << endl;
}
bool check( int* a, int* b, int n )
{
while( n > 0 && *a == *b ) { n--; a++; b++; }
// while( n && *a++ == *b++ ) n--; // shorter, but very obscure
return ( n == 0 );
}
|