Assuming that the number is a positive integer, you can use: static_cast<size_t>(log10(number)) + 1. If it is non-positive integer, then you have to do a little more work (e.g., use abs() for negative numbers).Originally Posted by dnguyen1022
This is a discussion on Help with outputting a list of squared numbers within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by dnguyen1022 By the way is there a function that determines the length of a number as in ...
Assuming that the number is a positive integer, you can use: static_cast<size_t>(log10(number)) + 1. If it is non-positive integer, then you have to do a little more work (e.g., use abs() for negative numbers).Originally Posted by dnguyen1022
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way
You can use log10 to determine the "length" of an unsigned number.
(Converting to string and taking the length of that might also help.)Code:#include <iostream> #include <cmath> int main() { unsigned arr[] = { 193887893, 123, 3892, 7, 382798, 1111 }; for (unsigned i = 0; i != sizeof(arr)/sizeof(arr[0]); ++i) { std::cout << arr[i] << ' ' << static_cast<unsigned>(std::log10(arr[i])) + 1 << '\n'; } }
To line things up, you should used setw with the length of the maximum number in the column.
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
So why are you building up a vector of the squares?
Why not just output i*i as you go?
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
It is definitely worth considering a "convert to string" or similar approach, as the log method is quite slow - particularly on embedded systems, floating point calculations tend to do bad things.
Or simply a loop:
Code:int numdigits(int x) { int count = 0; if (x < 0) { count++; // If you want to count the minus, e.g. for space in a string or some such. x = -x; } if (x == 0) return count + 1; while(x) { x /= 10; count++; } return count; }
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Because the exercise's purpose from the book was to show me how to use header and source files....
How do you convert an int to a string? I searched it up, and all I got was a string to a number and not the other way around. That loop just simply slipped my mind. And that log thing is almost incomprehensible to me lol
One way is:Originally Posted by dnguyen1022
Code:std::stringstream ss; ss << number; // Now ss.str() gives the string, so we can write: size_t width = ss.str().length();As in you do not understand matsp's code?Originally Posted by dnguyen1022
As in you have not learnt about logarithms?Originally Posted by dnguyen1022
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way
I understood the loop, but not the log stuff...Thanks for your help though, the columns even out nicely.
How many digits are in that? Well, when do we get a new digit? When we multiply by 10. Using ^ to mean "to the power of,"Code:1177263
So log10(n) answers the question "ten to what power is n?" We know that 10^d has d+1 digits (think about it), and we won't get d+2 digits unless we have 10^(d+1). So if we get a number between d and d+1, but less than d+1, we know we'll have a number with d+1 digits. So anon took the log10 of your number, and then cast it into an unsigned integer, which gets rid of everything but the whole number part of it, which tells us how many digits. Got it?Code:10^log10(n) = n
So 1177263 has seven digits.Code:log10(1177263) = 6.070873495 Just whole number part = 6 Plus one = 7
Last edited by CodeMonkey; 01-16-2009 at 03:10 PM. Reason: mistake
"If you tell the truth, you don't have to remember anything"
-Mark Twain
Logarithmic functions return the value a for x, from the equation x = b ** a (wher ** means "to the power of"). b is a known base, e.g. log2(8), b = 2, a = 3. Log10(100) b = 10, a = 2. Log10(16000) = 4.2.
There is something called "natural" logarithms, which use b = 2.717...
Logarithms are good for multiplication of large numbers, because x * y = b **(log(x) + log(y)) - so in the old days, when a large multiplication needed to be done, a log-table would be used to find the log(x) and log(y), then these numbers added (which is much simpler than multiplying large numbers), and then the log-table looked up in reverse to find the answer.
Nowadays, we can mostly just get on with multiplying numbers directly, thanks to calculators and computers.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Thank you CodeMonkey and matsp for the log review. The coding makes sense now, but
What is that part for?Code:unsigned arr[] = { 193887893, 123, 3892, 7, 382798, 1111 };
Would this set of numbers be for the compiler's reference or simply for me(us) to see?
Just for us to see how it works - e.g. 193887893 will print out <fx counting.../> 9 digits.
Whenever you show how something works, or want to perform some sort of calculation, you need some "sample data", something that represents your data for the purpose of testing. In this case, some varying length numbers to show that the output is correct for those values.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
ohhh got it. Thanks.