Thread: Need help with some corrections

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    5

    Unhappy Need help with some corrections

    I am trying to type the C programming code to run the computation by four arithmetic operators, and them print out the result as a 16 bit binary number.

    However, I am stuck on writing the legal function divide() that our class are requiring us to handin:

    Code:
    short divide(short num1, short num2)
    {
        short i, result = 0;
    
        fum2 = num2 << 8;
    
        for(i = 0x100; i > 0; i = i >> 1){
    
            if(num1 >= num2){
                result = result + 1;
                num1 = num1 – num2;
                }
    
            num2 = num2 >> 1;
            }
    
         return result;
    
    }
    This code actually worked and returned the variable as "result" back to the main function. Thus, my main function will take the two shorts (variables) and print the output as:

    254 / 16 = 0000000000001111

    or

    25 / 25 = 0000000000000001

    However, there is a TRICKY part:
    My instructor only allow us to use the operators such as: &&, ||, &, |, <<, >>, +, -, ==, !=, and = on the function divide()

    In this case, my code would be illegal since I use operators as >= and >. I can't think about using the above allowed operators to complete this function, so I need help. Can anyone enlightened me how I should wirte this code satisfying the above condition?
    Last edited by jim090872; 11-10-2006 at 02:27 AM.

  2. #2
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    you could just change it to a do while loop

    Code:
    i = 0x100;
    do
    {
       calculations
    } while ((i = i >> 1) != 0);

    somthing like that
    Last edited by sl4nted; 11-10-2006 at 12:26 AM.

  3. #3
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    How about checking each bit of the two numbers, one after the other. If they are different, the one which has a one is greater then the one which has a 0.

    Here's an example (in 5 bit) :

    n1 : 00101 vs n2 : 10011

    First bit : 1 vs 1. Same so as of yet, they're both equal.
    Second bit : 0 vs 1. Different. So as of this step n2 is greatest.
    Third bit : 1 vs 0. Different. So now n1 is greatest.
    Fourth bit : 0 vs 0. Same. So no change, and n1 is still greatest.
    Fifth bit : 0 vs 1. Diffrent. So the greatest becomes n2.

    Since you've crossed all the bits, n2 is now your greatest. Return n2.

    Sound good ?
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    5
    Quote Originally Posted by sl4nted
    you could just change it to a do while loop

    Code:
    i = 0x100;
    do
    {
       calculations
    } while ((i = i >> 1) != 0);

    somthing like that
    I just figure out the loop code by using !=
    Thanks for telling me, anyway.

    Now the problem is here: num1 >= num2
    We can't use the greater and equal operator, so I am having a trouble to find any other expression to stop the increment of result.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    5

    Cool

    Quote Originally Posted by Happy_Reaper
    How about checking each bit of the two numbers, one after the other. If they are different, the one which has a one is greater then the one which has a 0.

    Here's an example (in 5 bit) :

    n1 : 00101 vs n2 : 10011

    First bit : 1 vs 1. Same so as of yet, they're both equal.
    Second bit : 0 vs 1. Different. So as of this step n2 is greatest.
    Third bit : 1 vs 0. Different. So now n1 is greatest.
    Fourth bit : 0 vs 0. Same. So no change, and n1 is still greatest.
    Fifth bit : 0 vs 1. Diffrent. So the greatest becomes n2.

    Since you've crossed all the bits, n2 is now your greatest. Return n2.

    Sound good ?
    It doesn't make sense to me. Since we are doing the division, what's the point to check each bit and return the greatest value after all?

  6. #6
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    you were asking how to emulate >= without using >=. What I showed you there does that.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    On the other hand, the algorithm is already slow enough that you could just emulate >= by continuously decrementing both numbers and checking which reaches zero first.

    Code:
    loop
        --a, --b
        if a == 0 and b != 0
            a < b
        else if b == 0
            a >= b
    Last edited by jafet; 11-11-2006 at 07:43 AM.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > My instructor only allow us to use the operators such as: &&, ||, &, |, <<, >>, +, -, ==, !=, and =
    I've never understood the logic of assignments with such useless restrictions.

    What's it meant to teach - futility?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    5
    Quote Originally Posted by Happy_Reaper
    you were asking how to emulate >= without using >=. What I showed you there does that.
    Hi Happy Reaper:
    I follow your method and write this code:
    Code:
    hort divide(short num1, short num2){
      short i, j, ok;
      short result;
      result = 0;
    
      num2 = num2 << 8;
    
      for (i = 0x100; i != 0; i = i >> 1){
        for (j = 0; j != 15; j = j + 1){
    
          if (num1 & 1 && num2 & 0)
            ok = 1;
    
          else if (num1 & 0 && num2 & 1)
            ok = 0;
    
          else if (num1 & 1 && num2 & 1)
            ok = 1;
    
          else if (num1 & 0 && num2 & 0)
            ok = 0;
    
          num1 = num1 >> 1;
          num2 = num2 >> 1;
          } // for j
    
          if (ok == 1){
            result = result | i;
            num1 = num1 << 16;
            num2 = num2 << 16;
            num1 = num1 - num2;
        } // if
    
      num2 = num2 >> 1;
      } // for i
    However, the program still doesn't work, but I just fell like it is almost done.

    Please enlighten me more if you are still there. i will be very thankful!

    To: jafet:
    Thanks for your suggestion. But you're using >= and >, which are not allowed as well, so your code is still illegal on my assignment.

  10. #10
    Registered User
    Join Date
    Nov 2006
    Posts
    5
    Quote Originally Posted by Salem
    > My instructor only allow us to use the operators such as: &&, ||, &, |, <<, >>, +, -, ==, !=, and =
    I've never understood the logic of assignments with such useless restrictions.

    What's it meant to teach - futility?
    I feel the same way here. My professor just tries to drive students insane with such meaningless restrictons on a couple of prgramming assignments.

    However, as long as I am in this course, I still have to write the legal code and save my grade. duh..

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    These "bit-ops only" questions come up quite frequently, really. Perhaps your instructor is daft enough to have not changed the function names. Searching for these names, as well as those in your other assignments, may turn up a wealth of information for you.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  12. #12
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    I was rather thinking that you've have an alternate function "greater or equal" Which accepts 2 shorts, n1 and n2, and determines in n1 is greater or equal than n2.

    In this function, you would implement the code which I suggested.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Corrections on School assignment...
    By hubris in forum C++ Programming
    Replies: 9
    Last Post: 05-19-2009, 04:54 PM
  2. Another incorrect code needs some corrections
    By dotrunghai82 in forum C Programming
    Replies: 1
    Last Post: 11-12-2006, 09:34 PM
  3. string help needed
    By tmitch in forum C Programming
    Replies: 8
    Last Post: 11-13-2005, 02:43 PM
  4. Code in desparate need of corrections!
    By Narclepto99 in forum C++ Programming
    Replies: 2
    Last Post: 04-14-2003, 11:29 PM
  5. How to print an array? Help PLEASE!!!!
    By green_eel in forum C Programming
    Replies: 3
    Last Post: 11-17-2002, 07:23 PM