600.120 Intermediate Programming
Spring 2009 -- Assignment 2
Due Wednesday, 2/11 by 2:45pm
The goal for this week is to do structured programming in C, using functions
and pointers to organize processing and update data.
- Readings: C text Chapters 4-6, Appendix B
- Tutorials: Continue working with unix and emacs.
- Compilation: gcc -ansi -std=c99 -pedantic -Wall -Wextra -O pg#.c
- Submission: Upload your source file pg2.c to the webCT assignment page.
Make sure that you hit SUBMIT after uploading. You can retrieve and resubmit
as many times as you want before the deadline. If you retrieve, it's as if
you never submitted, so make sure you have time to resubmit! If you need to
explain anything in general, include a README plain text file with your
submission. Anything not on webCT by the deadline gets a 0 = no credit. Also
bring a printout of your source code to class on the due date.
- Collaboration: This is an individual assignment. Remember that you may
use or adapt code from your texts and lectures, but you MUST include a comment
to cite the original source.
- Help: check the TA
info course webpage for hours when help will be
available in the CS lab. This will be updated with one more person over the
next week.
- Grading: 10 points style & submission, 5 points reflection questions, 30
points functionality = 45 total. -5 points if there are any warnings using
the required compilation options. 0 points if it doesn't compile (has errors,
no a.out created). Any violation of the implementation details will result
in a 0 grade for the assignment.
The Problem:
Write a program (in file pg2.c) that can be used to convert numbers from
one base to another. In order to make it easier to test your program (and,
hint hint, to write it), always output the original value as a decimal (base
10) also, even if it is not requested. If a requested base b is <= 10, then
it uses the digits 0 through b-1. When a base is >10, then it also uses the
lower case letters of the alphabet as digits. For example, something in base
20 would use digits 0-9 and letters a-j, corresponding to values 10-19. You
are also required to do error checking on the input and stop processing when
an error occurs, displaying a relevant error message. Your program is
required to work (and should error check these inputs also) for bases 2
through 36. Here are some sample runs:
Enter original base: 13
Enter number in base 13: a3b50
(Decimal value is 294125)
Enter desired base: 6
Converted value is 10145405
Enter original base: 4
Enter number in base 4: 123456
ERROR: invalid characters in number
Enter original base: 4
Enter number in base 4: 2031
(Decimal value is 141)
Enter desired base: 1b6
ERROR: invalid base input
Implementation Details:
- You MUST NOT use any form of string variables (character arrays) to solve
this problem. You may use string literals for output.
- You MUST use multiple functions to organize the processing.
- You MAY NOT use global (external) variables.
- You should get input in functions other than main.
- You can only use getchar() for input.
- You will probably want or need to use some recursion.
- You will probably need to use pointers for pass by reference.
Reflection: Are there any restrictions on the values that your
solution will process correctly? If so, what and why? Could you eliminate
the problem without violating the implementation details? Discuss in a
comment at the start of your program.