Puzzle DB20


Determine if two integer arrays are equal

[E-6]Two arrays are equal if they are the same length and have the same values at the same indexes.

It does not usually make sense to test if two floating point arrays are equal. Since floating point operations are not precise, the == operation often returns false even when with ideal mathematics it would be true. Use something like the answer to D14 to compare two floating point arrays.

x:
   0    1    2    3    4    5    6    7    8    9
  10   11   12   13   14   15   16   17   18   19
  20   21   22   23   13

y:
   0    1    2    3    4    5    6    7    8    9
  10   11   12   13   14   15   16   17   18   19
  20   21   22   23   24

Arrays are NOT equal

Here is a framework:

int equal( int size, int x[], int y[] )
{
.......
}

int main()
{
  const int SIZE = 30;
  int x[ SIZE ], y[ SIZE ];
  
  fillArrayInOrder( SIZE, x, 0 );
  fillArrayInOrder( SIZE, y, 0 );
  x[SIZE-1] = -99;

  printf("\nx:\n");
  printArray( SIZE, x );
  printf("\n\ny:\n");
  printArray( SIZE, y );

  if ( equal( SIZE, x, y) )
    printf("\n\nArrays are equal\n");
  else
    printf("\n\nArrays are NOT equal\n");
}


Answer         Next Page         Previous Page Home