Help with outputting a list of squared numbers

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

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,422
    Quote 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).
    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

  2. #17
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #18
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,294
    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"

  4. #19
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by anon View Post
    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
    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.

  5. #20
    Registered User
    Join Date
    Nov 2008
    Posts
    110
    Quote Originally Posted by iMalc View Post
    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. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,422
    Quote 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();
    Quote Originally Posted by dnguyen1022
    That loop just simply slipped my mind.
    As in you do not understand matsp's code?

    Quote Originally Posted by dnguyen1022
    And that log thing is almost incomprehensible to me lol
    As in you have not learnt about logarithms?
    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

  7. #22
    Registered User
    Join Date
    Nov 2008
    Posts
    110
    I understood the loop, but not the log stuff...Thanks for your help though, the columns even out nicely.

  8. #23
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    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.
    Last edited by CodeMonkey; 01-16-2009 at 02:10 PM. Reason: mistake
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  9. #24
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  10. #25
    Registered User
    Join Date
    Nov 2008
    Posts
    110
    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. #26
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by dnguyen1022 View Post
    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
    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.

  12. #27
    Registered User
    Join Date
    Nov 2008
    Posts
    110
    Would this set of numbers be for the compiler's reference or simply for me(us) to see?

  13. #28
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by dnguyen1022 View Post
    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.

  14. #29
    Registered User
    Join Date
    Nov 2008
    Posts
    110
    ohhh got it. Thanks.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. Outputting a list of squared Doubles
    By dnguyen1022 in forum C++ Programming
    Replies: 13
    Last Post: 01-19-2009, 12:24 PM
  3. Recursion Revisited again, and again!
    By clegs in forum C++ Programming
    Replies: 93
    Last Post: 12-08-2007, 07:02 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 04:20 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21