As you may know, integer values in C++ are limited by the number of bits used to represent them. For instance, a 64-bit unsigned long long has a maximum value of 2^64 - 1, or 18,446,744,073,709,551,615.
To represent integers of arbitrary length, we can use a singly linked list, where each node stores a single digit. For this lab, you'll build and manipulate such lists using manual memory management (new/delete), reinforcing your understanding of object design, dynamic memory, and parameter passing.
For example, the number 123 can be stored like this:
[ 3 ] -> [ 2 ] -> [ 1 ] -> NULL
Here, the first (head) node stores the least significant digit
(3), and the last node contains the most
significant digit (1).
This lab reinforces the following core skills in CS202:
new and delete
This problem is inspired by Problem 8.19 from Elements of Programming Interviews and Problem 2.5 from Cracking the Code Interview.
You will read from standard input. Each line contains two space-separated integers:
integer1 integer2 integer1 integer2 ... integer1 integer2
Each integer is of arbitrary length, and the two integers on a line may differ in length.
Your program should add each pair of integers. Given the following input:
1 1 123 123 1 12
Your program should output the following:
2 246 13
To receive full credit, your solution must meet the following criteria:
List, LinkedList) with nodes storing type int
push_front or equivalent)
addLists(List a, List b)) that returns a new list containing the digit-wise sum
new/delete with no memory leaks or segmentation faults
std::strings and build lists digit-by-digit from them.
valgrind ./main to test for memory leads and invalid frees
We will grade your submission relative to the rubric below.
Submissions using STL containers such as std::list will receive a maximum of 23/35 points; see below.
+2 Code is well formatted, commented (inc. name, assignment, and overview), with reasonable variable names
+12 Uses custom list with fundamental type int. If your list code exactly mirrors the
provided sample without any adaptation or simplification for the specific problem (e.g.,
minimal methods, no unnecessary generalization), up to 6 points may be deducted.
+2 Defines a function that takes two lists and returns the sum as a linked list
+3 Your submission must pass Valgrind tests to receive the full 3 points for memory correctness.
+18 Test cases involving at least one number with two or more digits successfully solved (2 points each)
To facilitate testing, you were previously asked to clone the course Github repository as follows:
git clone https://github.com/semrich/CS202-fall24.git cs202
For this assignment, update this clone by using the following:
git pull
The files list.h, list.cpp, and main.cpp are required. Ensure that your Makefile or test script recognizes main.cpp as your entry point. To test your solution against ours, type:
make test
Please run the following command prior to submission:
tar -cvf lab4.tar list.h list.cpp main.cpp
Note: Although submission will be facilitated by Canvas, we will compile and test on EECS lab machines!
If you develop your solution elsewhere please make sure it works on
the lab computers prior to the deadline.