Puzzles C31 ... C40

Part C — Arrays

These puzzles involve rearranging the order of the elements in an array.


Puzzle C31 — reverse the elements in an array of integers

[E-7] Write function that reverses the order of the elements in an array of integers. Here is a sample run:

Original:
   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   25   26   27   28   29
  30   31   32   33   34   35   36   37   38   39

Reversed:
  39   38   37   36   35   34   33   32   31   30
  29   28   27   26   25   24   23   22   21   20
  19   18   17   16   15   14   13   12   11   10
   9    8    7    6    5    4    3    2    1    0

Make sure the function works on arrays with an even and an odd number of elements. Here is a testing framework: Testing Framework


Puzzle C32 — reverse the elements in an array between indexes Low and High

[E-10] Write function that reverses the order of the elements in an integer array between two indexes, low and high. Decide what your function should do for indexes out of bounds, or if high<low. Here is output from a test program:

   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   25   26   27   28   29

Reversing between 15 and 24
   0    1    2    3    4    5    6    7    8    9
  10   11   12   13   14   24   23   22   21   20
  19   18   17   16   15   25   26   27   28   29

Here is a testing framework: Testing Framework


Puzzle C33 — shift array elements one position to the right

[E-5] Write function that moves each element of the array up one position. Element 0 becomes a zero, and the last element of the array is discarded. Here is an example:

   0    1    2    3    4    5    6    7    8    9

Shifted Right One:
   0    0    1    2    3    4    5    6    7    8

Here is a testing framework: Testing Framework


Puzzle C34 — rotate array elements one position right

[E-7] Write a function that moves each element of an array to the next highest index. Unlike a right shift, the value at the highest index is put into the array at index 0. The array contains the same values as it starts with, but each value is in a new position. Here is an example:

   0    1    2    3    4    5    6    7    8    9

Rotated Right by One:
   9    0    1    2    3    4    5    6    7    8

Here is a testing framework: Testing Framework


Puzzle C35 — shift array elements one position to the left

[E-5] Write a function that shifts each element of an array down by one position. Replace the element in size-1 with a zero. The original element in slot zero is lost. Here is an example:

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

Here is a testing framework: Testing Framework


Puzzle C36 — rotate array elements one position left

[E-7] Write function that rotates array elements left by one position. The value originally in slot 0 is moved to slot n-1.

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

Here is a testing framework: Testing Framework


Puzzle C37 — shift array elements N positions right; the first N elements get 0

[M-12] Write a function that moves each element in an integer array N positions to the right. The first N positions are filled with zero, and the last N elements are discarded.

 777    1    2    3    4    5    6    7    8    9
  10   11   12   13   14   15   16   17   18   19

Right Shifted by 5 positions:
   0    0    0    0    0  777    1    2    3    4
   5    6    7    8    9   10   11   12   13   14

Decide what to do if the shift amount is negative or zero, or larger than the size of the array.

An easy way to do this is to call the function that shifts by one position N times. But this is N times slower than an efficient implementation. Try to come up with an efficient way to do this.

Here is a testing framework: Testing Framework


Puzzle C38 — shift array elements N positions left; the high N elements get 0

[M-10] Write a function that moves array elements N positions left. The first N entries are discarded, and the last N elements are set to zero.

   0    1    2    3    4    5    6    7    8    9
  10   11   12   13   14   15   16   17   18   19

Shift Left 7 Positions:
   7    8    9   10   11   12   13   14   15   16
  17   18   19    0    0    0    0    0    0    0

Here is a testing framework: Testing Framework


Puzzle C39 — rotate array elements N positions right

[M-10] Write a function that rotates an integer array right by N positions. If N is negative, rotate N positions left. If N is greater than the size of the array, change N to N%size.

   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   25   26   27   28   29

Rotated Right by 10:
  20   21   22   23   24   25   26   27   28   29
   0    1    2    3    4    5    6    7    8    9
  10   11   12   13   14   15   16   17   18   19

The easy version--repeatedly rotating right N times--is not efficient. But the other ways of doing this are hard. Write the easy version first so that you can test your efficient version [H-15]against it.

Here is a testing framework: Testing Framework


Puzzle C40 — rotate every array element N positions left

[M-10] Write a function that rotates an integer array left by N positions. If N is negative, rotate N positions right. If N is greater than the size of the array, change N to N%size.

   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   25   26   27   28   29

Rotated Left by 10:
  10   11   12   13   14   15   16   17   18   19
  20   21   22   23   24   25   26   27   28   29
   0    1    2    3    4    5    6    7    8    9

You can do this puzzle with some small changes to the solutions to C39, or see if you can create a completely different way to rotate [H-15].

Here is a testing framework: Testing Framework


--- Return to the main contents page