Smallest of 3 numbers without using Comparator operator.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Nethra Nayak
    New Member
    • Nov 2008
    • 1

    #1

    Smallest of 3 numbers without using Comparator operator.

    How to Write a C program to find the smallest of three integers, without using any of the comparision operators.?
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by Nethra Nayak
    How to Write a C program to find the smallest of three integers, without using any of the comparision operators.?
    Why do you want to do that? Is it a riddle from your textbook? If so, this is not
    a do-my-homework service; first give it a try yourself.

    kind regards,

    Jos (moderator)

    hint: check whether or not the sign bit of (a-b) is set.

    Comment

    • whodgson
      Contributor
      • Jan 2007
      • 542

      #3
      say 3 nums are n1,n2 & n3
      say n1=3,n2=15 & n3=9
      ni*n2==45...... ..........(1)
      n1*n3==27...... .........(2)
      n2*n3==135..... ........(3)
      the smallest num is common to expression 1 and 2 which is n1 or 3
      how rediculous!

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        Yes, but to determine which result was smallest, you'd have to use...a comparison operator.

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          As I wrote before: use the sign bit of an expression. Something like this:

          Code:
          #define ALL ((unsigned int)~0)
          #define SGN (ALL^(ALL>>1))
          #define LESS(X,Y) (((X)-(Y))&SGN)
          LESS(x, y) will be true (not 0) when x < y. This trickery-dickery only works for
          ints.

          kind regards,

          Jos

          Comment

          • whodgson
            Contributor
            • Jan 2007
            • 542

            #6
            Gannon11 says:
            Yes, but to determine which result was smallest, you'd have to use...a comparison operator.

            Why couldn't the 3 products be printed out as shown and then
            cout<<"Which int: ";
            cin>>numsmall;
            user chooses n1 or n2 or n3 on the basis of which n is common to two smallest products
            cout<<"The smallest int is: "<<numsmall ;
            QED but agreed <<<<<< than elegant

            Comment

            • Alan Lopez

              #7
              Let your three integers be:

              int num1, num2, num3;

              //starts computing smallest number

              if ( num1 < num2 ) {
              if ( num1 < num3 ) {
              printf( "Smallest if is %d\n", num1 );
              }
              }
              if ( num2 < num1 ) {
              if ( num2 < num3 ) {
              printf( "Smallest if is %d\n", num2 );
              }
              }
              if ( num3 < num1 ) {
              if ( num3 < num2 ) {
              printf( "Smallest if is %d\n", num3 );
              }
              }

              Comment

              Working...