CS240 Lab 2
Number Conversion and Palindromic
Numbers
Project Goal:
To manipulate characters/numbers/strings, do base conversion and format output. It
introduces you to a new problem that may show you how computer scientists become
interested in questions for which answers may not be known. This leads to a search
for algorithms and answers that often lead to other questions. This is the research
process.
Topics covered:
Functions, global variables, for, while, if, local variables, getchar(), basic printf, one
and twodimensional arrays, string manipulation (copy, compare, reverse,
concatenate) and outputformatting using a twodimensional array.
Task 0: Download the Initial Source Files
Download the file
lab2src.tar
and untar it by typing the command.
tar xvf lab2src.tar
The directory
lab2src
contains the initial sources as well as the script testall that will
test all your programs. To build your programs type "make" inside the directory lab2
src. Make sure you compile and test your programs in lore.cs.purdue.edu.
Task 1: Number conversions
Write a program to convert a given number from one base to another. The format is:
convert <basefrom> <baseto> <number>
You now know how to input arguments on the command line. Assume that basefrom
and baseto are integers between 2 and 25 (both inclusive). Since base 16 uses the
characters A, B, C, D, E, F to represent decimal numbers 10 through 15, bases 17
through 20 will involve additional characters. For example, base 20 will involve the
additional characters G, H, I and J. Use defensive programming to make sure your
command line arguments are what you would expect. You may assume that no
number (in any base) has more than 32 digits, and all numbers are nonnegative.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentTo convert a number, say (123)9, from base 9 to another base such as base 8, simply
convert from base 9 to decimal (i.e., base 10), to get the number, i.e.,
(1 x 9
2
) + (2 x 9
This is the end of the preview.
Sign up
to
access the rest of the document.
 Fall '08
 REGO
 Binary numeral system, Decimal, Numeral system, Palindromic number

Click to edit the document details