CS240 Spring 2011: LAB02


Goal

Implement convert, a program to convert a number from one base system to another base system.

$ convert 11011 2 10
27

Introduction

The base or radix is the number of unique digits used to represent a numerical value. For example in the decimal system (base 10), the digits 0,1,2,3,4,5,6,7,8,9 are used to represent numbers. Similarly in the binary system (base 2), digits 0 and 1 are used.

In systems with a base higher than 10, alphabets beginning with 'a' are used to represent digits greater than 9. For example in the hexadecimal system (base 16), the digits used to represent numbers are: 0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f. Here, the letter 'a' represents the value 10 (decimal). If "10" was used instead of 'a', it could mean either "10" or '1' followed by '0'. Hence letters (or symbols in general) are used for digits greater than 9.

A number in any base system can be converted to the decimal system and vice versa; a brief tutorial is available at the following links:
Convert to base 10 from any other base
Convert from base 10 to any other base.

Instructions

  1. Start by creating a directory lab02.

  2. Write your program in a file "convert.c" and include a makefile for compiling it to the target "convert".

  3. The program convert takes three arguments : a number, the base system it is in, the base system to which it has to be converted.

  4. Assume any letters in the number will be in lower case. Similarly, when printing the result, print any letters in lower case.

  5. The program must check whether all arguments are present and that they are valid-- the number to be converted should be a valid number in the specified base system.

  6. The program should print only the result (the given number in the target "base system") followed by a new line. Please look at the examples below for more information.

  7. You may assume that irrespective of the base all numbers will have less than 32 digits and that they will be non-negative integers.

  8. Please make sure that the output of your program is the same as that of the reference implementation provided HERE. You may use THIS script to compare the output of your program against the reference implementation.

    Some sample input/output is provided below.

    		
    $ convert 1234 4
    Usage convert <number> <from_base> <to_base>
    $ convert 123a 4 7
    Quitting for a with base 4
    The entered number is invalid for the given base
    $ convert 123 4 7
    36
    $ convert 123 10 7
    234
    $ convert 123 4 10
    27
    $ convert 123 4 37
    The bases should lie between 2 and 35
    $ convert 123213185231412342341231234234423412321341234234123441234 5 8
    The entered number is longer than 32 digits, please enter a smaller number
    

Submit

Type cd .. in lab02 and change working directory to the parent directory of lab02.

In the parent directory of lab02, type turnin -v -c cs240=XXX -p lab02 lab02 to turnin your work. Replace XXX with your section number-- F1130, F130, F330, F730, F930, R1130, R330, R930, T1130.

Now, you may use the command, turnin -c cs240=XXX -p lab02 -v to verify your submission.

This lab is due on Monday 31 January 11:59PM.