Module 9. Subscripted Variables and Pointers Exercises

1.  Given the definition

   int box[10];

what is wrong with the following assignment statement?  If there is
nothing wrong, say so.

   box[10] = 20;

2.  Is the following statement correct?  If correct, answer
``Correct''.  If not correct, answer ``Wrong'' and give a reason:

In any one array there can be values of different types.

3.  Is the following statement correct?  If correct, answer
``Correct''.  If not correct, answer ``Wrong'' and give a reason:

Passing large arrays into functions is a bad idea because it takes a
long time to copy an array from a calling function into a called
function.

4.  Assume the following definition and initialization:

   int link[5] = { 2, 3, 4, 0, 1} ;

What output does each of the following statements produce:

(a) cout << link[0];

(b) cout << link[link[2]];

(c) Given any integer k (0<=k<=4), what is the value of the following
expression

      link[link[link[link[link[k]]]]]

5.  A Fibonacci sequence of numbers is defined as follows: The first two
numbers in the sequence are 0 and 1.  Then, each additional Fibonacci
number is the sum of the two previous numbers in the sequence.  Thus,
the first ten Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, and
34.

Complete the following function so that the function generates the
first fib_num ( fib_num > 2 ) numbers and puts them in the array F.
Do NOT write a recursive function.  Declare any local variable(s) you
need.

   void Fib_general (int F[], int fib_num)
   {
    // Declare any local variable(s)


    // Initialize the first two numbers in the array F



    // Use a loop to generate the rest





   }

6.  Complete the following recursive binary search function which
searches a sorted (in ascending order) array A for a given value
Element.  The parameter First is the first index of the array being
searched, while the parameter Last is the last index of the array
being searched.

   int BinSearch (int A[], int Element, int First, int Last)
   {
    int Mid;

    if(_______________)
    {
      return(-1);       // not found;
    }
    else 
    { 
      Mid = (First + Last) / 2;
      if(________________)
          return(Mid);
      else 
      {
          if (Element < A[Mid])
             return(_______________);
          else 
             return(_______________);
      }
    }
   }


7.  Write 3 lines of C++ code below to (1) declare the variable zolish
to be a pointer to an integer, (2) make zolish point to the location
where xerxes is, and (3) store the number 45 in the location where
zolish is pointing.

  int xerxes;
  xerxes = 37;

8.  Now what will
  cout << xerxes << endl;
print?

9.  Write 3 lines of C++ code below to (1) declare the variable salary
to be a pointer to a float type value, (2) dynamically allocate a
float location and make salary point to it, and (3) store the value
150.75 in the location where salary is pointing.

10.  Write a line of C++ code to de-allocate the float location that
you dynamically allocated in part 3.

11.  Assume the following declarations:
   int pixel;
   int pad;
   int *plane;
   int *dots;

What is stored in pixel, pad, plane, and dots after the following
statements?
   pixel = 24;
   pad = 57;
   plane = &pixel;
   dots = &pad;