Name: ____________________________________________________ Alpha: _____________________

Describe help received: _________________________________________________________________

  1. [10pts] One nice feature of static arrays is that they can be initialized with value at the point of their definition in a single statment, unlike with dynamic arrays where you must first allocate, then give values one at a time. For example:
    double A[4] = { 1.2, 3.71, -0.5, -3.141 };
    So the array element values go inside { }'s in a comma-separated list. The first such value is the index 0 value, then index 1, and so on.

    Suppose we use the previous Lab's representation of a card as cardvale = 20*suitvalue + facevalue, with 2-14 as the face values, and 0-3 as the suit values. Complete the function definition below, making use of the static array definitions for S and F. Note: the function should return a single string that is the "pretty print" version of the card represented by the single integer card value cv.

    string cardValueToString(int cv)
      // note: clubs="\u2663", diamonds="\u2666", hearts="\u2665", spades="\u2660"
      string S[4] = {"\u2663","\u2666","\u2665","\u2660"}; 
      string F[15] = {"",""," 2"," 3"," 4"," 5"," 6"," 7"," 8"," 9","10"," J"," Q"," K"," A"};
  2. [10pts] Suppose we have a nice struct point for representing points. If I'm defining a struct triangle for representing triangles, I might choose to define the data member representing the verticies like this:
    point verts[3];
    On the other hand, if I'm defining a struct polygon for representing arbitrary polygons (triangles, squares, trapezoids, hexagons, etc.) I'd define it like this instead:
    point *verts;
    Why would I do it differently for polygons?

  3. [10pts] Consider the following program:
    #include <iostream>
    using namespace std;
    int* foo()
      int B[3] = {1,-2,7};
      return B;
    int main()
      int* A = foo();
      for(int i = 0; i < 3; i++)
        cout << A[i] << endl;
      return 0;
    Compiling and running this program yields this:
    ~/$ g++ hwex.cpp
    hwex.cpp: In function 'int* foo()':
    hwex.cpp:6:7: warning: address of local variable 'B' returned [enabled by default]
    ~/$ ./a.out
    Explain what the problem is here? That doesn't mean parroting the warning back, but explaining what it means and why it's a problem and, if you can, why we get the crazy output shown in the sample run!
  4. [70pts] Consider the program splitme.cpp (example run shown below). Your job is to split it (appropriately!) into separate .h and .cpp files and demonstrate that it still works. NOTE: this requires no new programming, just splitting into files and adding the right #defines.
    ~/$ g++ splitme.cpp -o bigdrop
    ~/$ ./bigdrop
    Laura 6 112.3@40 119.8@32 118.2@30 130.0@20 128.6@22 151.2@1
    Biggest per day weight drop is 1.3