How to count in binary in base 10 [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I would like an integer in c++ to count up in binary like:
1, 10, 11, 100, 101, 110, 111
But i want to values to be in base 10 like:
one, ten, eleven, one hundred, one hundred and one, etc.
Basically count up in decimal but look like base 10.
Is there a algorithm to do this.
-Joseph

This is how you should proceed:
use a string for binary values to display it
you should implement a cycle which will end when your number reaches 0 and should write the result of currentValue % 2 to the start of your string then divide your number with 2
you should implement a function which will return the humanely readable number text, you might want to take inspiration from here, but a clue is that the digit number % 3 is relevant. If it is 0, then you will need to write the digit if the digit next to it to the left does not happen to be one, so values between 11-19 are an exception here and don't forget thousand, million, billion, if the modulo is 1, then you will have a special text, like eleven, ... nineteen, twenty, ..., ninety and if modulo is 2, then the text will be the digit + "hundred"
You will basically have a number and will be able to get the binary from it and the textual representation as well.
String is not necessary for the algorithm, one can cout a std::bitset as well.

Related

Sum of LCM range from 1 to 10^9 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
So, first programming course, first nongraded assignment:
In C++, for the range of numbers from 1 to 1 billion, find the sum of the numbers that are divisible (without remainder) by all 1 through 99. How do I go about this? How do I even find the lowest number divisible by 1:99?
edit: this isn't hw to be turned in, just something to think about. I would try some type of vectorizing in matlab, but this is my first day trying c++ so I really have no idea, I just learned how to initialize a variable.
// In pseudocode a very basic algorithm:
main
for i: 1 to 1000000000
if (TestValue(i))
Output(i)
TestValue(i)
for j: 1 to 99
if j does not divide i evenly
return false
return true
Of course, this won't be very performant. You might notice that if a number is evenly divisible by all numbers between 1 and 99, then it must be divisible by the set of prime factors in 1..99. For instance, in 1..19 the prime factors are: 2, 2, 2, 2, 3, 3, 5, 7, 11, 13, 17, 19. If something is evenly divisible by all numbers 1..19 then it must be evenly divisible by 2*2*2*2*3*3*5*7*11*13*17*19 = 232792560. To find all the numbers between 1 and 1000000000 that are evenly divisible by 1..19, I would find all the numbers between 1 and 1000000000 that are evenly divisible by 232792560 instead.

Why my double is shown as scientific? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
How can I add a scientific and a float number ? I have something like
var1 0.99999899 var2 3.5008552e-05 sum 3.5008552e-05
but what I dont understand is why in the first place var2 is shown as scientific while in the first place I declared
double var1, var2;
so, actually their sum is just var2...
thanks
a
The way a floating-point value is displayed is down to the mechanism by which you display it. It is not a property of the value itself, nor is it in any way stored within the variable:
A number is a number is a number. What you call "scientific" is not a class of numbers. It's a class of representations of numbers. The same way that "twelve" and "12" and "XII" and "a dozen" and "IIIIIIIIIIII" all represent the same number. This "scientific" thing only exists when you decide to represent the number in some specific way (i.e. when you output it). Calculations don't "turn numbers into scientific" the same way that saying that "2 * 6 is twelve" doesn't turn numbers into English words. The variables always store the numbers not the representations. — R. Martinho Fernandes
Your display mechanism — std::cout? — is choosing the best way to output the value. You can override this with IO manipulators such as std::fixed, though it's pretty fiddly sometimes to get it just how you want it, due to library limitations.
If you have <stdio.h> available then you may use int printf ( const char * format, ... ); the format specifier would look something like this: printf("%.5f", your_double) where 5 is the number of digits you want after the decimal point, the default is 6.
(printf docs)

Method to find number of digits after converting from a different base number

The text in quotes gives a bit of background on my program in case it's needed to understand my issue, you might be able to fully understand with the stuff at the end unquoted if you don't feel like reading it.
I'm working on the common project of sorting in C++, and I am
currently doing radix sort. I have it as a function, taking in a
vector of strings, an integer holding the max number of digits, and an
integer with the radix/base of the numbers: (numbers, maxDigits, radix)
Since the program takes in numbers of different base and as a string,
I'm using stoi to convert them to a base 10 integer to make the
process easier to generalize. Here's a quick summary of the algorithm:
create 10 queues to hold values 0 to 9
iterate through each digit (maxDigit times)
iterate through each number in the vector (here it converts to a base 10)
put them into the queue based on the current digit it's looking at
pull the numbers out of the queues from beginning to end back into the vector
As for the problem I'm trying to wrap my head around, I want to change the maxDigit value (with whatever radix the user inputs) to a maxDigit value after it is converted to base 10. In other words, say the user used the code
radixSort(myVector, 8, 2)
to sort a vector of numbers with the max number of digits 8 and a radix of 2. Since I convert the radix of the number to 10, I'm trying to find an algorithm to also change the maxDigits, if that makes sense.
I've tried thinking about this so much, trying to figure out a simple way through trial and error. If I could get some tips or help in the right direction that would be a great help.
If something is in radix 2 and max digits 8, then its largest value is all ones. And 11111111 = 255, which is (2^8 - 1).
The maximum digits in base 10 will be whatever is needed to represent that largest value. Here we see that to be 3. Which is the base 10 logarithm of 255 (2.40654018043), rounded up to 3.
So basically just round up log10 (radix^maxdigits - 1) to the nearest whole number.

Print nth Fibonacci number [upto 1000 digits] [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How can one calculate nth Fibonacci number in C/C++? The Fibonacci number can contain upto 1000 digits. I can generate numbers upto 2^64 (unsigned long long). Since that is the limit of numbers, so I am guessing there has to be some other way of doing it - which I do not know of.
EDIT:
Also, it has to be done without using any external libraries.
I'll give a few hints since you haven't indicated that you've started yet.
A thousand digits is a lot. More than any built-in numeric type in C or C++ can hold.
One way to get around it is to use an arbitrary-precision math library. This will have constructs that will give you basically as many digits as you want in your numbers.
Another way is to roll your own cache-and-carry:
unsigned short int term1[1024]; // 1024 digits from 0-9
unsigned short int term2[1024]; // and another
unsigned short int sum[1024]; // the sum
addBigNumbers(&term1, &term2, &sum); // exercise for the reader
I'd expect the algorithm for addBigNumbers to go something like this:
Start at the ones digit (index 0)
Add term1[0] and term2[0]
Replace sum[0] with the right digit of term1[0] + term2[0] (which is ... ?)
Keep track of the carry (how?) and use it in the next iteration (how?)
Repeat for each digit
Now, since you're calculating a Fibonacci sequence, you'll be able to re-use these big numbers to get to the next term in the sequence. You might find that it's faster not to copy them around but to just change which ones are the terms and which one is the sum on your repeated calls to addBigNumbers.
You could try using GMP for arbitrarily large integers.

double variable type output is always 0 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
For an assignment I have to create a "rock paper scissor lizard spock" game where I have to output the percentage of the winnings at the end. In the program I calculate the number of games player A and B have won including tie games, the total games played, and the win percentage of each player. All the variables are int except for the percentage which is a double variable type. When I calculate the percentage (games won / total games) I get a 0 as a result. Any ideas why? Sorry I could not provide any code, this is an assignment and I'm not allowed to post it anywhere online.
Without seeing code I can't be 100% sure, but my guess is that you're dividing values like this:
int numWins = /* ... */
int numLosses = /* ... */
double ratio = numWins / numLosses; // <-- Error!
In C and C++, that last line is a logic error. Although you're storing the result as a double, because you're dividing two ints the division is done as integer division and then stored in a double. C and C++ don't look at the type of the variable you're assigning to when deciding what division to use; the ratio of two ints is always computed using integer division.
To fix this, you can add a cast:
double ratio = (double)numWins / numLosses;
This casts numWins to a double, which ensures that the division is done as floating-point division because at least one of the operands is a double. That should fix your issue.
Hope this helps!
In most programming languages, division between two integers is an integer. Thus, 5 / 10 will give 0, as will 1 / 10 and 9 / 10. This takes place before assignment to the result variable, so the fact that the result variable is a double is irrelevant. You need to turn either the divisor or the dividend into a double for the floating point calculation to kick in.