Lab1

Reading:

The World of C++, part 1

The World of C++, part 2

Inheritance in C++

Code Notes: Intro to C++, part 4

Using Emacs editor:

Quick Emacs Reference

Debugging in Emacs

Files:

Copy all files from the lab1/problem folder. In your home directory, make a labs/lab1 folder and issue the following command inside the labs/lab1 folder:

cp /comp/15/public_html/labs/lab1/problem/* .

Problem:

Expand the Array class from Lab 0 into a new class called AdjustableArray class, and add the following member functions:

1. Find a given input data:

// Return an index of the Array where a given data is found. 
// If not found, return -1.
int AdjustableArray::find(int aData);

2. Insert a new data after an existing entry:

// Insert aData after aExistingEntry.
// If aExistingEntry is not found, insert in the end of the Array
void AdjustableArray::insertAfter(int aExistingEntry, int aData);

3. Find and delete an input data:

// Find and delete aData.
// If not found, do nothing.
void AdjustableArray::deleteThis(int aData);

Expected Output:

(for the given main.cpp)

Negative index.
a2: { }
a3: { 1 2 3 4 5 6 7 8 9 10 }
a3: { 1 2 3 15 4 5 6 7 8 9 10 }
a3: { 1 2 3 15 4 5 6 7 8 9 10 100 }
a3: { 1 3 15 4 5 6 7 8 9 10 100 }
a3: { 1 30 3 15 4 5 6 7 8 9 10 100 }
a3: { 1 30 3 15 4 5 6 7 8 9 10 100 }

Leak Check:

You can use “valgrind” to quickly check whether your program has any memory leak. Here is the command:

valgrind --leak-check=yes ./Array

Here is the expected output from valgrind:

==13520== Memcheck, a memory error detector
==13520== Copyright (C) 2002-2013, and GNU GPL’d, by Julian Seward et al.
==13520== Using Valgrind-3.9.0 and LibVEX; rerun with -h for copyright info
==13520== Command: ./Array
==13520==
Negative index.
a2: { }
a3: { 1 2 3 4 5 6 7 8 9 10 }
a3: { 1 2 3 15 4 5 6 7 8 9 10 }
a3: { 1 2 3 15 4 5 6 7 8 9 10 100 }
a3: { 1 3 15 4 5 6 7 8 9 10 100 }
a3: { 1 30 3 15 4 5 6 7 8 9 10 100 }
a3: { 1 30 3 15 4 5 6 7 8 9 10 100 }
==13520==
==13520== HEAP SUMMARY:
==13520== in use at exit: 0 bytes in 0 blocks
==13520== total heap usage: 4 allocs, 4 frees, 180 bytes allocated
==13520==
==13520== All heap blocks were freed — no leaks are possible
==13520==
==13520== For counts of detected and suppressed errors, rerun with: -v
==13520== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 6)

Note that it reports 0 errors.

Solution:

Posted here.