Decimal number from whatever base the user inputs c++

  • C/C++
  • Thread starter Jkohn
  • Start date
  • Tags
    Base C++
In summary, the conversation discusses a programming assignment that involves converting a number from a string to an integer, and then converting that integer to a decimal number in a user-specified base. The participants in the conversation share different approaches, including using a place value system and the DIV/MOD algorithm. The conversation ends with one participant expressing confusion and seeking help with implementing their chosen approach.
  • #1
Jkohn
31
0
Hey all, I am majoring in math and taking this c++ which I am having some trouble with. [Im into paper and pencils ]

So my assignment is to have a number saved in a string then convert it from a string to an integer (have that done) and finally convert that number to a decimal number from whatever base the user inputs.

I am lost with the int-base to decimal.

I was thinking using BASE 2 and convert it which ever base.
Integer--> convert it to base 2-->user inputs base-->converst BASE 2-->BASE N

Ex.
read 6
convert that to base 2-->110 [4+2+0=6]
user wants base 2-->print 110

Im assuming there is an algorithm for converting between bases that I don't know about.

Any ideas on how to do this and additional help would be really appreciated!

ty all!
 
Technology news on Phys.org
  • #2
If you are somewhat clever you may be able to discover a base converting algorithm on your own.

With the place value system with base b, we would treat a positive number as a sum some terms. Each term is an integer in the range 0 to b-1, multiplied by bi, where i is an integer designating place. For example:

d2*b2 + d1*b1 + d0*b0

Hint: The largest bi with a non-zero digit di will be less than the number. If bi is greater than than the number you want to represent, then the digit that multiplies bi must be zero, because if we added 1*b^i, the sum would already be too large.
 
  • #3
Thats easy I got that..my problem is that I don't know how to program that! What if its place 1 2 4..but for other 1 5 25

thats where I am confused
 
  • #4
Did you read my hint?

so let's say we have the number 333 in base 5

3 * 5^2 + 3 * 5^1 + 3 * 5^0

and we wanted to convert that to base 2.

How many times does 2^7 go into this number? How many times does 2^6 go into this number? What would we have left over?

You might attempt to convert some numbers between bases using pencil and paper, and then see if you are able to describe a method that will always work.
 
  • #5
the hint makes sense..but

so let's say we have the number 333 in base 5

3 * 5^2 + 3 * 5^1 + 3 * 5^0

and we wanted to convert that to base 2.

How many times does 2^7 go into this number? How many times does 2^6 go into this number? What would we have left over?

I don't see the relevance in that..wouldnt it be easier to just do base 2 and then convert to the base that user input
 
  • #6
Jkohn said:
Hey all, I am majoring in math and taking this c++ which I am having some trouble with. [Im into paper and pencils ]

So my assignment is to have a number saved in a string then convert it from a string to an integer (have that done) and finally convert that number to a decimal number from whatever base the user inputs.

I am lost with the int-base to decimal.

I was thinking using BASE 2 and convert it which ever base.
Integer--> convert it to base 2-->user inputs base-->converst BASE 2-->BASE N

Ex.
read 6
convert that to base 2-->110 [4+2+0=6]
user wants base 2-->print 110

Im assuming there is an algorithm for converting between bases that I don't know about.

Any ideas on how to do this and additional help would be really appreciated!

ty all!

Hey Jkohn and welcome to the forums.

For anything involving base you need to use the DIV/MOD algorithm.

Essentially you run a loop where you get the modulus in some base, subtract that, and then divide and repeat the process until the whole number is gone. As an example:

Lets say you have 123. First you get modulus base 10 which is 3. Store that as A = 3. Subtract 3 from 123 = 120. Divide by 10 = 12. Again do a modulus 10 which gives B = 2. Subtract 2 from 12 = 10. Divide by 10 = 1. Find the modulus base 10 which gives C = 1. Subtract 1 which gives 0. Since we have zero, we stop.

Then we reconstruct the number by going backwards, by printing "CBA" as our number base 10.

If you want to do this for any base (even ones that are variable, i.e. you can have variable bases for different digits), you use the same idea.
 
  • #7
Jkohn said:
Im assuming there is an algorithm for converting between bases that I don't know about.
Your approach converting the input string to an integer first, then converting the integer to an output string is the most straight forwad solution. Trying to directly convert one string to another would be complex, you'd end up having to implement some string based math operators.

As mentioned above, the div / mod approach will get you the output string, but in reverse order. Also as mentioned above, you could raise the base number (multiply it by itself), also as an integer until it exceeded your input integer value, keeping track of the iteration (the power you raised the base to), then reduced the power by one (divide the powered integer by the base), then use div / mod like algorithm to get the most significan digits of the output string first.
 
  • #8
I just realized the assignment is BINARY --> DECIMAL
So:
input A: 110
input B: BASE 2
print: 6

im so damn lost! computers so ehh
 
  • #9
what I am trying to do is read for each case up to base 10..and then using the amount of characters
res = x^ammnt of char-1

do that up to 0

i just have no idea how to code that!
 

Related to Decimal number from whatever base the user inputs c++

1. What is a decimal number?

A decimal number is a number expressed in the base-10 number system, which uses 10 digits (0-9) to represent all possible values. It is the most commonly used number system in everyday life.

2. What is a base in relation to decimal numbers?

A base refers to the number of digits used in a number system. In the decimal system, the base is 10 because there are 10 digits (0-9) used to represent numbers.

3. How do I convert a decimal number to a different base in C++?

To convert a decimal number to a different base in C++, you can use the built-in functions such as itoa() or stringstream to convert the number to a string and then use stoi() to convert it back to a different base.

4. Can decimal numbers be written in a different base?

Yes, decimal numbers can be written in a different base by converting them to the desired base using mathematical operations. For example, to convert a decimal number to binary, you can use the division method or the repeated subtraction method.

5. Why is it important to understand decimal numbers and their relation to different bases?

Understanding decimal numbers and their relation to different bases is important as it helps in various mathematical and scientific calculations. It also helps in understanding the concept of number systems and how different cultures and civilizations have developed their own numbering systems based on their needs and resources.

Similar threads

  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
Replies
25
Views
3K
  • Programming and Computer Science
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
929
  • Programming and Computer Science
Replies
4
Views
2K
Replies
17
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
22
Views
4K
  • Programming and Computer Science
Replies
25
Views
2K
Back
Top