Challenge 07: Adding List-Based Integers (revisited)

Problem overview

This is an update of a lab given to fall 2022 CS202 students. See this page for most of the same overview.

As you may already know from earlier courses, integer values in C++ (as well as other programming languages) are limited by the number of bits used to represent these data. For instance, a 64-bit unsigned integer has the maximum value of 2^64 - 1 or 18446744073709551615.

One method of representing integers of arbitrary length, which was covered last spring, is a a linked list data structure such that that a node in the linked list corresponds to a single digit in the integer.

For instance, the number 123 can be stored as linked-list that could look like this:

[ 3 ] -> [ 2 ] -> [ 1 ] -> NULL

Note that the first (or head) node in this list contains the least significant digit (in this case 3), while the last node contains the most significant digit (1).

For this problem, you are to read in pairs of arbitrary length integers and produce correct output as outlined below.

Inspiration

Note, this problem is inspired by Problem 8.19 from Elements of Programming Interviews and Problem 2.5 from Cracking the Code Interview.

The goal is to cover an interview-related practice problem based on a basic data structure (container) from a CS140 lab, but with a more explicit C++/OOP-centric view, namely using the STL and/or templating. Using friendship and/or operator overloading is optional.

Input / Output

You will be given a series of integers from standard input in the follow format:

integer1 integer2
integer1 integer2
...
integer1 integer2

Each integer is of arbitrary length. Moreover, integer1 is not guaranteed to be the same length as integer2.

Example

You are to add each pair of integers. Given the following input:

1 1
123 123
1 12

Your program should output the following:

2
246
13

Requirements

On top of being interview prep, this specific assignment is intended to both review and assess your understanding of templated sequence containers. Please choose one of the following solution frameworks:

  1. You can create a custom, templated linked list implementation in C++ (i.e., you cannot use std::list); however, you are allowed to use Dr. Emrich's code and/or your notes from a prior class as a starter/template.

  2. You can use the appropriate STL container adapter for this problem (HINT: this is either a queue or a stack). You must instantiate this container adapter with the with the fundamental type int.

Either implementation must manage dynamic memory properly for full credit (i.e., no memory leaks or segmentation faults). This is obviously harder with Option #1.

Hints (same as before)

  1. You may wish to read the integers in as std::strings.

  2. Be sure to account for the carry when you perform addition.


Rubric

We will grade your submission relative to the rubric below.

+2    Code is well formatted, commented (inc. name, assignment, and overview), with reasonable variable names
+5    uses one of the two allowed methods for solving this problem
+3    No issues reported by valgrind on second test applied
+18   Test cases involving at least one number with two or more digits successfully solved (2 points each)

Testing your code prior to submission

To faciliate testing, you were previously asked to clone the course Github repository as follows:

git clone https://github.com/semrich/cs302-23.git cs302

For this assignment, update this clone by using the following:

git pull

We'll discuss this in class but note that your program must be named "solution.cpp" and compilable using make. To test your solution against ours, type:

make test