Results 1 to 7 of 7

Thread: Logic Required

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2002
    Location
    india
    Posts
    24

    Unhappy Logic Required

    hi!
    actually,i want to write a program in C.the specifications are as below:-
    1. Input should be a two digit number(say ab)
    2. If a>b then Output -1
    3. If a<b then Output +1
    4. If a==b then Output 0
    5. But we have not to use divide(/) or modulo(%) operator.
    6. I think,by using bitwise operators,we may get our goal.But
    HOW?
    Please give logic behind it??????????????

    regards

    Rohit..

  2. #2
    Frenzied Member axion_sa's Avatar
    Join Date
    Jan 2002
    Location
    Joburg, RSA
    Posts
    1,724
    why get so complicated? it's a very small program and any
    increase in speed (if any) will be insignificant.

    just use normal equality expressions (==, >, <)

  3. #3
    Junior Member
    Join Date
    Feb 2002
    Location
    UK
    Posts
    23
    You can mutiply adding up shifts e.g.

    x * 10 = x<<1 + x<<3

    <<1 (*2)
    <<2 (*4)
    <<3 (*8)
    <<4 (*16)

    etc.

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    The problem is seperating A and B. The ususal way is using / and % with 10, but this is forbidden.

    You could use hex digits and claim that they are digits too. Then it is easy:
    a = ab >> 4;
    b = ab & 0x0f;

    Decimal digits are far harder.
    it takes a lot of bitmask and shifting (I can't think of a way), or you can simulate the operation a cpu does when dividing: substract and count
    Code:
    // do a division and modulo at the same time
    int ab = GetInput();
    int temp = ab;
    int cnt = 0;
    int result = -2; // error by default
    while(temp >= 10)
    {
      temp -= 10;
      ++cnt;
    }
    // now cnt == ab / 10, temp == ab % 10
    if(cnt == ab) result = 0;
    if(cnt < ab) result = -1;
    if(cnt > ab) result = 1;
    return result;
    That's a dirty trick however, and may be forbidden.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Cheating now hehe, Msvc7 came up with this solution:
    PHP Code:
      00013    b8 cd cc cc cc     mov     eax, -858993459        cccccccdH
      
    00018    f7 64 24 0c     mul     DWORD PTR _x$[esp+16]
      
    0001c    c1 ea 03     shr     edx
    so in C++ that would be:
    PHP Code:
        unsigned int x,y;
        
    cin>>x;
        
    x-=10*(y=(x*0xcccd)>>19);
        if(
    == xresult 0;
        if(
    xresult = -1;
        if(
    xresult 1;
        
    cout<<result
    Last edited by kedaman; Feb 19th, 2002 at 01:36 PM.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    so the workaround is the same as:
    x / 10 == x * (m/10) / m
    and we are searching for an m that is a power of 2 and can be divided by 10 evenly. There is no such number, but at 0xcccd, which is 52429, the difference to 2^19 (= 524288) is so small compared to the magnitude of the numbers, that the relatively unprecise shift-right division will come up with the right result.

    MSVC 7 is really impressive. That thing must be possible with many numbers.
    There should be some optimization like "optimize calls to pow, sqrt, sin etc. with constant parameters away". This would preserve code readability without the necessity of compile time template calculations, yet cause no impact on runtime.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    There should be some optimization like "optimize calls to pow, sqrt, sin etc. with constant parameters away". This would preserve code readability without the necessity of compile time template calculations, yet cause no impact on runtime.
    Might be, I haven't tried out yet, that would quite beat the purpose of templates in a lot of cases indeed in that case it could threaten BORK but it has a lot of other intuitive features, which a C++ programmer can't dream of
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

Posting Permissions

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



Click Here to Expand Forum to Full Width