+ Reply to Thread
Results 1 to 8 of 8
  1. #1
    Member
    Join Date
    Oct 2000
    Location
    Gainesville, FL, USA
    Posts
    312

    Beginner C++ problem

    I need a program that prints the "hundreds" digit in a series of integers entered by the user. For instance, if the user enters 2356 and 764 and 894, then the program will display 3, 7, and 8.

  2. #2
    Member
    Join Date
    Oct 2000
    Location
    Gainesville, FL, USA
    Posts
    312
    I do not want code, just an idea, concept or explanation of how to do it. I want to put the code together myself. I just don't know who to determine the correct digit.

  3. #3
    Member strangerstill's Avatar
    Join Date
    Sep 2001
    Location
    Oxford
    Posts
    203
    ((int) n / 100) % 10
    or
    ((int) n % 1000) / 100

    Your choice: first probably faster.
    %: modulus operator ('remainder on division by')

  4. #4
    Member
    Join Date
    Oct 2000
    Location
    Gainesville, FL, USA
    Posts
    312
    I see how these expressions would work with an integer of four or more digits, and I greatly appreciate that. But how about three digit integers? I'm not supposed to use if statements yet. And thanks again.

  5. #5
    Member strangerstill's Avatar
    Join Date
    Sep 2001
    Location
    Oxford
    Posts
    203
    OK. Let's try 386:

    (int) 386 / 100 = 3
    3 % 10 = 3

    or

    (int) 386 % 1000 = 386
    386 / 100 = 3

    See? It works.

    Note that the (int) cast applies to n, not to (n/100)
    This is in case n is a single; of course I'd expect it to be an int already so that's a bit unnecessary: in fact

    (i / 100) % 10

    will work just as well; because both i and 100 are integers, C++ will use integer division and return an integer.

    Oh, and my cryptic reference to speed: thinking about it, both will be equal in speed (one integer division, one integer modulus), but the first looks tidier. On the other hand, if n is of type long, then the following code:

    (int)(n % 1000) / 100

    will be fastest as it involves one long modulus and an integer division as opposed to

    (n / 100) % 10

    which would involve a long modulus and a long division.

    Hope that makes sense.

  6. #6
    Member
    Join Date
    Oct 2000
    Location
    Gainesville, FL, USA
    Posts
    312
    Of course you're right - I was confusing the quotient with the modulus thinking the latter would be 0 given 386%1000. When in doubt, read the book.

    Thanks once more.

    Originally posted by strangerstill
    OK. Let's try 386:

    (int) 386 / 100 = 3
    3 % 10 = 3

    or

    (int) 386 % 1000 = 386
    386 / 100 = 3

    See? It works.

    Note that the (int) cast applies to n, not to (n/100)
    This is in case n is a single; of course I'd expect it to be an int already so that's a bit unnecessary: in fact

    (i / 100) % 10

    will work just as well; because both i and 100 are integers, C++ will use integer division and return an integer.

    Oh, and my cryptic reference to speed: thinking about it, both will be equal in speed (one integer division, one integer modulus), but the first looks tidier. On the other hand, if n is of type long, then the following code:

    (int)(n % 1000) / 100

    will be fastest as it involves one long modulus and an integer division as opposed to

    (n / 100) % 10

    which would involve a long modulus and a long division.

    Hope that makes sense.

  7. #7
    Member strangerstill's Avatar
    Join Date
    Sep 2001
    Location
    Oxford
    Posts
    203
    Happy to be of help. Hope you get further with C++ than I did.

  8. #8
    Junior Member
    Join Date
    Oct 2006
    Posts
    2
    is there source code for this program?? if theres, would you please post it. thank you

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts







New Security Features Planned for Firefox 4
Another Laptop Theft Exposes 21K Patients' Data
Oracle Hits to Road to Pitch Data Center Plans
Microsoft Preps Array of Windows Patches
Microsoft Nears IE9 Beta With Final Preview
Simplified Analytics Improve CRM, BI Tools
Android Passes RIM as Top Mobile OS in 2Q
VMware Updates Hyperic System Management
File Monitoring Key to Enterprise Security
LinkedIn Snaps Up SaaS Player mSpoke