Chapter 4:  (pages 211 to 213)

7. What is the main difference between a vector object and an array object?
    between a vector and a list object?

14. Give the value of v.size() after executing each of the following statements:

vector<int> v(25);    //___________
v.push_back(-4);      //___________
v.resize(40);         //___________
m = v.back();         //___________
v.pop_back();         //___________
15. Given the declarations
int arr[] = {4, 6, 2, 9, 3, 8, 7, 1};
int arrSize = sizeof(arr)/sizeof(int);
vector<int> v(arr, arr+arrSize);
  what is the list of elements in v after the execution of the statement v.resize(5)?

16. Trace the following code, and give the output:

int arr[] = {4, -6, 22, 7, 13, 8};
int arrSize = sizeof(arr)/sizeof(int);
vector<int> v(arr, arr+arrSize);

while (!v.empty())
{
    cout<< v.back() <<" ";
    v.pop_back();
}

Chapter 5 (pages 268 to 273)
Review Exercise:
3. What is the result of the following declaration? (Multiple Choices)
int * a = new int(35);
    (a) a is an array of 35 integers.
    (b) *a is an integer with initial value 35.
    (c) a is an integer with initial value 35.
    (d) a is an array of 35 integers with initial value 35.

Written Exercises
15. Assume the declaration

int m = 35, *intPtr;
double x, *dblptr;
     (a) Assign intPtr a value so that it points to m.
     (b) Assign dblPtr a value so that it points to x, and then use the pointer to assign x the value 5.3.

17. Repeat the following declaration for each part, (a)-(d).  Fill in the value for *p and *q.

int a = 15, b = 25, *p = &a, *q = &b;
     (a)  *p = *q + 2      *p = ______
     (b)  *q += 10         *q = ______
     (c)  (*q)++           *q = ______
     (d)  q = p            *p = ______    *q = _______

19. The upcoming declaration includes integer and pointer variables. For parts (b)-(e), repeat the
declaration and use the results from part (a).

    int x = 6, y = 5, *px, *py;

    (a) Write two assignment statements that set px and py to point at integers x and y, respectively.
    (b) Write an output statement using px that prints the contents of x.
    (c) Write a statement using py that assigns 20 as the contents if y.
    (d) What is the resulting value of *py + *px?
    (e) After executing *py = *px, are &x and py equal?

20. The following statement declares both an array and a pointer:

  int arr[] = {9, 2, -3, 5, -1}, *px;

    (a) Give a statement that sets px to point at array arr.
    (b) Give a cout statement using px that outputs the value 9 from the array.
    (c) Give a cout statement using px that outputs the value -3 from the array.
    (d) Give a statement that sets px to point at element arr[1]. After executing this statement,
        (i)  what is the value of *(px + 2)?
        (ii) what is the value of *px + 2?

21. Trace the following function:

   template <typename T>
    T *f(T *arr, int n)
    {
        T *cum = new T[n];
        T *p = cum, *end = cum + n;

      *p = *arr;
        p++;
        arr++;

      while (p < end)
        {
            *p = *(p-1) + *arr;

            arr++;
            p++;
        }

      return cum;
    }

    (a)    int arr[] = {3, 5, 7, -9, 6}, *p;
           p = f(arr, sizeof(arr)/sizeof(int));

            What are the contents of the dynamic array p after f() has been called?

    (b)    string str[] = {"C", "+", "+", " Programs"}, *p;
           p = f(str, sizeof(str)/sizeof(string));

            What are the contents of p after f() has been called?

22. What does p point to after the execution of each of the following declarations?
    (a)    int *p = new int(20);
    (b)    int *p = new int[25];

    (c)    Give the delete statements that would deallocate the dynamic memory for parts (a) and (b).

Chapter 6:
Pages 319 to 321:
# 13, 14, 16, 17, 18, 19, 20, 21, and 23.

13. Consider the C++ array

int arr[] = {-15, 5, 35, -19, -12, 17, -4};
int arrSize = sizeof(arr) / sizeof(int);

(a) Declare the list object intList that holds the integers from the array arr.
(b) Declare the iterator iter for an integer list.
(c) Initialize the iterator iter to the beginning of the list intList.
(d) Assume iter is set to the start of the list.

iter++;
iter++;                // what value does iter point at?
cout << *iter;         // what is the output?
iter = intList.end();
iter--;
iter--;                // what value does iter point at?
cout << *iter;         // what is the output?
iter = intList.end();
iter++;                // what value does iter point at?
cout << *iter;         // what is the output?
(e) Use the iterator to write a code segment that scans the list elements and replaces
     each negative value by the corresponding positive number.
14. Use list<char> objects chList and revList and their member functions:
list<char> chList, revList;
char ch;

(a) Give the status of chList after each of the following instructions:

chList.push_front('t'); _____________________
chList.push_front('a'); _____________________
chList.push_back('j');  _____________________
chList.pop_front();     _____________________
(b) Write a code segment with a while loop that deletes the characters from chList
     and store them in revList, but in reverse order.  Use the member functions pop_front(),
     push_front(), and empty().
16. Assume the following declarationss:
list<string> strList;
list<string>::iterator strIter;
    Also assume that the list has the following contents:
vector  list  begin  insert
    What are the contents of the list after each statement sequence?
  (a)strIter = strList.begin();
     strList.insert(strIter, "template");

  (b) strIter = strList.end();
     strList.insert(strIter, "switch");

  (c) Assume that *strIter is "begin"
        strIter++;
        strList.erase(strIter);

17. Assume that strList is a list object that stores a sequence of strings and that strIter is an iterator for the object.
     Write a code segment that scans the elements in the list and outputs only those strings
     whose length is greater than four elements.

18.Use list<char> objects chList and newList and their member functions:

list<char> chList, newList;
     Trace the following code:
while (chList.size() != 0)
{
    newList.push_front(chList.front());
    newList.push_back(chList.front());
    chList.pop_front();
}
     (a) Assume that chList has the three characters in the string "C++".
          What is the resulting sequence of characters in newList?
     (b) What is the sequence if chList has the characters in the string "walk"?

19. Trace the following code, and display the resulting elements in the list:

int arr[] = {1, 2, 3, 4};
int arrSize = sizeof(arr) / sizeof(int);
list<int> intList(arr, arr+arrSize);
list<int>::iterator iter = intList.begin();
int i;

for(i=1; i <= arrSize; i++)
    intList.insert(iter++, i);

20. Trace function inList, which inserts new items into a list:
template <typename T>
void inList(list<T>& aList, const T& item)
{
   list<T>::iterator iter;

   iter = aList.begin();

   while (iter != aList.end())
   {
      if (item == *iter)
         return;
      iter++;
   }
   aList.push_back(item);
}

     (a) Assume that the list<int> object intL is originally empty.  What are the elements in the
          resulting list after making six calls to function inList() with data values 5 2 4 5 7 2?
     (b) Assume that the list<char>object charL is originally empty.  Make 11 calls to the function
          with characters from the string "mississippi".  What are the resulting elements in the list charL?

21. Implement the function rmOrderedDuplicates(), which removes duplicate values from an ordered list:

template <typename T>
void rmOrderedDuplicates(list<T>& aList):
      For instance, assume that the integer list intList contains the values{2 2 7 8 8 8 15 20 20}.
     After rmOrderedDuplicates() has been called, the list has values {2 7 8 15 20}.  Hint: Modify
     removeDuplicates from Section 6-3 so that the second iterator stops under one of two conditions:
     when it locates the end of the list or when it finds a data value unequal to the one referenced by the first iterator.

23. Implement, according to the instructions in parts (a) and (b), the function count(), which takes item
      as argument and returns the number of times item occurs in a list.

template <typename T>
int count(const list<T>& aList, const T& item);
    (a) Implement the function by scanning the list and maintaining a count of the number of occurrences of item.
    (b) Implement the function by making repeated calls to seqSearch() until the return iterator is aList.end().