CSCI 241 - Homework 7:
Add it up!

Due by 11:59.59pm Monday, May 9

The GitHub URL for this assignment is https://classroom.github.com/a/3Pz6W07n

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 ~rhoyle/pub/cs241/hw7/

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;
};

Doubly Linked
struct BigInt {
    int digit;
    struct BigInt *next, *prev;
};

Some things to keep in mind:

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

Extra Credit

Handle multiplication as well as addition.


handin

README

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

Now you should make clean to get rid of your executables and handin your folder containing your source files, Makefile, and README.

Grading

Here is what I am looking for in this assignment:


Last Modified: Oct. 26, 2017 - Roberto Hoyle