lab

EECS 280 Lab 2: Pointers and Arrays

Lab Due Sunday, 3 Tuesday, 5 Feb 2019, 8pm

In this lab, you will review the basics of pointers in C/C++ and practice how to use them to traverse and manipulate arrays. Additionally, you’ll learn to use a unit testing framework, which will allow you to test your code using a tool that is similar to those you will find in real world development. Finally, we’ve prepared a worksheet that reviews similar topics.

You may work alone or with a partner. Please see the syllabus for partnership rules.

Submit the code files below on the autograder. We encourage you to complete the lab worksheet, but it is not turned in for credit.

Files to submit

Completion Criteria/Checklist:

This checklist will give you an idea of what we look for when grading.

To pass this lab, you must complete tasks 1-3. Task 4 is optional.

Review

Traversing Arrays

There are two basic ways in which we can traverse an array, we can keep track of the current element by using an index or using a pointer. Even though each of them have different pros and cons, throughout the course we may ask you to use one over the other. Below there are two snippets of code that contrast both methods.

int arr[5] = { 1, 2, 3, 4, 5 };

// Traversal by Index
int sum = 0;
for (int i = 0; i < 5; ++i) {
  sum += arr[i];   // or sum += *(arr + i);
}

// Traversal by Pointer
int sum = 0;
for (int *ptr = arr; ptr < arr + 5; ++ptr) {
  sum += *ptr;
}

Note: Since arr[i] is the same as *(arr + i), the latter is still traversal by index.

The biggest difference is whether the variable that controls the loop is an index (i.e. an int) or a pointer to an element. There are a few other things worth noting:

  Traversal by Index Traversal by Pointer
Initialization The loop index starts at 0. The pointer starts at the address of the first element.
Loop condition Loop while the index is less than length. Loop while the pointer has not reached address arr + length, which is past the end of the array.
Increment Increase index by 1. Move the pointer forward by 1.
Element access Array indexing - use index as offset from start of array and then dereference. The pointer is already pointing to the current element, so just dereference it (i.e. *ptr).

Lab Exercises

The Files

We have provided starter files for this lab. Use the following commands in a terminal at your working directory to download the files.

$ wget eecs280staff.github.io/lab/lab02/starter-files.tar.gz
$ tar -xvzf starter-files.tar.gz

Here’s a summary of this lab’s files. You will turn in the bolded ones to the autograder.

File Description
lab02.h Function declarations for lab02.cpp.
lab02.cpp Write implementations of the lab functions here.
lab02_main.cpp Contains a main function that calls the lab functions.
lab02_tests.cpp Contains unit tests that call the lab functions.
unit_test_framework.h Declarations and macros of the unit test framework library.
unit_test_framework.cpp Implementation of the unit test framework library.

Testing Code

The lab02_main.cpp file contains some simple code you can use to sanity check your function implementations.

In task 3 you will learn to use a unit testing framework to write additional unit tests for each function individually.

The starter code should “work” out of the box, so make sure you are able to compile and run it with the following command. The code may be missing some pieces, contain some bugs, or crash when you run it, but you’ll fix each throughout the course of the lab.

$ g++ -Wall -Werror -g -pedantic --std=c++11 lab02.cpp lab02_main.cpp -o lab02.exe

Task 1 - Printing an Array

Your first task is to write two versions of a function to print out an array. In the first version, you should use traversal by index to iterate through the array. In the second version, you should use traversal by pointer. Find the printArrayIndex and printArrayPtr functions in lab02.cpp and complete their implementations.

Task 2 - Array Transformations

Now, let’s implement some functions that apply transformations to an array. In these functions, you must use traversal by pointer.

Your implementation for these functions should be “in place”. That is, you may not allocate an extra array, although you may use pointers or temporary ints as you need.

You will find function stubs for each of the transformations in lab02.cpp. Fill in the implementation for each. The main function already in lab02_main.cpp contains code to test these as well.

Task 3 - Unit Test Framework Tutorial

We’ve put together a lightweight testing framework that will help you write and run tests for the course projects. To learn how to use it, work through the tutorial at:

https://eecs280staff.github.io/unit_test_framework/

The tutorial will show you how to write test cases for the functions that you implemented in this lab. This is a required part of the lab and you must submit your completed tests in lab02_tests.cpp to the autograder.

Task 4 - Challenge (Optional)

The following is an example of a question that might show up on a coding interview:

Let’s say you have a sorted array, but there may be duplicate elements. How can you efficiently remove the duplicates? Write an implementation of the removeDuplicates function in lab02.cpp. Your solution must use traversal by pointer. Again, as described in task 2, your solution must be “in place” and must run in linear time.

Submit

Submit the required files to the autograder.