Exercises

  1. 8.7 (True or False) State whether the following are true or false. If false, explain why.

    1. Two pointers that point to different built-in arrays cannot be compared meaningfully.

    2. Because the name of a built-in array is implicitly convertible to a pointer to the first element of the built-in array, built-in array names can be manipulated in the same manner as pointers.

  2. 8.8 (Write C++ Statements) For each of the following, write C++ statements that perform the specified task. Assume that unsigned integers are stored in four bytes and that the starting address of the built-in array is at location 1002500 in memory.

    1. Declare an unsigned int built-in array values with five elements initialized to the even integers from 2 to 10. Assume that the constant size has been defined as 5.

    2. Declare a pointer vPtr that points to an object of type unsigned int.

    3. Use a for statement to display the elements of built-in array values using array subscript notation.

    4. Write two separate statements that assign the starting address of built-in array values to pointer variable vPtr.

    5. Use a for statement to display the elements of built-in array values using pointer/offset notation.

    6. Use a for statement to display the elements of built-in array values using pointer/offset notation with the built-in array’s name as the pointer.

    7. Use a for statement to display the elements of built-in array values by subscripting the pointer to the built-in array.

    8. Refer to the fifth element of values using array subscript notation, pointer/offset notation with the built-in array name’s as the pointer, pointer subscript notation and pointer/offset notation.

    9. What address is referenced by vPtr + 3? What value is stored at that location?

    10. Assuming that vPtr points to values[ 4 ], what address is referenced by vPtr -= 4? What value is stored at that location?

  3. 8.9 (Write C++ Statements) For each of the following, write a single statement that performs the specified task. Assume that long variables value1 and value2 have been declared and value1 has been initialized to 200000.

    1. Declare the variable longPtr to be a pointer to an object of type long.

    2. Assign the address of variable value1 to pointer variable longPtr.

    3. Display the value of the object pointed to by longPtr.

    4. Assign the value of the object pointed to by longPtr to variable value2.

    5. Display the value of value2.

    6. Display the address of value1.

    7. Display the address stored in longPtr. Is the address displayed the same as value1’s?

  4. 8.10 (Function Headers and Prototypes) Perform the task in each of the following:

    1. Write the function header for function zero that takes a long integer built-in array parameter bigIntegers and a second parameter representing the array’s size and does not return a value.

    2. Write the function prototype for the function in part (a).

    3. Write the function header for function add1AndSum that takes an integer built-in array parameter oneTooSmall and a second parameter representing the array’s size and returns an integer.

    4. Write the function prototype for the function described in part (c).

  5. 8.11 (Find the Code Errors) 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;
      
  6. 8.12 (Simulation: The Tortoise and the Hare) In this exercise, you’ll re-create the classic race of the tortoise and the hare. You’ll use random-number generation to develop a simulation of this memorable event.

    Our contenders begin the race at “square 1” of 70 squares. Each square represents a possible position along the race course. The finish line is at square 70. The first contender to reach or pass square 70 is rewarded with a pail of fresh carrots and lettuce. The course weaves its way up the side of a slippery mountain, so occasionally the contenders lose ground.

    There is a clock that ticks once per second. With each tick of the clock, your program should use function moveTortoise and moveHare to adjust the position of the animals according to the rules in Fig. 8.18. These functions should use pointer-based pass-by-reference to modify the position of the tortoise and the hare.

    Fig. 8.18 Rules for moving the tortoise and the hare.

    Animal Move type Percentage of the time Actual move
    Tortoise Fast plod 50% 3 squares to the right
      Slip 20% 6 squares to the left
      Slow plod 30% 1 square to the right
    Hare Sleep 20% No move at all
      Big hop 20% 9 squares to the right
      Big slip 10% 12 squares to the left
      Small hop 30% 1 square to the right
      Small slip 20% 2 squares to the left

    Use variables to keep track of the positions of the animals (i.e., position numbers are 1–70). Start each animal at position 1 (i.e., the “starting gate”). If an animal slips left before square 1, move the animal back to square 1.

    Generate the percentages in Fig. 8.18 by producing a random integer i in the range 1i10. For the tortoise, perform a “fast plod” when 1i5, a “slip” when 6i7 or a “slow plod” when 8i10. Use a similar technique to move the hare.

    Begin the race by displaying

    
    BANG !!!!!
    AND THEY'RE OFF !!!!!
    

    For each tick of the clock (i.e., each iteration of a loop), display a 70-position line showing the letter T in the tortoise’s position and the letter H in the hare’s position. Occasionally, the contenders land on the same square. In this case, the tortoise bites the hare and your program should display OUCH!!! beginning at that position. All positions other than the T, the H or the OUCH!!! (in case of a tie) should be blank.

    After displaying each line, test whether either animal has reached or passed square 70. If so, display the winner and terminate the simulation. If the tortoise wins, display TORTOISE WINS!!! YAY!!! If the hare wins, display Hare wins. Yuch. If both animals win on the same clock tick, you may want to favor the tortoise (the “underdog”), or you may want to display It's a tie. If neither animal wins, perform the loop again to simulate the next tick of the clock.

  7. 8.13 (What Does This Code Do?) What does this program do?

    Fig. 8.19 What does this program do?

    Alternate View
    
     1   // Ex. 8.13: ex08_13.cpp
     2   // What does this program do?
     3   #include <iostream>
     4   using namespace std;
     5
     6   void mystery1(char*, const char*); // prototype
     7
     8   int main() {
     9      char string1[80];
    10      char string2[80];
    11
    12      cout << "Enter two strings: ";
    13      cin >> string1 >> string2;
    14      mystery1(string1, string2);
    15      cout << string1 << endl;
    16    }
    17
    18    // What does this function do?
    19    void mystery1(char* s1, const char* s2) {
    20       while (*s1 != '') {
    21          ++s1;
    22       }
    23
    24       for (; (*s1 = *s2); ++s1, ++s2) {
    25          ; // empty statement
    26       }
    27     }
    
  8. 8.14 (What Does This Code Do?) What does this program do?

    Fig. 8.20 What does this program do?

    Alternate View
    
     1   // Ex. 8.14: ex08_14.cpp
     2   // What does this program do?
     3   #include <iostream>
     4   using namespace std;
     5
     6   int mystery2(const char*); // prototype
     7
     8   int main() {
     9      char string1[80];
    10
    11      cout << "Enter a string: ";
    12      cin >> string1;
    13      cout << mystery2(string1) << endl;
    14   }
    15
    16   // What does this function do?
    17   int mystery2(const char* s) {
    18      unsigned int x;
    19
    20      for (x = 0; *s != ''; ++s) {
    21         ++x;
    22      }
    23
    24      return x;
    25    }
    
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset