More Pointer Exercises
8.20 |
Modify the card shuffling and dealing program of Figs. 8.258.27 so the shuffling and dealing operations are performed by the same function (shuffleAndDeal). The function should contain one nested looping statement that is similar to function shuffle in Fig. 8.26. |
||
8.21 |
What does this program do? 1 // Ex. 8.21: ex08_21.cpp 2 // What does this program do? 3 #include 4 using std::cout; 5 using std::cin; 6 using std::endl; 7 8 void mystery1( char *, const char * ); // prototype 9 10 int main() 11 { 12 char string1[ 80 ]; 13 char string2[ 80 ]; 14 15 cout << "Enter two strings: "; 16 cin >> string1 >> string2; 17 mystery1( string1, string2 ); 18 cout << string1 << endl; 19 return 0; // indicates successful termination 20 } // end main 21 22 // What does this function do? 23 void mystery1( char *s1, const char *s2 ) 24 { 25 while ( *s1 != '' ) 26 ++s1; 27 28 for ( ; *s1 = *s2; s1++, s2++ ) 29 ; // empty statement 30 } // end function mystery1 |
||
8.22 |
What does this program do? 1 // Ex. 8.22: ex08_22.cpp 2 // What does this program do? 3 #include 4 using std::cout; 5 using std::cin; 6 using std::endl; 7 8 int mystery2( const char * ); // prototype 9 10 int main() 11 { 12 char string1[ 80 ]; 13 14 cout << "Enter a string: "; 15 cin >> string1; 16 cout << mystery2( string1 ) << endl; 17 return 0; // indicates successful termination 18 } // end main 1920 // What does this function do? 21 int mystery2( const char *s ) 22 { 23 int x; 24 25 for ( x = 0; *s != ''; s++ ) 26 ++x; 27 28 return x; 29 } // end function mystery2 |
||
8.23 |
Find the error in each of the following segments. If the error can be corrected, explain how.
|
||
8.24 |
(Quicksort) You have previously seen the sorting techniques of the bucket sort and selection sort. We now present the recursive sorting technique called Quicksort. The basic algorithm for a single-subscripted array of values is as follows:
Each time Step 1 is performed on a subarray, another element is placed in its final location of the sorted array, and two unsorted subarrays are created. When a subarray consists of one element, that subarray must be sorted; therefore, that element is in its final location. The basic algorithm seems simple enough, but how do we determine the final position of the first element of each subarray? As an example, consider the following set of values (the element in bold is the partitioning elementit will be placed in its final location in the sorted array): 37 2 6 4 89 8 10 12 68 45
Once the partition has been applied to the array, there are two unsorted subarrays. The subarray with values less than 37 contains 12, 2, 6, 4, 10 and 8. The subarray with values greater than 37 contains 89, 68 and 45. The sort continues with both subarrays being partitioned in the same manner as the original array. Based on the preceding discussion, write recursive function quickSort to sort a single-subscripted integer array. The function should receive as arguments an integer array, a starting subscript and an ending subscript. Function partition should be called by quickSort to perform the partitioning step. |
||
8.25 |
(Maze Traversal) The grid of hashes (#) and dots (.) in Fig. 8.44 is a two-dimensional array representation of a maze. In the two-dimensional 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 array that contains a dot. Figure 8.44. Two-dimensional array representation of a maze.
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 will 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 will arrive at the exit of the maze. There may be a shorter path than the one you have 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 character array 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. |
||
8.26 |
(Generating Mazes Randomly) Write a function mazeGenerator that takes as an argument a two-dimensional 12-by-12 character array and randomly produces a maze. The function should also provide the starting and ending locations of the maze. Try your function mazeTraverse from Exercise 8.25, using several randomly generated mazes. |
||
8.27 |
(Mazes of Any Size) Generalize functions mazeTraverse and mazeGenerator of Exercise 8.25 and Exercise 8.26 to process mazes of any width and height. |
||
8.28 |
(Modifications to the Simpletron Simulator) In Exercise 8.19, you wrote a software simulation of a computer that executes programs written in Simpletron Machine Language (SML). In this exercise, we propose several modifications and enhancements to the Simpletron Simulator. In Exercise 21.26 and Exercise 21.27, we propose building a compiler that converts programs written in a high-level programming language (a variation of BASIC) to SML. Some of the following modifications and enhancements may be required to execute the programs produced by the compiler. [Note: Some modifications may conflict with others and therefore must be done separately.]
|
||
|
|||
8.29 |
What does this program do?
|