I am having trouble figuring out how to convert base 10 to base 3, I have tried several different attempts and this is my latest effort, anyone have any ideas.
I was giving this algorithm but am having trouble creating a function for it:

1. Set k to the number of digits needed in the converted number(k = floor(logb(v)+ 1) or (floor(10(v)/log10( b )+1)
2. Print digit (v/(b^(k-1)) most significant remaining digit
3. set v to v%(b^(k-1)) the remaining part of the base 10 digit
4. subtract 1 from k(reduce the power on the base by 1)
Implementation position = position/base

#include <iostream>
#include <cmath>
#include <iomanip>
#include <fstream>

using namespace std;

int convert_base(int v, int b);
ifstream inputdata;

int main(int argc, char *argv[]){
int a, b, c, d, e, f, i;
int base = 3;
int count = 6;

inputdata.open("Base_Conversion_File.dat");
if(!inputdata){
cout << "There was a problem opening the data file." << endl;
return 1;
}
inputdata >> a >> b >> c >> d >> e >> f;
cout << a << "\t" << b << "\t" << c << "\t"
<< d << "\t" << e << "\t" << f << endl;

for(i = 0; i <= count; i++){
if(i == 0){
cout << a << " converted to base 3 is:\t" << convert_base(a, base) << endl;
}
else if(i == 1){
cout << b << " converted to base 3 is:\t" << convert_base(b, base) << endl;
}
else if(i == 2){
cout << c << " converted to base 3 is:\t" << convert_base(c, base) << endl;
}
else if(i == 3){
cout << d << " converted to base 3 is:\t" << convert_base(d, base) << endl;
}
else if(i == 4){
cout << e << " converted to base 3 is:\t" << convert_base(e, base) << endl;
}
else if(i == 5){
cout << f << " converted to base 3 is:\t" << convert_base(f, base) << endl;
}
}
return 0;
}

int convert_base(int v, int b){
int k, i, convert;

k = floor(log10(v)/log10(b)) + 1;
cout << (int)(v/ pow(b, k-1));

v = v % pow(b, k-1);

return k;
}
2
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by monkeybut

Your professor has actually given you a complete solution -- but his pseudocode is a bit off.

Your function should take only one argument: v - the value to convert to base 3.

Everywhere you see "b" in his pseudocode you should replace that with "3" (because "b" is the base -- your base is 3). (You could, if you desire, add a second argument to the function: b, and use that as the base, then call the function with a base of 3. It is trivial to make the function work either way.)

Inside the function, the first thing you should do (as you have) is calculate k. You have used the second form the professor gave you, since there isn't any log3() function in the standard library. Remember that the logarithm to any arbitrary base "b" can be calculated by dividing the logarithms using another base (for example, base 10) as:
{\bf log}_{3}( v ) = \frac{{\bf log}_{10}( v )}{{\bf log}_{10}( 3 )}

The number k represents the number of digits in the output. Your professor's algorithm uses it to select the bits out of the argument v that are to be printed - most significant to least significant. Most numbers have more than one digit, so you will need to loop through them:

for (; k != 0; k--)

(His pseudocode was not very careful to indicate that there is a loop in there... but if you think about it you will see it is necessary. Step 4 is all about the loop. Perhaps he should have had a "step 5: if (k != 0) start again at 1" or something like that.)

I am glad to see also that you caught the need to use pow() where he wrote "^" and the need to cast the results to int (I would have used unsigned long since the algorithm will not work with negative numbers). I would also calculate pow( b, k - 1 ) only once, using a temporary integer variable.

There is one final consideration to the algorithm which your professor did not indicate: a v with value zero will have a k of zero. You need to check if v is zero (or k -- whichever) and simply cout << "0"; if that is the case, instead of going through all the calculations and loops.

Oh, I almost missed this: your convert_base() function should return void -- it is supposed to use cout directly. (As per your professor's instructions. I would have used an ostringstream and returned a string to the caller, so that you can use the function exactly as you do...) So what your professor wants to see is something like:

int v;
while (inputdata >> v) {
cout << v << " converted to base " << base << " is ";
convert_base( v, base );
cout << ".\n";
}

Don't waste effort like you are with lines 25 through 44. All you need to do is call the function as above.

Hope this helps.

Thanks that worked, and thanks for the advice for lines 25-44, that cut out a lot of code and actually made my output format the way I needed.