# CSCI 241 - Homework 7:Add it up!

Due by 11:59.59pm Tuesday April 18 2017

You may work with a partner on this assignment. Consider using a git repository on https://dev.cs.oberlin.edu

## Introduction

For this assignment, I want you to create a program called additup that will add up a sequence of positive integers. One catch, the integers can be any number of digits long.

## Program behavior

Your program should read in a sequence of integers, one per line using the following algorithm.

1. Skip leading whitespace -- you can consider multiple blank lines as this, so you are welcome to just use isspace() in a while loop.
2. Skip leading 0's in the number
3. Read in the digits from most significant to least significant (i.e., "123" is one hundred twenty-three)
4. Once you run out of digits, discard the rest of the line until a newline is reached (or EOF)
5. If there was no valid integer at the start of a line, you should return as if you had read in the value 0. If you hit EOF without seeing a number, you should return NULL.
6. Print out the total so far by printing the string "Total: " with one space and then the total seen so far.
7. Once you hit EOF, your program should stop. Remember that you can signal EOF by hitting ctrl-D on your keyboard.

## Sample run

```% ./additup
1234
Total: 1234
9876
Total: 11110
1                       bobo
Total: 11111
1234567890987654321
Total: 1234567890987665432
^D
```

There is also a sample binary (Linux) for you to play with in ~hoyle/pub/cs241/hw07/

## Programming details

In order to support arbitrarily long integers, you'll need to represent your data in a linked list format. I'll leave it up to you to decide if you want singly or doubly linked lists, but the nodes should look like one of the following:

```Singly Linked
struct BigInt {
int digit;
struct BigInt *next;
};

struct BigInt {
int digit;
struct BigInt *next, *prev;
};
```

Some things to keep in mind:

• You should use an accumulator to keep track of the sum. Set the value initially to 0 and then keep adding it to the next BigInt.
• Addition takes place from the least significant digit on up.
• You need to be able to handle carrying values from one position to the next. No digit value should be greater than 9.
• Printing takes place from the most significant digit on downward. This, coupled with addition above, means you'll need to have to traverse your list in both directions (either doubly-linked or just reverse it as needed). Note: do not rely upon the recursion to be able to do the reversing for you.

### Error conditions

Check to see if malloc() fails. If it does, print a message and immediately exit with a non-zero value. (e.g., EXIT_FAILURE from exit(3) or something from the sysexits(3) group if you'd prefer)

### Output matching

As there is very little output specified, I'm going to require that you match the sample output exactly.

### Keep your memory clean and tidy

To keep you thinking about your memory management, I want you to clean up all memory that you dynamically allocated before the end of the program. That means that you should be able to run under valgrind with no memory leaks reported.

## Testing

I've provided a program called gen_nums that will generate random positive integer sequences for you. It is located in ~rhoyle/pub/cs241/hw07/ as well. It requires 2 parameters with an optional third. They are:

1. A positive integer indicating the max # of digits in each number
2. A positive integer indicating the number of numbers to be generated
3. An optional positive integer to be used as a random number seed.

I'm using the C pseudo-random number generator so that you should get identical sequences of numbers for the same input parameters. You can vary the seed to get different sequences if desired.

```% ./gen_nums 10 3 1
6753
629127
9
% ./gen_nums 10 4 1
6753
629127
9
6062
% ./gen_nums 10 4 2
9
518475729
3794370229
710063
```

## handin

Create a file called README that contains

1. Your name and a description of the programs
2. A listing of the files with a short one line description of the contents
3. Any known bugs (including valgrind warnings) or incomplete functions
4. An estimate of the amount of time you spent completing this assignment
5. Any interesting design decisions you'd like to share

```    % cd ~/cs241
% handin -c 241 -a 7 hw7
% lshand
```

Here is what I am looking for in this assignment:

• A working Makefile with your program, all, and clean as targets
• A program that will add up any length of positive integers, ending after receiving an EOF
• An internal linked-list representation using structs
• Output matching the sample program
• Appropriately modular code
```valgrind:           [/5]