Problem >dizzy <me

My Code Used For

Change number from(base 2,base 3...To base 9) to give number in base 10=(decimal)

ex:
if u enter number 1352 base 8 give u number 746 in decimal.

My program is based one 2 line:
1-decimal=Quotient*b+array[j]; u may change this for decimal=array[j];
>> i used j to take first number in array that will be 1 in ex. <<
2-decimal=decimal*b+array[++j];

I have another problem when i enter number like 1352 to change it to decimal i have to enter 1 and make Enter than enter 3 and make Enter than..........
i try To change int array with char array and it work but give number wrong more than int.

So I hope one find solution to but number 1352 with out Partition.

I hope all answer me soon :)

This Is the Code:

#include <iostream>
using namespace std;

int main()
{
int array[100];
int j=0,i,b,Quotient=0;
int decimal,counter=0,bit;

cout<<"Enter Base Number :";
cin>>b;
cout<<"\n Enter Bits of number : ";
cin>>bit;
cout<<"\n Enter number : ";
for(i=0;i<100;i++)
{
cin>>array[i];
counter++;
if(counter==bit )
break;
}

decimal=Quotient*b+array[j];

{
decimal=decimal*b+array[++j];
}

cout<<"\nDecimal Number is :"<<decimal;
return 0;
}
Last edited on
closed account (D80DSL3A)
You can take the number whole (without partition) then strip each digit from it by using the % and / operators.

Using your example of num_b = 1352
1
2
3
4
5
6
7
8
9
10
int digit=0;

digit = num_b%10;// digit = 2
num_b = num_b/10;// now num_b = 135
digit = num_b%10;// digit = 5 this time
num_b = num_b/10;// num_b = 13
digit = num_b%10;// digit = 3
num_b = num_b/10;// num_b = 1
digit = num_b%10;// digit = 1
num_b = num_b/10;// num_b = 0 , which tells us we are done. 

You can find decimal from these digits as follows:
decimal = 2 + 5*b + 3*b^2 + 1*b^3

You can do it all in one while loop. Use an integer variable (say pow_b) to build up the powers of b as you are stripping digits from num_b.
Start with int pow_b = 1;

Then, each time you do this:
1
2
digit = num_b%10;
num_b = num_b/10;

add the term:
decimal += digit*pow_b;
and increase the power of b to prepare for the next loop iteration.
pow_b *= b;// increase the power


I hope these hints help.
EDIT: Corrected order of operations.
Last edited on
Thanks For u r help :)

and i had done New Program Can Change from any base To any base but i have
didn't add from base (11:16) .
i don't know how make program read number like this 2EA in 16 To change it .
i hope u try my code and see if u can add it and write here code.

#include <iostream>
using namespace std;

int main()
{
char array[100],Number[100];
int x,j,i,b,DecimalNumber=0;
int B,temp,c=1;
cout<<"Enter Base your Number u will Enter :";
cin>>b;
cout<<"\nEnter number : ";
cin>>x;
cout<<"\nEnter The Base u will Change To it : ";
cin>>B;
for(i=1;x!=0;i++)
{
array[i]=x%10;
x=x/10;
}
for(j=i-1 ;j>0;j--)
{
DecimalNumber=DecimalNumber*b+array[j];
}
if(B==10)
cout<<"\nDecimal Number is :"<<DecimalNumber<<endl;
else
{
while(DecimalNumber!=0)
{
temp=DecimalNumber % B;

if( temp < 10)
temp =temp + 48;
else
temp = temp + 55;

Number[c++]=temp;
DecimalNumber =DecimalNumber / B;
}

cout<<"The Number in Base "<<B<<": ";
for(int l=c-1 ;l>0;l--)
cout<<Number[l];

}

return 0;
}
Last edited on
Numbers that are greater than base 10 can contain letters, so you will need to work with strings. You would probably need to rewrite most of your code to do it.

I wrote this, but it doesn't check for any errors in input:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <string>
#include <cmath>

std::string ChangeBase(const std::string &num, const int &from, const int &to)
{
    std::string newnum;
    const std::string digits = "0123456789ABCDEF";
    int value = 0;

    for (unsigned int i = 0; i < num.length(); i++)
        value += (int)digits.find(num[i]) * pow(from, (int)num.length() - i - 1);

    if (value == 0)
        return "0";

    while (value > 0)
    {
        int digit = value % to;
        value /= to;
        newnum.insert(0, 1, digits[digit]);
    }

    return newnum;
}

int main()
{
    std::string number;
    int base1, base2;

    std::cout << "Enter a whole number: ";
    std::cin >> number;
    std::cout << "Enter the base to convert from: ";
    std::cin >> base1;
    std::cout << "Enter the base to convert to: ";
    std::cin >> base2;
    std::cout << "The converted number is " << ChangeBase(number, base1, base2) << ".";
    return 0;
}
Thanks :)

Can u explain These Codes For me :)

why u add const std::string digits = "0123456789ABCDEF";

num.length() What it mean to add length() ? u mean length read how many number user enter ?...

and explain :

1- value += (int)digits.find(num[i]) * pow(from, (int)num.length() - i - 1);

2- while (value > 0)
{
int digit = value % to;
value /= to;
newnum.insert(0, 1, digits[digit]);
}

and thanks again for u r idea :)
I find when i enter small letters don't give correct answer ??

in u r program.
Last edited on
Abdo2 wrote:
why u add const std::string digits = "0123456789ABCDEF";

That's just to make things easier to look up. digits[0] = '0', digits[1] = '1', ..., digits[14] = 'E', digits[15] = 'F'.

Abdo2 wrote:
num.length() What it mean to add length() ?

num.length() is the length of the string num. It makes sure that the loop will go over every digit in the number.

Abdo2 wrote:
value += (int)digits.find(num[i]) * pow(from, (int)num.length() - i - 1);

The find function will look through the digits string ("0123456789ABCDEF") for the position of the current digit in num. EG looking for '5' gives 5, looking for 'C' gives 12, etc.
Then the value of the position is multiplied by a power of the base you're converting from. If the number has 3 digits, then the first digit (on the left) is multiplied by from * from * from. The second by from * from, and the third by from. The results are added to value. Basically, the loop converts the number to base 10.

Abdo2 wrote:
while (value > 0)
{
int digit = value % to;
value /= to;
newnum.insert(0, 1, digits[digit]);
}

This loop converts a base 10 number to whatever base "to" is. You can find the reasoning in fun2code's post.

Abdo2 wrote:
I find when i enter small letters don't give correct answer ??

As I said, it doesn't check for input errors. The digit string "0123456789ABCDEF" does not contain any lower case letters.
How Can u fixed it in the Code ?
You can use the toupper function in the cctype library to capitalize the string before checking it.
Can u write the code i don't know about ctype library much :)
Take a look at the examples. It's easy enough.
Topic archived. No new replies allowed.