Programming Assessment 1
Programming Assessment 1
Programming Assessment 1
Learning Outcomes
In this assignment you will demonstrate your understanding of arrays, strings, functions, and the
typedef facility. You may also (but are not required to) use structs (see Chapter 8) if you wish.
You must not make any use of malloc() (Chapter 10) or file operations (Chapter 11) in this project.
Big Numbers
It is sometimes necessary to work with very big numbers. For example, application areas such as
cryptography require exact manipulation of integers containing hundreds of digits. And by now you
will be aware that the standard int and double data types do not possess that ability.
In this project you will develop a high-precision numeric calculator. You’ll be using C arrays
to store the digits of the numbers, so it won’t quite be arbitrary precision; but it will certainly be
possible to have 500-digit values manipulated exactly within reasonable computation times, which is
big enough for many purposes.
Before doing anything else, you should copy the skeleton program ass1-skel.c from the LMS
Assignment 1 page, and spend an hour or two reading through the code, understanding how it fits
together. Check that you can compile it via either Grok or a terminal shell and gcc. Once you have it
compiled, try this sequence:
mac: ./ass1-skel
> a=12345
> a+23456
> a?
register a: 35801
> a+a
> a?
register a: 71602
> ^D
ta daa!!!
The “>”s are a prompt printed by the program, with the remainder of each of those lines the commands
typed by the user. There are also two output lines in response to the “?” commands, and the final (what
else?!) “ta daa!!!” message printed by the program as it exits. (Note that there is some fiddly code
that makes use of the isatty() function to decide where to write each output line, so that the program
works sensibly when input commands come from a file, and also when the output is directed to another
text file. You do not need to understand how all that code works.)
The calculator maintains 26 one-letter “variables” (or registers), each of which has an initial value
of zero, and can store one integer value. Each “command” operates on one of those 26 variables,
applying a single operator to it, using (except for the “?” operator) one further non-negative integer
operand. So, in the example shown above, register “a” is first assigned the value 12345; then it has
23456 added to it; then it is printed; and then register “a” has register “a” added to it; and then it is
printed a second time.
The skeleton program uses a simple array of int variables to store the 26 registers. This skeleton
is provided as a starting point, so that you don’t have to develop a whole lot of uninteresting functions.
1
The only operators provided in the skeleton program are “=” (assignment); “+” (addition); and “?”
(printing). And the arithmetic will suffer from integer overflow, of course.
8 2 4 5 5 4 3 2 1 ? ? ...
where v[0] stores the (current) number of digits in the register’s value; where “?” represents a value
that isn’t currently in use; and where the array v is declared to contain a total of INTSIZE+1 elements.
All input numbers will be non-negative integers, and nor is a subtraction operator required. That
means that you do not need to store a sign, nor worry about negative numbers.
If you wish to read Chapter 8 early, you may instead define and use a suitable struct for each of
the registers (rather than the array option that is illustrated in the example), and then maintain the set
of registers in an array of struct (rather than as an array of arrays). Note that the use of struct types
is not required in this project, and you can get full marks without them.
As part of this first stage you’ll need to rewrite the function do plus() so that two variables of
your new type longint_t are added correctly, and you’ll also need to modify several other functions
(zero_vars() and do_assign(), plus others) so that they operate correctly after you have changed
the underlying type. You should carefully plan the required changes before you jump in and start
typing, because you’ll need to do quite a bit of editing before getting the program back to “compil-
able/testable” state again.
As part of your changes, your program should check for overflow beyond INTSIZE digits both
when constants are being input, and also when addition is being performed. If an overflow situation
arises your program should print a suitable error message (your choice of wording) and then exit.
Successful completion of this stage means that your program should be able to handle additions
involving numbers of up to INTSIZE (currently 500) decimal digits. Your program should detect any
operations that overflow beyond INTSIZE digits, and write an error message of your choice and then
exit, returning EXIT FAILURE.
2
As a second change required in this stage, note the commas in the output values, following the
Australian convention of placing them every three digits, counting from the right. (Other cultures
have different conventions.)
General Tips...
You will probably find it helpful to include a DEBUG mode in your program that prints out interme-
diate data and variable values. Use #if (DEBUG) and #endif around such blocks of code, and then
#define DEBUG 1 or #define DEBUG 0 at the top. Turn off the debug mode when making your
final submission, but leave the debug code in place. The FAQ page has more information about this.
The sequence of stages described in this handout is deliberate – it represents a sensible path
through to the final program. You can, of course, ignore the advice and try and write final program
in a single effort, without developing it incrementally and testing it in phases. You might even get
away with it, this time and at this somewhat limited scale, and develop a program that works. But in
general, one of the key things that makes some people better at programming than others is the ability
to see a design path through simple programs, to more comprehensive programs, to final programs,
that keeps the complexity under control at all times. That is one of the skills this subject is intended to
3
teach you. And if you submit each of the stages as you complete it, you’ll know that you are accumu-
lating evidence should you need to demonstrate your progress in the event of a special consideration
application becoming necessary.
Nor should you post your code to any public location (github, codeshare.io, etc) while the assign-
ment is active or prior to the release of the assignment marks.