why do i always get true in the loop?

This is a discussion on why do i always get true in the loop? within the C++ Programming forums, part of the General Programming Boards category; Code: for (i=1; i <= numDigits/2; ++i) { if ((num &#37; 10^i ) / 10^(i-1) != (num / 10^(numDigits - ...

  1. #16
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Code:
    for (i=1; i <= numDigits/2; ++i) {
       if ((num &#37; 10^i ) / 10^(i-1)  !=  (num / 10^(numDigits - i)) % 10^(i-1)) {
          palindrome = 0;
          break;
       }
    }
    This is the code I suggested. It is short. Have no idea if it works. And you still need to count the digits. This can be done easily though:
    Code:
    while (num !=0) {
       num / 10;
       ++numDigits;
    }
    Now reversing a number:
    Code:
    temp = num;
    for (i=0; i <= numDigits; ++i) {
       rev = 10^(numDigits-i) * (temp % 10);
       temp /= 10;
    }
    if (rev != num)
       palindrome = 0;
    NOTE that i haven't really paid attention to the bounds of the loop, the parenthesis and generally the details. Put you understand the idea. The second code is more readable, but a big longer. The first seems to be more slow in the worst case scenario, but it can break more quickly so it should give a better average perfomance (details, details I know)

    EDIT: If you want to be even more strict, you could separate the number in two parts. If it is odd ignore the middle digit. Then reverse one part and check if the two parts are now equal. If they are it is a palindrome and you have an algorithm that runs in half speed from the other
    Last edited by C_ntua; 09-12-2008 at 02:26 AM.

  2. #17
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    Quote Originally Posted by C_ntua View Post
    Code:
    for (i=1; i <= numDigits/2; ++i) {
       if ((num &#37; 10^i ) / 10^(i-1)  !=  (num / 10^(numDigits - i)) % 10^(i-1)) {
          palindrome = 0;
          break;
       }
    }
    This is the code I suggested. It is short. Have no idea if it works. And you still need to count the digits. This can be done easily though:
    Code:
    while (num !=0) {
       num / 10;
       ++numDigits;
    }
    Now reversing a number:
    Code:
    temp = num;
    for (i=0; i <= numDigits; ++i) {
       rev = 10^(numDigits-i) * (temp % 10);
       temp /= 10;
    }
    if (rev != num)
       palindrome = 0;
    NOTE that i haven't really paid attention to the bounds of the loop, the parenthesis and generally the details. Put you understand the idea. The second code is more readable, but a big longer. The first seems to be more slow in the worst case scenario, but it can break more quickly so it should give a better average perfomance (details, details I know)

    EDIT: If you want to be even more strict, you could separate the number in two parts. If it is odd ignore the middle digit. Then reverse one part and check if the two parts are now equal. If they are it is a palindrome and you have an algorithm that runs in half speed from the other
    tanx alot. i will try it .by the way how could you manage to think like this!?

  3. #18
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Just to add to C_ntua's suggestion - there is no need to calculate the number of digits before you reverse the number. Just replace the for-loop with the while-condition of the calculate number of digits loop, and use multiply by 10 to form the reversed number, you just picked the lowest digit off, so you need to multiply it "reamaining number of digits" by 10. I'm not going to write the code out for you, since writing the code is how you learn how to write code. Copy'n'paste is something ANYONE can do.

    Also, 10 ^ x in C does not do 10 to the power of x - it caculates 10 XOR x, which is a somewhat "random" sequence of numbers. [And in this case, we only need about 10 or 11 numbers to cover the whole range of 32-bit integers (for 64-bit integers it would be about 18 numbers), so providing an array of 10 to the power of X as integers in table form would be quite feasible]

    As to "how do you figure this out" - it is a combination of basic understanding of how to deal with "number to digits" and general "how you solve problems" (as in, what method can you use to calculate a number backwards).


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

  4. #19
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    many tanx ... specially for your comprehensive and detailed explanation of what took place so far ...

    many thanks .. by the way , is it normal to think more than 5 hours for such a simple assignment (finding the best way of implementing it ? or its just a waist of time and energy , and i should concentrate on more complex issues !

    tanx again ...
    Last edited by Masterx; 09-12-2008 at 01:00 PM.

  5. #20
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    What is normal anyway? Besides, percentage-wise more time is spent on small bugs that may only exist because of a single typo than on larger concepts. For example: x == 6; Nowadays compilers will mention these things, but those used to take ages to notice. In any event, you are obviously new, and as you get a deeper understanding of what you are doing, you will spend less time thinking about "simple things."

  6. #21
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by Masterx View Post
    many tanx ... specially for your comprehensive and detailed explanation of what took place so far ...

    many thanks .. by the way , is it normal to think more than 5 hours for such a simple assignment (finding the best way of implementing it ? or its just a waist of time and energy , and i should concentrate on more complex issues !

    tanx again ...
    Depends on what you want to do. Many ppl post here about bugs they have. They get from a simple solution to the most detailed things you can think of in C. My point is that it is good to learn, you don't actually have to sit and make perfect the program if don't want to or more importantly don't need to

  7. #22
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    many tanx to all of you and your pieces of advice

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

Similar Threads

  1. Creating a map
    By DanFraser in forum C# Programming
    Replies: 7
    Last Post: 01-23-2009, 06:23 AM
  2. Loop seg error
    By Zishaan in forum Game Programming
    Replies: 2
    Last Post: 03-28-2007, 02:27 PM
  3. Forced moves trouble!!
    By Zishaan in forum Game Programming
    Replies: 0
    Last Post: 03-27-2007, 07:57 PM
  4. I need help as soon as possible.
    By hyrule in forum C++ Programming
    Replies: 7
    Last Post: 11-09-2005, 05:49 PM

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