1
h08
CS16 F17
Name:
(as it would appear on official course roster)
Umail address: @umail.ucsb.edu
Optional: name you wish to be called
if different from name above.
Optional: name of "homework buddy"
(leaving this blank signifies "I worked alone")

h08: Vectors and Pointers

ready? assigned due points
true Thu 11/16 02:00PM Tue 11/28 02:00PM

You may collaborate on this homework with AT MOST one person, an optional "homework buddy".

MAY ONLY BE TURNED IN IN THE LECTURE LISTED ABOVE AS THE DUE DATE. There is NO MAKEUP for missed assignments.


PLEASE MARK YOUR HOMEWORK CLEARLY, REGARDLESS OF IF YOU WRITE IT OUT IN INK OR PENCIL!

1.(20 pts) Given the following header:

vector <string> split(string target, string delimiter);

implement/write the function split so that it returns a vector of the strinfs in target that are separated by the string delimiter. For example:

split("10,20,thirty", ",")

should return a vector with the strings “10”, “20”, and “thirty”.

2.(20 pts) Write a function called swapFrontBack that takes as input argument a vector of integers. The function should swap the first element in the vector with the last element in the vector. The function should check if the vector is empty to prevent errors.

3.(5 pts) What is the freestore (also known as the heap) used for?

4.(5 pts) What operator returns unused memory to the heap?

5.(5 pts) What C++ unary operator is the “de-referencing” operator?

6.(5 pts) What C++ unary operator is the “address-of” operator?

7.(20 pts) Consider the following code:

int a=3, b=5, *p=&a, *p2;

We can draw a diagram that shows the effect of this code in memory, like this.

pointerdiagram

Note that we are showing the contents of a and b, and we are showing where p1 and p2 point. Since p2 is uninitialized, we show it pointing to a question mark (?). If a regular int variable were uninitialized, we would show a question mark (?) inside the box for the variable. Your job is to draw similar diagrams for the code below. Draw the diagrams as many times as needed for each question (that is, show all the intermediate steps). Please do not be messy and make your diagram very clear and readable.

int a=2, b, *p1=&b, *p2=&a, *p3;
p3 = p2;
*p1 = 8;
p2 = p1;
p1 = p3;
*p2 = 4;



int a=2, b=3, *p1, *p2;
p2 = &a;
p1 = &b;
*p1 = *p1 + *p2;





8.(10 pts) What will be the output of this program?

#include <iostream>
using namespace std;
int main() {
    int *pc, c;
    
    c = 5;
    cout << "Address of c (&c): " << &c << endl;
    cout << "Value of c (c): " << c << endl << endl;

    pc = &c;    
    cout << "Address that pointer pc holds (pc): "<< pc << endl;
    cout << "Content of the address pointer pc holds (*pc): " << *pc << endl << endl;
    
    c = 11;    
    cout << "Address pointer pc holds (pc): " << pc << endl;
    cout << "Content of the address pointer pc holds (*pc): " << *pc << endl << endl;

    *pc = 2; 
    cout << "Address of c (&c): " << &c << endl;
    cout << "Value of c (c): " << c << endl << endl;

    return 0;
}

9.(4 pts) Write a program that introduces int variables x and y and int* pointer variables p and q. Set x to 2, y to 8, p to the address of x, and q to the address of y. Then print the following information:

(a) The address of x and the value of x. (1 pt)

(b) The value of p and the value of *p. (1 pt)

(c) The address of y and the value of y. (1 pt)

(d) The value of q and the value of *q. (1 pt)

(e) The address of p (not its contents!). (1 pt)

(f) The address of q (not its contents!). (1 pt)

Print the program and the outputs of the program on a separate sheet of paper and attach that to this homework. Please do not use more than 1 page (two sided is ok, but one sided is preferred).