CS240 Lab 5
Fun with Number Base Converter

Overview

In this lab, you are required to read an interger in base 10 and convert it into various bases according to the user specification. You must implement the conversion part using a recursive function.

Background

As an overview, 123 in base 7 would be in decimal, which is 66. The number 7B in base 15 would be in decimal, which is 116.

To convert a decimal number to another number system (i.e., the base), you can use the method shown in the following figures (which show conversion into binary):

Observe that to convert the integer, we divide the number by the base. The remainder gives the least significant digit of the converted number. The quotient from the first division is divided by the base again. The remainder gives the second least significant digit of the converted number. We repeat the procedure until the quotient becomes smaller than the base. When that happens, the conversion is completed. Hint: You may want to make the recursive call before printing a converted digit, because the first converted digit will be the least significant digit.

Lab Exercise

To fulfill the basic requirement of the lab, you will need to write a program that converts an input positive integer (in base 10) into other bases, which are also inputs by the user.

Notice the following:

Input and Output format

The basic program should use the following prompts to get user input:

Please enter a positive integer in base 10: 
Please enter the target bases: 

Tips and Hints

Recursive Function

Recall that a recursive function is a function that calls itself, and the three rules of recursion are:

Sample Output

The sample output of the program is shown as follows,

lore 39 % ls
Makefile    lab05.c
lore 40 % make
gcc -o lab05.out lab05.c 
lore 41 % ./lab05.out
Please enter a positive integer in base 10: 161
Please enter the target bases: 2 13 9
The number in base 2 is: 10100001
The number in base 13 is: C5
The number in base 9 is: 188

lore 42 % ./lab05.out
Please enter a positive integer in base 10: 1879
Please enter the target bases: 5 11 19
The number in base 5 is: 30004
The number in base 11 is: 1459
The number in base 19 is: 53H

Turnin

When you are satisfied that your program works correctly ( or you run out of time ), please do the following.

Name your program as lab05.c.

  1. Create a directory named lab05

  2. Copy all program files ( *.c and *.h ) and your Makefile to the directory created in step 1.

  3. Goto the parent directory of lab05 and type the following command into the terminal:

    turnin -c cs240=XXXX -p lab05 lab05

    where XXXX represents your lab section number (number only!).

    The turnin section should be as follows:

    Section Time TA
    0201 Th 15:30-17:20 Dan Zhang
    0301 F 9:30-11:20 Suli Xi
    0401 F 13:30-15:20 Youhan Fang
    0501 Th 9:30-11:20 J.C. Chin

    Make sure turnin reports that your project was submitted for grading.

  4. Submit often to make sure that you did not try to submit after turnin has been turned off.

  5. You can check the files you have submitted by running the following command:
    turnin -c cs240=XXXX -v -p lab05
    where XXXX represents your lab section number (number only!).

Grading Breakdown

2 points Makefile exists and works correctly
5 points Read input correctly
5 points Recursive function is properly used
5 points Conversion works correctly
3 points Print the result correctly