Decimal number from whatever base the user inputs c++

  • Context: C/C++ 
  • Thread starter Thread starter Jkohn
  • Start date Start date
  • Tags Tags
    Base C++
Click For Summary

Discussion Overview

The discussion revolves around converting numbers from various bases to decimal in C++. Participants are exploring algorithms and methods for base conversion, particularly focusing on converting a number represented as a string into an integer and then into a decimal format based on user input. The conversation includes both theoretical aspects and practical programming challenges.

Discussion Character

  • Exploratory
  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant expresses confusion about converting an integer from a string to decimal based on user-defined bases.
  • Another participant suggests that understanding the place value system in base b could help in developing a conversion algorithm.
  • A different participant mentions the need for a DIV/MOD algorithm to facilitate base conversion, explaining the process of modulus and division iteratively.
  • Some participants discuss the relevance of converting to base 2 first before converting to the user-defined base, while others question this approach.
  • One participant realizes their assignment specifically involves converting binary to decimal and expresses frustration with the programming aspect.
  • Another participant proposes a method involving raising the base number to track significant digits during conversion.

Areas of Agreement / Disagreement

Participants have not reached a consensus on the best method for base conversion, with multiple competing views and approaches being discussed. Some participants agree on the utility of the DIV/MOD algorithm, while others suggest alternative methods or express confusion about the process.

Contextual Notes

Participants mention various assumptions about the conversion process, including the complexity of directly converting strings and the need for iterative methods. There are also references to specific base cases and the challenges of implementing these algorithms in code.

Who May Find This Useful

This discussion may be useful for students learning about number systems, base conversions, and programming in C++, particularly those facing similar homework challenges in mathematics or computer science.

Jkohn
Messages
31
Reaction score
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
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.
 
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
 
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.
 
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
 
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.
 
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.
 
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
 
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!
 

Similar threads

Replies
1
Views
2K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
Replies
25
Views
4K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K