Comparing two numbers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • purifier

    #1

    Comparing two numbers

    The problem is to write a program in 'C' to find the greatest of 2 given
    numbers... Easy? huh
    here's the catch
    do not use 'if' or any conditional statements
    if u want it to be a little more tougher you can use the if but this time
    no relational operators or any of the predefined functions....

    Can someone please help me solve the problem....

  • Joona I Palaste

    #2
    Re: Comparing two numbers

    purifier <cnbctv18@gmail .com> scribbled the following:[color=blue]
    > The problem is to write a program in 'C' to find the greatest of 2 given
    > numbers... Easy? huh
    > here's the catch
    > do not use 'if' or any conditional statements
    > if u want it to be a little more tougher you can use the if but this time
    > no relational operators or any of the predefined functions....[/color]
    [color=blue]
    > Can someone please help me solve the problem....[/color]

    How about (1) doing your homework and (2) speaking English instead of
    d00dsp33k?

    --
    /-- Joona Palaste (palaste@cc.hel sinki.fi) ------------- Finland --------\
    \-------------------------------------------------------- rules! --------/
    "All that flower power is no match for my glower power!"
    - Montgomery Burns

    Comment

    • Mike Wahler

      #3
      Re: Comparing two numbers


      "purifier" <cnbctv18@gmail .com> wrote in message
      news:19793998f8 7506ee09a36174c 4a83f22@localho st.talkaboutpro gramming.com...[color=blue]
      > The problem is to write a program in 'C' to find the greatest of 2 given
      > numbers... Easy?[/color]

      Yup.
      [color=blue]
      > huh
      > here's the catch
      > do not use 'if' or any conditional statements[/color]

      Still easy.
      [color=blue]
      > if u want it to be a little more tougher you can use the if but this time
      > no relational operators or any of the predefined functions....[/color]

      Still easy.
      [color=blue]
      > Can someone please help me solve the problem....[/color]

      OK, I have the solutions all ready to go.

      All I need now is your instructor's email
      address, and I'll send them off.

      -Mike


      Comment

      • Mike Wahler

        #4
        Re: Comparing two numbers


        "Mike Wahler" <mkwahler@mkwah ler.net> wrote in message
        news:rwzGd.9239 $Ii4.3051@newsr ead3.news.pas.e arthlink.net...[color=blue]
        >
        > "purifier" <cnbctv18@gmail .com> wrote in message
        >[/color]
        news:19793998f8 7506ee09a36174c 4a83f22@localho st.talkaboutpro gramming.com...[color=blue][color=green]
        > > The problem is to write a program in 'C' to find the greatest of 2 given
        > > numbers... Easy?[/color][/color]

        OK, here's a hint for the first form:
        0 evaluates to false, 1 evaluates to true.

        BTW did your assignment specify whether the inputs
        could be equal? If so, what should the result be?

        -Mike


        Comment

        • Keith Thompson

          #5
          Re: Comparing two numbers

          "purifier" <cnbctv18@gmail .com> writes:[color=blue]
          > The problem is to write a program in 'C' to find the greatest of 2 given
          > numbers... Easy? huh
          > here's the catch
          > do not use 'if' or any conditional statements
          > if u want it to be a little more tougher you can use the if but this time
          > no relational operators or any of the predefined functions....
          >
          > Can someone please help me solve the problem....[/color]

          Q: How do I drive a screw without using a screwdriver?

          A: You don't. Use a screwdriver. That's what they're for.

          Now if you actually have some legitimate reason to drive a screw
          without using a screwdriver, someone might be able to come up with a
          method -- but nobody is going to help you unless you tell them *why*
          you want to do such a silly thing, and why your first question isn't
          "Where can I find a screwdriver?".

          It's the same thing with your question. Without any background, it's
          not particularly interesting. If the background is that it's a
          homework question, we're not going to do your homework for you. If
          it's just a fun little quiz, some of us might be interested, but you
          need to say so.

          --
          Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
          San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
          We must do something. This is something. Therefore, we must do this.

          Comment

          • Peter Nilsson

            #6
            Re: Comparing two numbers

            purifier wrote:[color=blue]
            > The problem is to write a program in 'C' to find the greatest
            > of 2 given numbers... Easy? huh
            > here's the catch
            > do not use 'if' or any conditional statements
            > if u want it to be a little more tougher you can use the if but
            > this time no relational operators or any of the predefined
            > functions....
            >
            > Can someone please help me solve the problem....[/color]

            I read a similar (but more restricted) question very recently
            in another forum.

            The following uses strtol() to convert arguments on the command
            line, and printf() to output the maximum. Otherwise, it doesn't
            use any conditional, logical, equality or relational operators,
            nor does it use any selection or iteration statements.

            Not sure if Google will mangle this...

            % type max2.c
            typedef unsigned long q1;int printf(const char*,...);long
            strtol(const char*,char**,in t);q1 q2(q1 q3,q1 q4,q1 q5,q1
            q6){return(q5-q6)*(((((q3-q4)&(q4-q3) )+2)/(((q3-q4)&(q4-
            q3))+1))-1)+q6;}q1 q7(long q8){return q2(q8,0,q2(q8/2,0,1
            ,0),0);}q1 q9(long q8){return q2(q8,-1,q2(q8/2,-1,1,q2(q8
            /2,0,1,0) ),0);}q1 q10(long);q1 q11(long q8){return 0;}q1
            q12(long q8){return 1;}q1 q13(long q8){return q10(q8/2);}
            q1 q14(long q8){q1(*q15[])(long)={q12,q1 3};return q15[q2(
            q9(q8),1,0,1)](q8);}q1 q10(long q8){q1(*q15[])(long)={q11
            ,q14};return q15[ q2(q7(q8),1,0 ,1)](q8);}q1 q17(long q3,
            long q4){return q2(q10( q3),1,q2(q10(q4 ),1,q10(q3-q4),1),
            q2(q10(q4),1 ,0,q10(q3-q4 )));}long q18(long q3,long q4){
            return q3;}long q19(long q3,long q4){return q4;}long q20(
            long q3,long q4){long(*q15[])(long,long)={q 19,q18};return
            q15[q2(q17(q3,q4),1 ,0,1)](q3,q4);}long q21(const char*q2)
            {return strtol(q2,0,10) ;}int q22(char **q23){printf(" %ld"
            "\n",q20(q21(q2 3[1]),q21(q23[2])));return 0;}int q24(char
            **q23){return 0;}int main(int q25,char**q23){ int(*q15[])(
            char**)={q24,q2 2};return q15[q2(q25,3,1,0)](q23);}

            % gcc -ansi -pedantic max2.c -o max2.exe

            % max2 -5 42
            42

            %

            --
            Peter

            Comment

            • purifier

              #7
              Re: Comparing two numbers

              It's a real sad thing to know, Sir... I didn't expect that my post would
              make you think that it was my homework... I'm an Electronics Engineer, but
              i'm interested a lot in programming... One of my friends asked me this
              question in a mail. I just pasted that mail here... For your information i
              even said "Can you please help me solve the problem" Why would I say huh
              and all kinds of stuff and then ask people to solve the problem? I could
              have directly given the problem...
              Anyway, Sorry if i mislead you guys... Thank you for the replies.. Atleast
              if possible, please give me some ideas...
              Thank You

              Comment

              • Nick Austin

                #8
                Re: Comparing two numbers

                On 16 Jan 2005 16:01:42 -0800, "Peter Nilsson" <airia@acay.com .au>
                wrote:

                [...][color=blue]
                >The following uses strtol() to convert arguments on the command
                >line, and printf() to output the maximum. Otherwise, it doesn't
                >use any conditional, logical, equality or relational operators,
                >nor does it use any selection or iteration statements.[/color]

                [...][color=blue]
                >typedef unsigned long q1;
                >q1 q2(q1 q3,q1 q4,q1 q5,q1 >q6)
                >{
                > return(q5-q6)*(((((q3-q4)&(q4-q3) )+2)/(((q3-q4)&(q4-q3))+1))-1)+q6;
                >}[/color]

                I don't think that's portable, namely the use of the bitwise operator:

                ( q3 - q4 ) & ( q4 - q3 )

                It seems to me that it assumes twos compliment and could give the
                wrong answer on other architectures.

                Nick.

                Comment

                • Mike Wahler

                  #9
                  Re: Comparing two numbers


                  "purifier" <cnbctv18@gmail .com> wrote in message
                  news:8a6984c70e ddf0d59fc9d38e4 4703a2f@localho st.talkaboutpro gramming.com...[color=blue]
                  > It's a real sad thing to know, Sir... I didn't expect that my post would
                  > make you think that it was my homework... I'm an Electronics Engineer, but
                  > i'm interested a lot in programming... One of my friends asked me this
                  > question in a mail. I just pasted that mail here... For your information i
                  > even said "Can you please help me solve the problem" Why would I say huh
                  > and all kinds of stuff and then ask people to solve the problem? I could
                  > have directly given the problem...
                  > Anyway, Sorry if i mislead you guys... Thank you for the replies.. Atleast
                  > if possible, please give me some ideas...[/color]

                  I gave you one good clue already:

                  0 evaluates to false, 1 evalutates to true.

                  Another:

                  0 + x == x

                  -Mike


                  Comment

                  • infobahn

                    #10
                    Re: Comparing two numbers

                    purifier wrote:[color=blue]
                    >
                    > It's a real sad thing to know, Sir... I didn't expect that my post would
                    > make you think that it was my homework... I'm an Electronics Engineer, but
                    > i'm interested a lot in programming...[/color]

                    Then you shouldn't have any trouble deriving a solution yourself.
                    [color=blue]
                    > Anyway, Sorry if i mislead you guys... Thank you for the replies.. Atleast
                    > if possible, please give me some ideas...[/color]

                    Here are some hints: in C, you can turn an expression into a 0 or a 1
                    using !!(expression), and you can put "first number is bigger" and
                    "second number is bigger" into an array like this:

                    char *bigger[] =
                    {
                    "first number is bigger",
                    "second number is bigger"
                    };

                    You can demonstrate equality as a separate step (possibly a preceding
                    step):

                    char *equality[] =
                    {
                    "numbers are equal",
                    "numbers are not equal"
                    };

                    Here's another hint: you can turn a number into a string using
                    sprintf, you can compare two strings for equality using strcmp,
                    which rather conveniently returns 0 when the two strings are
                    equal. !!strcmp, of course, returns either 0 or 1.

                    Here's yet another hint: you are not limited to storing your
                    original two numbers in strings. You can also store their
                    difference. If they weren't equal, one will be bigger than
                    the other. If the second is bigger than the first, the result
                    of a subtraction will be negative. Detecting a minus sign
                    in the first byte of a string is trivial.

                    If an electronic engineer can't get it with all those hints,
                    there's something wrong with the awards process.

                    Comment

                    • Mike Wahler

                      #11
                      Re: Comparing two numbers

                      "infobahn" <infobahn@btint ernet.com> wrote in message
                      news:41EB63B2.2 D262EE6@btinter net.com...[color=blue]
                      > If an electronic engineer can't get it with all those hints,
                      > there's something wrong with the awards process.[/color]

                      You gave away more than I would have! :-)

                      The only additional clue I was going to add was
                      the word "array".

                      -Mike


                      Comment

                      • Bonj

                        #12
                        Re: Comparing two numbers

                        Easy - use inline assembly.
                        Or subtract one from the other and the test the sign bit.


                        "purifier" <cnbctv18@gmail .com> wrote in message
                        news:19793998f8 7506ee09a36174c 4a83f22@localho st.talkaboutpro gramming.com...[color=blue]
                        > The problem is to write a program in 'C' to find the greatest of 2 given
                        > numbers... Easy? huh
                        > here's the catch
                        > do not use 'if' or any conditional statements
                        > if u want it to be a little more tougher you can use the if but this time
                        > no relational operators or any of the predefined functions....
                        >
                        > Can someone please help me solve the problem....
                        >[/color]


                        Comment

                        • Bonj

                          #13
                          Re: Comparing two numbers

                          People always like to think that the help they're giving is of commercial
                          value. If they think otherwise, they'll jump on the 'homework' bandwagon.
                          Happens every time.

                          "purifier" <cnbctv18@gmail .com> wrote in message
                          news:8a6984c70e ddf0d59fc9d38e4 4703a2f@localho st.talkaboutpro gramming.com...[color=blue]
                          > It's a real sad thing to know, Sir... I didn't expect that my post would
                          > make you think that it was my homework... I'm an Electronics Engineer, but
                          > i'm interested a lot in programming... One of my friends asked me this
                          > question in a mail. I just pasted that mail here... For your information i
                          > even said "Can you please help me solve the problem" Why would I say huh
                          > and all kinds of stuff and then ask people to solve the problem? I could
                          > have directly given the problem...
                          > Anyway, Sorry if i mislead you guys... Thank you for the replies.. Atleast
                          > if possible, please give me some ideas...
                          > Thank You
                          >[/color]


                          Comment

                          • Przemek Drochomirecki

                            #14
                            Re: Comparing two numbers

                            purifier wrote:[color=blue]
                            > The problem is to write a program in 'C' to find the greatest of 2 given
                            > numbers... Easy? huh
                            > here's the catch
                            > do not use 'if' or any conditional statements
                            > if u want it to be a little more tougher you can use the if but this time
                            > no relational operators or any of the predefined functions....
                            >
                            > Can someone please help me solve the problem....
                            >[/color]

                            max(a,b) = (abs(a-b)+a+b)>>1;

                            this hint has of course some obvious limitations :)


                            przemek drochomirecki

                            Comment

                            • À̱â¿î

                              #15
                              Re: Comparing two numbers

                              Hi~ all

                              i think that this is the problem about getting sign bit of numbers...
                              this is very easy... see follow code lines...
                              you can simplfy multiple lines to a line...


                              int main(int argc, char *argv[])
                              {
                              int a = 2.0;
                              int b = -23.1;
                              int lagestNumber = 0;
                              int signBitCount = (sizeof(int) * 8) - 1;
                              int signBitMask = 0x1 << signBitCount;
                              int a_minus_b = a - b;
                              int b_minus_a = b - a;
                              int signBitA = 0;
                              int signBitB = 0;

                              signBitA = ((unsigned)a_mi nus_b & signBitMask) >> signBitCount;
                              signBitB = ((unsigned)b_mi nus_a & signBitMask) >> signBitCount;

                              x = (signBitA ^ 0x1) * a + (signBitB ^ 0x1) * b;
                              printf("lagestN umber = %d\n", lagestNumber);

                              return 0;
                              }


                              "purifier" <cnbctv18@gmail .com> wrote in message
                              news:19793998f8 7506ee09a36174c 4a83f22@localho st.talkaboutpro gramming.com...[color=blue]
                              > The problem is to write a program in 'C' to find the greatest of 2 given
                              > numbers... Easy? huh
                              > here's the catch
                              > do not use 'if' or any conditional statements
                              > if u want it to be a little more tougher you can use the if but this time
                              > no relational operators or any of the predefined functions....
                              >
                              > Can someone please help me solve the problem....
                              >[/color]


                              Comment

                              Working...