What you are dealing with is called
base conversion.
• Numbers do
not have a “base” (or “radix”).
• Human readable
representations of numbers have a radix (or base).
The
radix is the number of symbols you use for digits. Most humans use a radix of TEN, with the arabic digits:
0 1 2 3 4 5 6 7 8 9
After that, numbers are expressed as a polynomial. For example, the number 123 (base=radix=10) is the polynomial:
1×10
2 + 2×10
1 + 3×10
0
See the radix in there? (All those tens.)
The
place is the power to which we raise the radix. The radix works with negative powers too:
1×10
-1 + 2×10
-2 = 0.12
The radix can be anything you want. In computer science, radices of 2, 8, 10, and 16 are most common. The following all equivalent:
10102 (radix = 2, binary)
128 (radix = 8, octal)
1010 (radix = 10, decimal)
A16 (radix = 16, hexadecimal)
Notice that the radix is always expressed in base 10.
Notice also that these are all the
same number, even though they are all
written differently.
To the exercise you recounted in your OP explains how to take a number and create a human-readable representation of that number. However, there are two important mistakes which I think have confused you.
The exercise converts the
base 10 number “17” into a base 3 number.
The base 10 number is 1×10
1 + 7×10
0.
The equivalent base 3 number is 1×3
2 + 2×3
1 + 2×3
0 (or 122
3).
You can verify that these are equivalent by doing the math:
1×101 + 7×100
= 1×10 + 7×1
= 10 + 7
= 17
1×32 + 2×31 + 2×30
= 1×9 + 2×3 + 2×1
= 9 + 6 + 2
= 17
The method to do it is:
(1) peel off the least significant digit using a remainder of division with the radix
(2) reduce the number using integer division with the radix
(3) repeat until there is nothing left.
Here is your example:
17 / 3 = 5R2 → 2 is the new least significant digit (1’s place)
5 / 3 = 1R2 → 2 is the next digit (3’s place)
1 / 3 = 0R1 → 1 is the final digit (9’s place)
What remains is zero, and we can discount an infinite number of leading zeros from any number. So we are done! The new number is:
122
3
Your mission, should you choose to except it, is to take a number (which does not have a base!) and convert it to a human-readable representation using a given radix (or base).
In C++, to obtain the
remainder, use the
%
operator:
17 % 3 = 2
In C++, use the
/
to obtain the integer quotient:
17 / 3 = 5
By repeating, in this order (take the remainder, then take the integer quotient of division), until your number is reduced to zero.
The only
special cases you need to worry about are:
• negative numbers (which I don’t think your homework requires you to care about)
• zero (if a number is zero to begin with, you still want to output a “0”).
Input:
• a number, N
• the radix (or base), R
Loop termination condition:
• quit when N == 0
Output:
• a string
One more thing. In C++, to convert a digit into a character, add
'0'
to it. So I can represent the number 7 by adding the value of the symbol representing zero.
The hard part of this kind of assignment is that you peel the least-significant digits off first, and those appear at the
end of the string. Fortunately C++ makes that a pretty easy thing to overcome:
1 2 3 4
|
std::string s;
s = '0' + s; // s == "0"
s = '4' + s; // s == "40"
s = '9' + s; // s == "940"
|
This should be enough to help you write a function that converts a
single number into a
single string.
Once you have that function written,
then you can move on to manipulating lists of numbers and the like.
Hope this helps.