CSCI 241 - Homework 7:
Add it up!
Due by 11:59.59pm Tuesday April 24, 2018
You may work with a partner on this assignment. The GitHub URL for this assignment is https://classroom.github.com/a/onhCZKte
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.
- Skip leading whitespace -- you can consider multiple blank lines as
this, so you are welcome to just use isspace() in a while
loop.
- Skip leading 0's in the number
- Read in the digits from most significant to least significant (i.e.,
"123" is one hundred twenty-three)
- Once you run out of digits, discard the rest of the line until a newline
is reached (or EOF)
- 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.
- Print out the total so far by printing the string "Total: "
with one space and then the total seen so far.
- 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;
};
Doubly Linked
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:
- A positive integer indicating the max # of digits in each number
- A positive integer indicating the number of numbers to be generated
- 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
- Your name and a description of the programs
- A listing of the files with a short one line description of the contents
- Any known bugs (including valgrind warnings) or incomplete functions
- An estimate of the amount of time you spent completing this assignment
- 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:
- 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
- Good comments
- Runs under valgrind with no errors or warnings
- A README with the information requested above. The listing of known
bugs is important.
Last Modified: Oct. 26, 2017 - Roberto Hoyle