Vectors

Table of Contents

This is a "pair" assignment, which means that if you are working on a team with someone else, you and your partner should do your best to engage in the pair programming model. If at all possible, my recommendation is to use Repl.it, which will allow you to code together remotely using "multiplayer" mode. At any point in time, one of you is "driving," i.e. actually using the keyboard and mouse. The other is actively engaged following along, preventing bugs, and providing ideas. You can communicate with each other via Google Hangouts, Zoom, a phone call, or any other voice approach. In a pinch, you can also use the Repl.it chat window, though having voice communication with each other would be better.

You should make sure that over the course of an assignment that you spend roughly the same amount of time each "driving." I will also ask you to turn in a form rating the work that your partner does. My recommendation is to take turns approximately every 15 minutes or so. Set a timer to help you remember.

If pair programming in real-time just doesn't work for you and your partner, you have options:

1 Get started

1.1 Use GitHub classroom to create a repository for your assignment

The first thing you'll need to do is to create a repository for your project using GitHub classroom. If you are working on a team, this repository will be shared, so only one of you should do this. To help reduce a round of communication, I'm going to designate that if you are working in a pair, the member of your team whose Carleton official email address comes first alphabetically should do this next step of setting up the GitHub repository. This is opposite from the previous pair assignment; I'm trying to give everyone an equal chance to participate. If you are working alone, then you should do this step.

If you are the designated person above, visit this GitHub classroom link (which I've placed in Moodle). Log into GitHub if your browser prompts you to after you get to GitHub. You should be taken to a screen that invites you to either "Join an existing team" or to "Create a new team". Pick the team that you created for the last team assignment. GitHub should then hopefully respond with a screen that says "You are ready to go!" and supplies you with a link for the repository that you are creating. Click on that link, and you should be taken to the repository. If you've gotten this far, you have successfully created the repository, and you the user who is logged in have access to it. Contact your partner if you have one and tell them that this step worked. (If you are working alone on the project, you can skip the next step.)

The other one of you, after the above is complete, should visit the same GitHub classroom link in the first sentence in the previous paragraph. You'll see the screen asking you to "Join an existing team" or to "Create a new team." You should be able to see the team that your partner created above; click the "Join" button. This should then take you to the "You are ready to go!" screen, and you can hopefully find a link to the repository. Click on it. You now both have access to the same GitHub repository.

1.2 Select the correct repl in Repl.it (only if you're in a pair)

If you are working individually, you can continue to work in the my-work repl. If you are working in a pair, use the paired-work repl that you created for the BST assignment. Sharing with your partner should already be set up.

1.3 Clone your repository into Repl.it

For the person on your team whose email comes second alphabetically (or just you, if you're working alone), you should now clone the repository from GitHub into Repl.it. If you're in a pair, you can in principle both be watching in multiplayer mode while you do this.

In Repl.it, make sure that you are in your home directory in the terminal window. You can always navigate there by typing

cd ~

in the terminal window. To further confirm, type pwd (which is short for "print working directory.") You should see /home/runner. If you see something different, you're not in the home directory, so try again or ask for help.

In a different browser tab (leave the one with Repl.it open), visit our our class GitHub organization page online. Once you've landed there, you should see the GitHub repository you created in the GitHub Classroom step above. It should be named c-vector-username (where username is either own username, or a combination of the two of you if you're working in a pair). Contact us for help if the repository isn't there. Click on that repository title. This should take you to the home page for that repository. There is a green box on the page that says "Clone or download." Click that box, and make sure it says "Clone with HTTPS." Highlight the https link shown, and copy it to the clipboard. The link that you're copying should look something like

https://github.com/carleton251-term/c-vector-username.git

… though your username and term will be different.

Then navigate back to the Repl.it tab with the repl that you created above, and click on the terminal window within your new repl. Your next step is to clone the repository from GitHub. To do that, type git clone, then a space, then paste in the URL from the github page that you copied from above. (To paste, you might need to right-click in the terminal window and choose paste rather than using a keyboard shortcut.)

For example, I would type the following, though your username and term will be different:

git clone https://github.com/carleton251-term/c-vector-username.git

You'll be prompted to enter your GitHub username and password.

If all goes well, you should receive a directory, titled c-vector-username. If you type ls at the prompt, you should be able to see it. It will also appear in your file browser window with Repl.it on the left. Then navigate into that directory by typing in:

cd c-vector-username

… and you should be all set to work.

2 Assignment overview

Python, Java, and many other languages have the capability for an array that automatically expands itself as necessary. In Python it is called a "list"; in Java, it is called an "ArrayList"; generically, it tends to be referred to as a vector or a dynamic array. C has no such built-in vector capability, though it does have fixed-size arrays. For this assignment, you will implement a vector in C.

In the repository that you have cloned, you should find three files within: vector.h, vector.h, and tester.c. Your assignment is to add to the file vector.c, which should contain the implementations for all of the functions prototyped in vector.h. Make sure to read the comments in vector.h carefully and follow all of the instructions. Make sure to change only vector.c and leave the other files untouched.

3 Part 1

Implement the functions init, print, insert, and cleanup. Don't bother yet with making the array automatically double. You will need to make sure you have minimal stub code in the remaining functions to make it compile.

Here's how to get your code to compile: at the bash prompt, type:

clang -o tester vector.c tester.c 

which will create an executable file called tester which you can then run:

./tester 1

Notice the 1 at the end of the prompt: this will run tests just for part 1.

We will be using clang to test your code, so you should do the same. Make sure that you do not have any compiler errors or warnings under clang (warnings are often a sign that you have a bug, anyway!).

Your code should have no memory leaks or memory errors. We'll be testing this valgrind, which is a wonderful tool for detecting memory bugs in C. If valgrind produces error messages, this is considered incorrect, so it's important that you check it yourself.

To use valgrind, first compile your code with debugging info enabled:

clang -g -o tester vector.c tester.c 

Then run your executable through valgrind as follows:

valgrind --leak-check=full ./tester 1

(Again, don't miss the 1 at the end above)

You should see reports for every leaked block of memory, including file and line information for when the allocation happened.

When you have completed part 1, make sure that if for some reason you added new files that you have added them with Git, and that you have committed and pushed your changes. Using the same tag technique that you used in the lab, add the tag vectorpart1, and then push the tag. GitHub will run tests for both part 1 and part 2. If you have not yet completed part 2 yet, GitHub will show a red X for the tests failing, but if you look at the details you should be able to see the part 1 portion passed.

4 Part 2

Implement any remaining functions that you skipped in Part 1, and implement auto-doubling as well. To test part 2, use exactly the same commands shown in part 1 above to compile and run your code, but use a 2 at the very end of the line to run your program, instead of a 1. For example, to run it through valgrind, I would type

valgrind --leak-check=full ./tester 2

4.1 Submitting

When you have completed part 2, make sure that you have added to Git any new files, and that you have committed and pushed your changes. Using the same tag technique that you used in the lab, add the tag vectorpart2, and then push the tag.

5 Capstone work

Work in this section is 100% optional, and not worth any points. Nonetheless, if you're looking for an extra challenge, these are fun additional exercises to try.

Duplicate vector.h and vector.c to create two new files, stringvector.c and stringvector.h. Instead of holding integers, this new vector will hold C strings, i.e., arrays of characters. Create a new set of tests to test it out. Whenever you add a string to your vector, make sure that you make a copy of it. The assumption we'll make for this assignment is that the vector is then responsible for cleaning up any strings added to it.

6 How to test and submit your work

Go back and look at the sections at the end of Scheme Lab 2 labeled "How to test your work" and "How to submit your work." Those should apply identically here. Follow those instructions, and you should hopefully be all set.


Assignment written by Dave Musicant, with some updates and improvements by Laura Effinger-Dean.