7.28 (Palindromes) A palindrome is a string that’s spelled the same way forward and backward. Examples of palindromes include “radar” and “able was i ere i saw elba.” Write a recursive function testPalindrome
that returns true
if a string
is a palindrome, and false
otherwise. Note that like an array
, the square brackets ([]
) operator can be used to iterate through the characters in a string
.
7.29 (Eight Queens) Modify the Eight Queens program you created in Exercise 7.24 to solve the problem recursively.
7.30 (Print an array
) Write a recursive function printArray
that takes an array
, a starting subscript and an ending subscript as arguments, returns nothing and prints the array
. The function should stop processing and return when the starting subscript equals the ending subscript.
7.31 (Print a String Backward) Write a recursive function stringReverse
that takes a string
and a starting subscript as arguments, prints the string backward and returns nothing. The function should stop processing and return when the end of the string is encountered. Note that like an array
the square brackets ([]
) operator can be used to iterate through the characters in a string
.
7.32 (Find the Minimum Value in an array
) Write a recursive function recursiveMinimum
that takes an integer array
, a starting subscript and an ending subscript as arguments, and returns the smallest element of the array
. The function should stop processing and return when the starting subscript equals the ending subscript.
7.33 (Maze Traversal) The grid of hashes (#
) and dots (.
) in Fig. 7.27 is a two-dimensional builtin array representation of a maze. In the two-dimensional built-in array, the hashes represent the walls of the maze and the dots represent squares in the possible paths through the maze. Moves can be made only to a location in the built-in array that contains a dot.
There is a simple algorithm for walking through a maze that guarantees finding the exit (assuming that there is an exit). If there is not an exit, you’ll arrive at the starting location again. Place your right hand on the wall to your right and begin walking forward. Never remove your hand from the wall. If the maze turns to the right, you follow the wall to the right. As long as you do not remove your hand from the wall, eventually you’ll arrive at the exit of the maze. There may be a shorter path than the one you’ve taken, but you are guaranteed to get out of the maze if you follow the algorithm.
Write recursive function mazeTraverse
to walk through the maze. The function should receive arguments that include a 12-by-12 built-in array of char
s representing the maze and the starting location of the maze. As mazeTraverse
attempts to locate the exit from the maze, it should place the character X
in each square in the path. The function should display the maze after each move, so the user can watch as the maze is solved.
7.34 (Generating Mazes Randomly) Write a function mazeGenerator
that randomly produces a maze. The function should take as arguments a two-dimensional 12-by-12 built-in array of char
s and references to the int
variables that represent the row and column of the maze’s entry point. Try your function mazeTraverse
from Exercise 7.33, using several randomly generated mazes.