Puzzles C11 ... C20

Part C — Arrays

These puzzles involve arrays.


Puzzle C11 — add up all the integers in an array of integers

[E-6]Write a function that computes the sum of all the elements in an array of integers. A related problem is to compute the average of the elements in an array, but that is just a small change to this function. Here is a framework:

/* Puzzle C11 -- add up all the integers in an array of integers */

long addArray( int arr[], int size )
{
.....
}

void printArray( int arr[], int size )
{
  const int N = 10;
  int j;
  
  for ( j=0; j<size; j++ )
  {
    if ( j%N == N-1 )
      printf("%4d\n", arr[j] );
    else
      printf("%4d ", arr[j] );    
  }
}

#define SIZE 10
int main(int argc, char *argv[])
{
  int x[ SIZE ] = { -1, 1, -2, 2, -3, 3, -4, 4, -5, 5 };
  
  printArray( x, SIZE );
  printf("\n");
  printf("sum = %ld\n", addArray( x, SIZE ) );
  
  system("pause");	
  return 0;
}


Puzzle C12 — add up only the ODD integers in an array of integers

[E-6]Write a function that adds up only the odd integers in an array. Remember that negative integers can be odd. Here is some sample output:

   0    2    1   -3   -5    2    4    6    9   60

sum = 2

Modify the previous program to create this one.


Puzzle C13 — compute the sum of the absolute value of the difference between each array element and its index.

[E-8]Write a function that, for each element a[j], adds |a[j]-j| to a sum. Use an array of integers. Typical output:

  -3   -2   -1    0    1    2    3    4    5    6

sum = 30


Puzzle C14 — compute the sum of the squared difference between the elements of two arrays of doubles

[E-6]Write a function that operates on two arrays of double precision floats, a and b. Both arrays are the same size. For each index j, compute (a[j]-b[j])2 and add that to a sum. Output of a test run:

x:
  2.0000   0.0000  -3.0000   0.0000   0.0000
  0.0000   0.0000   0.0000   0.0000   0.0000
y:
  2.0000  -2.0000  -3.0000   2.0000   2.0000
  2.0000  -2.0000   2.0000   2.0000   2.0000

sum = 32.000000

For further testing, initialize the arrays with random values using puzzle C07 or C10. Here is a testing framework:

double squareDiffArray( double a[], double b[], int size )
{
...
}

void printArrayDouble( double arr[], int size )
{
  const int N = 5;
  int j;

  for ( j=0; j<size; j++ )
  {
    if ( j%N == N-1 )
      printf("%8.4lf\n", arr[j] );
    else
      printf("%8.4lf ", arr[j] );
  }
}

#define SIZE 10
int main(int argc, char *argv[])
{
  double x[ SIZE ] = { 2,0,-3,0,0,0,0,0,0,0 };
  double y[ SIZE ] = { 2.0, -2.0, -3.0, 2.0,2.0,2.0, -2.0,2.0,2.0,2.0  };

  printf("x:\n");
  printArrayDouble( x, SIZE );
  printf("y:\n");
  printArrayDouble( y, SIZE );
  printf("\n");
  printf("sum = %lf\n", squareDiffArray( x, y, SIZE ) );
  
  system("pause");	
  return 0;
}


Puzzle C15 — print every N'th element in an integer array, starting with element 0

[E-8]Write a function that prints every N'th integer of an array starting with element 0. This might be used to look at a big array when you just want to check that all the elements are there.

A more elaborate function would randomly skip forward an amount 0..N for each element it prints.

To test your code, initialize the array using C04 or C09.


Puzzle C16 — multiply every element in an integer array by 'scale'

[E-4]Write a function that muliplies every element in its argument array by an integer scale factor.


Puzzle C17 — multiply corresponding elements in two integer arrays to determine the elements of a third

[E-4]Write a function that has three integer arrays as argments. The function multiplies corresponding elements of two of first two arrays to compute the elements of the third array. All three arrays are the same size. Sample output:

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   24

y:
  11   10    9    8    7    6    5    4    3    2
   1    0   -1   -2   -3   -4   -5   -6   -7   -8
  -9  -10  -11  -12  -13

z:
   0   10   18   24   28   30   30   28   24   18
  10    0  -12  -26  -42  -60  -80 -102 -126 -152
-180 -210 -242 -276 -312

The first two arrays were initialized using fillArrayInOrder() and fillArrayDescending().


Puzzle C18 — make every integer in an array even by increasing the magnitude of all odd elements

[E-8]Write a function that inspects each integer of an array and changes the odd integers to even integers. Add -1 to odd integers less than one; add +1 to odd integers more than one. Typical output is:

 -12  -11  -10   -9   -8   -7   -6   -5   -4   -3
  -2   -1    0    1    2    3    4    5    6    7
   8    9   10   11   12


 -12  -12  -10  -10   -8   -8   -6   -6   -4   -4
  -2   -2    0    2    2    4    4    6    6    8
   8   10   10   12   12


Puzzle C19 — clamping: change all integers greater than X to X and all integers less than -X to -X

[E-6]Change an integer array so that all elements lie within the range -X..X, inclusive. Do this by setting all elements that are greater than X to X, and setting all elements that are less than -X to -X.

void clamp( int arr[], int size, int x )
{
....
}

void fillArrayInOrder( int arr[], int size, int start )
{
  int j;
  
  for ( j=0; j<size; j++ )
  {
    arr[j] = j+start;
  }
}

void printArray( int arr[], int size )
{
  const int N = 10;
  int j;
  
  for ( j=0; j<size; j++ )
  {
    if ( j%N == N-1 )
      printf("%4d\n", arr[j] );
    else
      printf("%4d ", arr[j] );    
  }
}

int main(int argc, char *argv[])
{
  const int SIZE = 25;
  int x[ SIZE ];
  
  fillArrayInOrder( x, SIZE, -SIZE/2 );
  printArray( x, SIZE );
  printf("\n\n");
  clamp( x, SIZE, SIZE/4 );
  printArray( x, SIZE );
    
  printf("\n\n");
  system("pause");	
  return 0;
}


Puzzle C20 — 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 C14 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 x[], int y[], int size )
{
.......
}

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

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

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


— Return to the main contents page