ICS 45C Spring 2022
Exercise Set 2

Due date and time: Wednesday, April 20, 11:59pm


Getting started

First of all, be sure that you've read the Reinforcement Exercises page, which explains everything you'll need to know, generally, about how the reinforcement exercises will work this quarter. Make sure you read that page before you continue with this one.

Before you begin work on these reinforcement exercises, there's a chore that you'll need to complete on your ICS 45C VM to get it set up to proceed.

Refreshing your ICS 45C VM environment

Even if you previously downloaded your ICS 45C VM, you will probably need to refresh its environment before proceeding with these exercises. Log into your VM and issue the command ics45c version to see what version of the ICS 45C environment you currently have stored on your VM. Note, in particular, the timestamp; if you see a version with a timestamp older than the one listed below, you'll need to refresh your environment by running the command ics45c refresh to download the latest one before you proceed with these exercises.

2022-04-14 11:33:28
Exercise Set 2 template added

If you're unable to get outgoing network access to work on the ICS 45C VM — something that afflicts a handful of students each quarter — then the ics45c refresh command won't work, but an alternative approach is to download the latest environment from the link below, then to upload the file on to your ICS 45C VM using SCP. (See the Project #0 write-up for more details on using SCP.) Once the file is on your VM, you can run the command ics45c refresh_local NAME_OF_ENVIRONMENT_FILE, replacing NAME_OF_ENVIRONMENT_FILE with the name of the file you uploaded; note that you'd need to be in the same directory where the file is when you run the command.

Creating your project directory on your ICS 45C VM

A project template has been created specifically for this set of exercises, containing a similar structure to the basic template you saw in Project #0. Among other things, it contains a version of the gather script that's different from the ones in other exercise sets, so you'll absolutely need to use the set2 template for these exercises, as opposed to the set1 template (or the basic template).

Decide on a name for your project directory, then issue the command ics45c start YOUR_CHOSEN_PROJECT_NAME set2 to create your new project directory using the set2 template. (For example, if you wanted to call your project directory set2, you would issue the command ics45c start set2 set2.)

A word about organizing your answers

Please do not copy and paste the problem (or portions of the problem) into the answer you submit. We are well aware of what question you're asking, so copying and pasting the problem simply slows us down when grading your work. Consequently, we will be deducting points when problems are copied and pasted into solutions.


Problem 1 (2 points)

We've discussed in C++ that functions can be declared with default arguments, which is to say that their signature can specify default values for arguments that are left out of a call to that function, in which case the defaults are used automatically. We've seen, too, that there's a rule in C++ that forbids parameters with defaults from being listed before parameters that don't have them, which would make the following function declaration (and lots of others like it) illegal in C++:

void doSomething(double x = 3.5, std::string y, double z, std::string w = "Boo");

At first blush, this might seem like an unnecessary restriction; as is so often the case when you're learning a programming language, it's much better to know why this rule is in place, rather than to just memorize the rule. And one way to understand why languages restrict certain behaviors is to consider what would happen if the restrictions weren't there. So let's imagine that the declaration of the doSomething function above is actually legal.

Write one call to the doSomething function above that would be problematic if the declaration of doSomething was legal. Briefly explain (in no more than a sentence) what would be problematic about it.

What to submit

Add one PDF file to your problems directory with this name: problem1.pdf, which contains your example and the issue with it.


Problem 2 (2 points)

This week, we've talked about two seemingly-similar ideas: references and pointers. While they do indeed share certain similarities — conceptually, they are both ways of retaining information about the location of some other object — their differences make them useful for very different things. It's not uncommon when learning C++ to have trouble determining what those differences are, so let's work on our understanding by considering one scenario where we might use one or the other, so we can understand which one we need (and, more importantly, why).

In prerequisite coursework — such as ICS 33, where it's covered in our curriculum — you've no doubt seen a data structure called a linked list, which arranges each piece of information into a separate object called a node, then uses some mechanism to allow each node to tell you the location of the next node in the list. Given that arrangement, if have a way of finding the first node, you can be assured that you'll be able to find all of the others. What mechanism you use to "link" the nodes together is a little different in different programming languages, but the idea is a pretty universal one, which I've implemented in many programming languages over the years.

Suppose that you were implementing a linked list in C++. If the idea of "linking" nodes together is a matter of one node knowing the location of the next one, then there are two mechanisms that might potentially be used to "link" the nodes together: references and pointers.

  1. Suppose that you implemented a linked list using references to link the nodes together (i.e., you have a head reference that indicates where the first node is, then a next reference in each node specifying where the next node is). In what ways, if any, would this work properly? In what ways, if any, would this not be able to work?
  2. Suppose that you implemented a linked list using pointers to link the nodes together instead. In what ways, if any, would this work properly? In what ways, if any, would this not be able to work?

What to submit

Add one PDF file to your problems directory with this name: problem2.pdf, which contains your answers to these two questions.


Problem 3 (2 points)

We've seen that there are two forms of memory allocation that can be done in C++: static and dynamic. One difference is where objects are stored: statically-allocated objects are predominantly stored on the run-time stack, while dynamically-allocated objects are instead stored in the heap. But if you haven't wrapped your mind around what these techniques are for, that sounds like a distinction without a difference, so let's make sure we've embraced the reasons why we need each.

  1. When you've statically allocated an object, what benefits do you get that you don't get when you dynamically allocate them?
  2. When you've dynamically allocated an object, what benefits do you get that you don't get when you statically allocate them?
  3. Given a choice — in a circumstance where either one would work just as well — which form of allocation would you prefer? Why?

What to submit

Add one PDF file to your problems directory with this name: problem3.pdf, which contains your answers to these three questions.


Problem 4 (2 points)

One of the things that can be confusing about references and pointers is the overloading of the same symbols; we see symbols like * and & appear in different places in the syntax of C++, with a related but different meaning depending on where they appear. For better or for worse, this is a problem we need to get ourselves past, as it's a minor but confusing syntactic issue that stands in the way of the deeper understanding we need. So let's work through this a bit.

The lens through which much of C++ can be understood is its type system, which are the rules that govern the usage and meaning of types in C++. Notably, we've seen that expressions in C++ not only do a job, but they also return a result; that result is an object and that object has a type. We've seen, too, that the legality of statements is often determined by the types of these expressions, which means that compilers need to be able to determine, for any expression, what its type is. If compilers can do it, given only a program's code, then we can do it, too, provided that we understand the rules we've learned so far.

Let's assume that we start with the following declarations:

int i = 3;
double d = 3.5;
std::string s = "Boo is perfect";
int& j = i;
int* k = new int{5};

What are the types of each of the following C++ expressions? For any of them that are illegal, write illegal; otherwise, write the expression's type in C++.

  1. i + 2
  2. i * d
  3. i + j
  4. *i
  5. &i
  6. &d
  7. &j
  8. s[*k]
  9. s[&i]
  10. *s
  11. *k
  12. &k
  13. k * 2
  14. *k * 2

What to submit

Add one PDF file to your problems directory with this name: problem4.pdf, which contains a list of the types of each of these expressions. Please write your answers in order and put the number in front of each of them; one of your goals is to make it as easy as possible for us to see whether you're correct. For example, if you thought the answer to every one of these was illegal, you would submit something that looks like this:

  1. illegal
  2. illegal
  3. illegal
  4. illegal
  5. illegal
  6. illegal
  7. illegal
  8. illegal
  9. illegal
  10. illegal
  11. illegal
  12. illegal
  13. illegal
  14. illegal

Problem 5 (2 points)

When we pass parameters to functions by value or by reference, we're doing different things; the end result will not necessarily be the same. And, of course, it's wise for us to understand exactly how the results will be different. But another angle we need to consider is why we need to have these two different kinds of parameters, which is to say that we should understand when we would be better off using one technique instead of the other.

  1. What are the design benefits of passing a parameter by value? What can you do when you pass by value that you can't do when you pass by reference?
  2. What are the design benefits of passing a parameter by reference? What can you do when you pass by reference that you can't do when you pass by value?
  3. Suppose you were going to write a function with one parameter, and that the result would be the same whether you passed the parameter by value or by reference — it's not always the case that the outcome will be different. In what circumstances would you expect the pass-by-value version to run faster? In what circumstances would you expect the pass-by-value version to use less memory?
  4. Same situation as the previous question. In what circumstances would you expect the pass-by-reference version to run faster? In what circumstances would you expect the pass-by-reference version to use less memory?

Note that these questions are not about how these techniques work; they're about why you would prefer one rather than the other.

What to submit

Add one PDF file to your problems directory with this name: problem5.pdf, which contains your answers to these four questions.


Deliverables

In Canvas, you'll find a separate submission area for each problem. Submit your solution to each problem into the appropriate submission area. Be sure that you're submitting the correct file into the correct area (i.e., submitting your Problem 1 solution to the area for Problem 1, and so on). Under no circumstances will we offer credit for files submited in the incorrect area.

Submit each file as-is, without putting it into a .tar.gz file or arranging it in any other way. (There is a gather script in the set2 template, but there's no need to use it.) If we asked for a PDF, for example, all we want is a PDF; no more, no less. If you submit something other than what we asked for, we will not be offering you any credit on the submission. There are no exceptions to this rule.

Of course, you should also be aware that you're responsible for submitting precisely the version of your work that you want graded. We won't regrade an exercise simply because you submitted the wrong version accidentally, and we won't be able to offer any credit on exercises that you forgot to submit.

Can I submit after the deadline?

Unlike some of the projects in this course, the reinforcement exercises cannot be submitted after the deadline; there is no late policy for these. Each is worth only 3% of your grade (and the lowest score doesn't count), so it's not a disaster if you miss one of them along the way.

What do I do if Canvas slightly adjusts my filename?

Canvas will sometimes modify your filenames when you submit them (e.g., when you submit the same file twice, it will change the name of your second submission to end in -1.tar.gz instead of just .tar.gz). In general, this is fine; as long as the file you submitted has the correct name, we'll be able to obtain it with that same name, even if Canvas adjusts it.

Can I work on this outside of the VM?

Yes, but be aware that you must submit the files in the appropriate format, and that any C++ code you submit must compile and run on the ICS 45C VM.