Midterm 2 Review

Functions

I might ask you some written questions about the purpose of functions.

I might ask you to write declarations only for some functions; pay attention to what the question is asking! It might be easier than you think.

E.g., write a declaration for a function named all_larger_than which returns a bool and takes as parameters a vector<int> and an int. Note that I don’t tell you what the names of the parameters; they don’t matter.

This function is an accumulating loop, so we could write it (taking the name as a hint to what it should do) as

bool all_larger_than(vector<int> vs, int n) {
  bool alt = true;
  for(int e : vs)
    alt = alt && e > n;
  return alt;
}

but we could shorten this to

bool all_larger_than(vector<int> vs, int n) {
  for(int e : vs)
    if(!(e > n))
      return false;

  return true;
}

Note that this is no longer a “pure” accumulating loop, so we can’t combine it with other accumulating loops.

I might ask you to write functions to do various kinds of “accumulating” loops: sum, product, all even, etc.

I might give you a main with some functions and ask you what output they will produce. E.g.,

int main() {
  int x = 1;
  f(x);
  x = 2;
  g(x);
  cout << x << endl;
  return 0;
}

void f(int y) {
  int x = 5;
  cout << y + x << endl;
}

void g(int z) {
  int x = 10;
  cout << x << endl;
}

What will this print?

Vectors and Arrays

Draw the table corresponding to the vector

vector<string> colors = { "Red", "Orange", "Yellow", 
                          "Green", "Blue", "Indigo",
                          "Violet" };

Construct a vector (of float) corresponding to the following table

Index Sample
0 1.2
1 5.3
2 2.7
3 9.8
4 -3.2

What command do we use to add new rows to the end of the vector?

What command do we use to remove a single row from the end of the vector?

What command can remove or add rows from/to the end of the vector?

Suppose I have a vector of strings vector<string> names and I want to find the length of the third name; what expression will give me this information?

Write the statement to construct a vector of ints with 8 elements.

Write the statement to construct an array of ints with 8 elements.

Write a statement to assign a value of 2 to the 0-th element of a vector<int> v.

Write a statement to assign a value of 2 to the 0-th element of an int array v.

References and Pointers

I might ask you to walk through some code involving reference, or pointers (but not both at the same time) and determine what boxes will be created, and what their values will be at the end of the execution. I might ask you to dynamically create an array of some type (via new), access its elements, and then free it when you are done with it. I might ask you to identify some mistakes that have to do with memory management.

In the following code, mark both the scope (beginning and ending) and lifetime (beginning and ending) of all variables/values:

int main() {
  int x = 1;
  int* y = new int();
  cout << x << *y << endl;

  f(x,y);

  int z = 2;
  cout << z << endl;

  return 0;
}

void f(int a, int *b) {
  cout << a << endl;
  delete b;
}

Here, the lifetime of the int in y is from the point of its creation in main to the point of its deletion in f.

What’s wrong with the following code?

int main() {
  float* f = new float();
  f();

  return 0;
}

void f() {
  int* x = new int[10]();
  cout << "hello!" << endl;

  // clean up after ourselves!
  delete x; 
}

Here’s the worst kind of pointer shuffling code I might give you:

int x = 1, y = 2, z = 3; 
int* a = &x;
int* b = &y; 
int* c = &z; 
int** q = &a;

a = b;
b = c;
c = a;
**q *= 2;

q = &b;
*q = &x;

*b *= 2;

I might ask you to write a loop over an array using pointers. Something like

int arr[10];

for(int*p = &arr[0]; p != &arr[10]; p++)

Note that this loop can be equivalently written as

for(int* p = arr; p != arr + 10; p++)

because an array can be implicitly converted into a pointer to its first element.

I might ask for some functions that take arguments by reference and do things with them. I might ask for a function that modifies its arguments, and you will have to infer that references are required. E.g., write a function product whose return type is void and which returns the product of a vector<int> through an out-parameter prod.

void product(vector<int> data, int& prod) {
    ...
}

I might ask what the differences are between pointers and references, in terms of what they can and can’t do.

Dynamic Memory

new and delete. Lifetime vs. scope. Using pointers as arrays.