# Thread: Help with outputting a list of squared numbers

1. Originally Posted by dnguyen1022
By the way is there a function that determines the length of a number as in its width of digits.
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).

2. You can use log10 to determine the "length" of an unsigned number.

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';
}
}```
(Converting to string and taking the length of that might also help.)

To line things up, you should used setw with the length of the maximum number in the column.

3. So why are you building up a vector of the squares?
Why not just output i*i as you go?

4. Originally Posted by anon
You can use log10 to determine the "length" of an unsigned number.

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';
}
}```
(Converting to string and taking the length of that might also help.)

To line things up, you should used setw with the length of the maximum number in the column.
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

5. Originally Posted by iMalc
So why are you building up a vector of the squares?
Why not just output i*i as you go?
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

6. Originally Posted by dnguyen1022
How do you convert an int to a string?
One way is:
Code:
```std::stringstream ss;
ss << number;
// Now ss.str() gives the string, so we can write:
size_t width = ss.str().length();```
Originally Posted by dnguyen1022
That loop just simply slipped my mind.
As in you do not understand matsp's code?

Originally Posted by dnguyen1022
And that log thing is almost incomprehensible to me lol
As in you have not learnt about logarithms?

7. I understood the loop, but not the log stuff...Thanks for your help though, the columns even out nicely.

8. Code:
`1177263`
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:
`10^log10(n) = n`
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:
```log10(1177263) = 6.070873495
Just whole number part = 6
Plus one = 7```
So 1177263 has seven digits.

9. 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

10. Thank you CodeMonkey and matsp for the log review. The coding makes sense now, but

Code:
`unsigned arr[] = { 193887893, 123, 3892, 7, 382798, 1111 };`
What is that part for?

11. Originally Posted by dnguyen1022
Thank you CodeMonkey and matsp for the log review. The coding makes sense now, but

Code:
`unsigned arr[] = { 193887893, 123, 3892, 7, 382798, 1111 };`
What is that part for?
That's just a set of numbers to show how the "number of digits" work.

--
Mats

12. Would this set of numbers be for the compiler's reference or simply for me(us) to see?

13. Originally Posted by dnguyen1022
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

14. ohhh got it. Thanks.