C++ track: lab 4: Sparse Vectors


Goals

To design a sparse vector class.

Language concepts covered this week

You'll need to understand structs, but other than that, no new C++ concepts are needed.

Sparse Vectors

Our goal for next week is to design a sparse matrix class. Sparse matrices are often used in mathematics, engineering, and all those good science things to help compute the answers to hard problems. We use them in CS a lot, too. A "sparse" matrix can be loosely defined as one where there are enough zero entries to be worth taking advantage of them to reduce the space and/or time complexities involved in manipulating or storing them.

Breaking big problems into little ones is a central tenet of modern software design philosophy. We'll do that by focusing this week on creating a simpler class that represents a sparse vector. Recall that a vector is just a one-dimensional matrix. This vector class you'll define is special: it must be able to span as many as 2,147,483,647 elements (that's INT_MAX for those of you keeping score at home). Since we obviously don't want to have to store two billion elements, we had better hope that the vector is sparse. What this means is that almost all of the entries will be zeros. Because of this structure, using an array to store the elements of the sparse vector is extremely wasteful; instead, we'll use a linked list that stores only nonzero entries. This is important: none of the entries in the linked list should be zeroes.

Here's the interface specification for this class:

Warning! If your C skills are rusty (i.e. if you don't remember how to work with linked lists, or find pointers confusing) you will find this lab extremely challenging. Even if you are comfortable with pointers and linked lists, you may find this lab taking much longer than previous labs. Try to get started early and send me email if you get stuck.