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 19
20 // 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.

  1. int *number; cout << number << endl;
  2. double *realPtr; long *integerPtr; integerPtr = realPtr;
  3. int *x, y; x = y;
  4. char s[] = "this is a character array"; for ( ; *s != ''; s++) cout << *s << ' ';
  5. short *numPtr, result; void *genericPtr = numPtr; result = *genericPtr + 7;
  6. double x = 19.34; double xPtr = &x; cout << xPtr << endl;
  7. char *s; cout << s << endl;
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:

  1. Partitioning Step: Take the first element of the unsorted array and determine its final location in the sorted array (i.e., all values to the left of the element in the array are less than the element, and all values to the right of the element in the array are greater than the element). We now have one element in its proper location and two unsorted subarrays.
  2. Recursive Step: Perform Step 1 on each unsorted subarray.

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
 
  1. Starting from the rightmost element of the array, compare each element with 37 until an element less than 37 is found. Then swap 37 and that element. The first element less than 37 is 12, so 37 and 12 are swapped. The values now reside in the array as follows:

    12 2 6 4 89 8 10 37 68 45
    

    Element 12 is in italics to indicate that it was just swapped with 37.

  2. Starting from the left of the array, but beginning with the element after 12, compare each element with 37 until an element greater than 37 is found. Then swap 37 and that element. The first element greater than 37 is 89, so 37 and 89 are swapped. The values now reside in the array as follows:

    12 2 6 4 37 8 10 89 68 45
    
  3. Starting from the right, but beginning with the element before 89, compare each element with 37 until an element less than 37 is found. Then swap 37 and that element. The first element less than 37 is 10, so 37 and 10 are swapped. The values now reside in the array as follows:

    12 2 6 4 10 8 37 89 68 45
    
  4. Starting from the left, but beginning with the element after 10, compare each element with 37 until an element greater than 37 is found. Then swap 37 and that element. There are no more elements greater than 37, so when we compare 37 with itself, we know that 37 has been placed in its final location of the sorted array.

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.]

  1. Extend the Simpletron Simulator's memory to contain 1000 memory locations to enable the Simpletron to handle larger programs.
  2. Allow the simulator to perform modulus calculations. This requires an additional Simpletron Machine Language instruction.
  3. Allow the simulator to perform exponentiation calculations. This requires an additional Simpletron Machine Language instruction.
  4. Modify the simulator to use hexadecimal values rather than integer values to represent Simpletron Machine Language instructions.
  5. Modify the simulator to allow output of a newline. This requires an additional Simpletron Machine Language instruction.
  6. Modify the simulator to process floating-point values in addition to integer values.
  7. Modify the simulator to handle string input. [Hint: Each Simpletron word can be divided into two groups, each holding a two-digit integer. Each two-digit integer represents the ASCII decimal equivalent of a character. Add a machine-language instruction that will input a string and store the string beginning at a specific Simpletron memory location. The first half of the word at that location will be a count of the number of characters in the string (i.e., the length of the string). Each succeeding half-word contains one ASCII character expressed as two decimal digits. The machine-language instruction converts each character into its ASCII equivalent and assigns it to a halfword.]
  8. Modify the simulator to handle output of strings stored in the format of part (g). [Hint: Add a machine-language instruction that will print a string beginning at a certain Simpletron memory location. The first half of the word at that location is a count of the number of characters in the string (i.e., the length of the string). Each succeeding halfword contains one ASCII character expressed as two decimal digits. The machine-language instruction checks the length and prints the string by translating each two-digit number into its equivalent character.]
  9. Modify the simulator to include instruction SML_DEBUG that prints a memory dump after each instruction executes. Give SML_DEBUG an operation code of 44. The word +4401 turns on debug mode, and +4400 turns off debug mode.
 
8.29

What does this program do?

 1 // Ex. 8.29: ex08_29.cpp
 2 // What does this program do?
 3 #include 
 4 using std::cout;
 5 using std::cin;
 6 using std::endl;
 7
 8 bool mystery3( const char *, const char * ); // prototype
 9
10 int main()
11 {
12 char string1[ 80 ], string2[ 80 ];
13
14 cout << "Enter two strings: ";
15 cin >> string1 >> string2;
16 cout << "The result is " << mystery3( string1, string2 ) << endl;
17 return 0; // indicates successful termination
18 } // end main
19
20 // What does this function do?
21 bool mystery3( const char *s1, const char *s2 )
22 {
23 for ( ; *s1 != '' && *s2 != ''; s1++, s2++ )
24
25 if ( *s1 != *s2 )
26 return false;
27
28 return true;
29 } // end function mystery3
 


Introduction to Computers, the Internet and World Wide Web

Introduction to C++ Programming

Introduction to Classes and Objects

Control Statements: Part 1

Control Statements: Part 2

Functions and an Introduction to Recursion

Arrays and Vectors

Pointers and Pointer-Based Strings

Classes: A Deeper Look, Part 1

Classes: A Deeper Look, Part 2

Operator Overloading; String and Array Objects

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism

Templates

Stream Input/Output

Exception Handling

File Processing

Class string and String Stream Processing

Web Programming

Searching and Sorting

Data Structures

Bits, Characters, C-Strings and structs

Standard Template Library (STL)

Other Topics

Appendix A. Operator Precedence and Associativity Chart

Appendix B. ASCII Character Set

Appendix C. Fundamental Types

Appendix D. Number Systems

Appendix E. C Legacy Code Topics

Appendix F. Preprocessor

Appendix G. ATM Case Study Code

Appendix H. UML 2: Additional Diagram Types

Appendix I. C++ Internet and Web Resources

Appendix J. Introduction to XHTML

Appendix K. XHTML Special Characters

Appendix L. Using the Visual Studio .NET Debugger

Appendix M. Using the GNU C++ Debugger

Bibliography



C++ How to Program
C++ How to Program (5th Edition)
ISBN: 0131857576
EAN: 2147483647
Year: 2004
Pages: 627

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net