Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,165 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,628 people online right now. Registration is fast and FREE... Join Now!




Base Conversion

 
Reply to this topicStart new topic

Base Conversion, Converting From base 10 to base b

SCStudent86
post 6 Oct, 2008 - 06:52 PM
Post #1


New D.I.C Head

*
Joined: 6 Oct, 2008
Posts: 21



Thanked 1 times
My Contributions


I know everybody has heard this question many times. How to convert from from base 10 to base b. I'm writing this C++ program for a numerical analysis class. Our professor wants us to use the "left-to-right" algorithm. I know the mathematics behind it but I'm having problems implementing it in C++. The only problem I'm having is writing the process of the for loop to get the base - b digits. I have written code to at least figure out the highest place value, but like i said before I'm not comfortable with what I'm doing in my for loops. I'm going to post the code by text file. The program is too long to be sent as a .cpp file biggrin.gif. It has several commands in it. Our professor prefers procedural programming over OOP. DON"T ASK WHY, haha. The only help i need is what i mentioned before. If anyone has ideas on what I should do about this part of my code I would really appreciate it. THANKS!



Attached File(s)
Attached File  program.txt ( 8.14k ) Number of downloads: 13
User is offlineProfile CardPM

Go to the top of the page

AmitTheInfinity
post 6 Oct, 2008 - 11:39 PM
Post #2


C Surfing ∞

Group Icon
Joined: 25 Jan, 2007
Posts: 1,015



Thanked 34 times

Dream Kudos: 125
My Contributions


if would have been better if you posted that code in your post.

well Horner's method is not that difficult to implement, you just made it bit more complex than required.

Here is some sample to see how it can be implemented.

cpp


do
{
cout << "Enter Your base :";
cin >> base
}while(base<2||base>16);
cout<<"Enter The number for this base :"
cin.getline(number); //number is string [or character array.]
int i=0;
if(number[i] >= 'A' && number[i] <= 'F' )
num2dec = (number - 55) * base;
else
num2dec = atoi(number[i]) * base;
for(i=1;i<number.length(); )
{
if(number[i] >= 'A' && number[i] <= 'F' )
digit = number - 55;
else
digit = atoi(number[i]);
num2dec += digit;
i++;
if(i<number.length()-1)
{
num2dec = num2dec * base;
}
else
break;

}



I haven't tried to run this code. see if it works for you, and remove syntax errors if any. smile.gif

This post has been edited by AmitTheInfinity: 7 Oct, 2008 - 01:13 AM
User is offlineProfile CardPM

Go to the top of the page

baavgai
post 7 Oct, 2008 - 04:27 AM
Post #3


Dreaming Coder

Group Icon
Joined: 16 Oct, 2007
Posts: 1,959



Thanked 95 times

Dream Kudos: 475

Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions


QUOTE(SCStudent86 @ 6 Oct, 2008 - 10:52 PM) *

Our professor prefers procedural programming over OOP. DON"T ASK WHY


Probably because he still listens to disco. tongue.gif Don't know why you're using C++ instead of C at that point.

CODE

"Invalid. Can only pick values 0/1. Please re-enter.    ";


You're being far too limiting. I want to choose the symbols that amuse me. For base two I want a and d. So my numbers will look like, "dada daa da", instantly imbuing most 80's songs with binary occult meaning. wink2.gif

If you want to constrain that list, why are you asking the user in the first place? Just define something like this and you're done.
cpp

char d[] = "0123456789abcdef";


Note that if you still want to ask the user for the list, you aren't checking for dups.
User is offlineProfile CardPM

Go to the top of the page

SCStudent86
post 7 Oct, 2008 - 01:30 PM
Post #4


New D.I.C Head

*
Joined: 6 Oct, 2008
Posts: 21



Thanked 1 times
My Contributions


QUOTE(AmitTheInfinity @ 7 Oct, 2008 - 12:39 AM) *

if would have been better if you posted that code in your post.

well Horner's method is not that difficult to implement, you just made it bit more complex than required.

Here is some sample to see how it can be implemented.

cpp


do
{
cout << "Enter Your base :";
cin >> base
}while(base<2||base>16);
cout<<"Enter The number for this base :"
cin.getline(number); //number is string [or character array.]
int i=0;
if(number[i] >= 'A' && number[i] <= 'F' )
num2dec = (number - 55) * base;
else
num2dec = atoi(number[i]) * base;
for(i=1;i<number.length(); )
{
if(number[i] >= 'A' && number[i] <= 'F' )
digit = number - 55;
else
digit = atoi(number[i]);
num2dec += digit;
i++;
if(i<number.length()-1)
{
num2dec = num2dec * base;
}
else
break;

}



I haven't tried to run this code. see if it works for you, and remove syntax errors if any. smile.gif

Thanx for this bit of code. I haven't tried it yet but i will use it eventually smile.gif. I forgot to mention our professor doesn't expect us to mess with Hex numbers, just values in the range 0-9. Using letters in our bases is extra credit. O and another thing biggrin.gif. I talked with a fellow peer of mine in class about getting the number of place values when using the left to right algorithm. All i needed to do was create a while loop to keep a count, then use that count in my for-loop when im getting the digits.

cpp
 

//Using WHILE-loop to get number of place values
while(pow((float)newBase, count) <= base10)
{
count++;
}

count -= 1;

divisor = pow((float)newBase, count);
dividend = base10;

//FOR-loop process to get digits for desired base conversion
for(i = count; i >= 0; i--)
{
digit[i] = dividend/divisor;
dividend = dividend % divisor;
divisor = pow((float)newBase, (i-1));
}

//Display Conversion
cout << base10 << " Base-10 is equal to ";

for(i = count; i >= 0; i--)
{
cout << digit[i] << "";
}

cout << " Base-" << newBase << endl << endl;

User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/21/08 02:52PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month