Winter 2005 UCLA CS 31 Section 1 (Shinnerl)

Written HW 2 FAQ File

The most recently added question is shown last.
Questions are in boldface. Answers are in regular text.

Last Update: 12pm Wed. 3/9.

  1. Can you give me a hint on number 1 (adding arrays and returning int*)?

    1. Please read the online handout on the kinds of memory used by a process.
    2. If your function acquires a (storage) resource and lets some other function use it, don't expect that other function to put it back (release the resource) when it's done with it.
    3. The best solution is not to work on arrays at all, but rather to use a class containing an array, and rely on the class constructor and destructor to acquire and release the storage automatically. But this still requires that the caller and the sumArrays() function work on different arrays, and not share storage. For example, use the vector library introduced in the book on page 290.

  2. Are we supposed to use a fixed-length array in Question 7, or should we use a dynamically sized array?

    It doesn't matter; you can easily handle both kinds with the same code, as shown below.

    void reverse (int *a, int length){
        // ...
    }
    
    Such a definition will work with any array, no matter how its storage was allocated. The compiler happily converts from int [] or int *const to int*, because the pointer argument a is received by value.

    (Recall: int *const means "constant pointer to int." Since the pointer is received by value, the reverse function has its own local copy of it. Changing that local copy (not done here anyway) would have no effect on the caller's copy of that pointer. Therefore, there is no reason for the compiler to force this function to keep its local pointer fixed. Therefore, the compiler lets the function receive the pointer as an int*, which works for all arrays, no matter how they're allocated.)

  3. For Question 9, do we need to create any visuals that actually show the points being manipulated? (I.e. outputting a coordinate plane with points on it that can change their positions.)

    No, you don't.

  4. I need a hint on how to print the bins in a histogram vertically on the screen.

    Store the data for each bin in an object of some special type that you create yourself as a struct or class. Or, build the histogram in a 2-dimensional array. Once you have the bins stored separately in memory, determine which bin is the longest. This determines how many rows of output you'll generate.

    With stream output ( cout << ), you must print rows one at a time, from top to bottom. However, assuming your bins grow from the bottom of your desired output toward the top, this means that the last element in each bin must be printed first. For each row of output, visit the bins one by one. If a given bin has enough data in it to warrant printing in the current row, then do so; otherwise, print space. Only the bottom row is guaranteed to have data in every column.



Written HW 2 home page