Thread: Help for a serious program

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    6

    Exclamation Help for a serious program

    Unsigned integer written in base B(2<=B<=256) can be presented as follows:
    Code:
    typedef struct {
     unsigned B;
     unsigned n;
     void * digits;
    } Number;
    Where B is base in which the number is written, n is the number of digits, and digits is pointer to the byte sequence in which the digits are stored(each byte contains one digit).least significant digit is located in the lowest byte.

    Write a function for substraction of two unsigned numbers in arbitrary base B.


    Send your opinions, ideas and if you want even the code itself. Thanks.
    Last edited by mr09082; 11-30-2010 at 07:12 AM. Reason: removed font abuse

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    This post seems to be more suitable for the Contests board, if anywhere. Avoid posting in unusual font sizes. Also, are you asking for help or what? Where's your attempt? We're not here to do homework for you.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    6

    HElp

    I am new here. The only thing i want is to here your ideas how you solve the problem.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Nobody is going to write your program for you or hand you an algorithm. You need to try to solve it on your own. Ask specific questions about what you don't understand. My general approach to problem solving for something like this is:

    1. Understand the problem, it's requirements and it's background
    2. Sketch out a general solution on paper/chalkboard/white board
    3. Code the solution in small chunks, testing and resolving errors as I go
    4. Put the finishing touches on it, and submit it

    Now, please read the following, make a serious attempt at your program and come back with specific questions.
    C Board - Announcements in Forum : C Programming
    C Board - Announcements in Forum : General Programming Boards
    How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Here's some code to get you started.
    Code:
    /**
     * Subtract one number from another, and return the result.
     * result = first - second
     */
    void subtract(Number* first, Number* second, Number* result)
    {
        int r, f, s;
        /* Convert first to a base 10 number, then store that in f */
        /* Convert second to a base 10 number, then store that in s */
        r = f - s;
        /* Convert r to the Number structure format, then store that in result */
    }
    bit∙hub [bit-huhb] n. A source and destination for information.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    bithub's solution will work, so long as your number fits within the confines of an integer on your system. Since you're likely using a 32-bit system, that's 2^31 - 1 (your problem states unsigned integers, so only non-negative values are allowed). This means you are limited to a four digit number if B is 256, which is probably not sufficient. I'm assuming your algorithm needs to iterate through the digits of each number, borrowing when needed. This is probably the way you learned to subtract when you were a kid in school.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by mr09082 View Post
    I am new here. The only thing i want is to here your ideas how you solve the problem.
    Well, that's all pretty simple really.
    All you need to do is to actually learn how to write programs in C.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    xian china
    Posts
    31
    Code:
    typedef struct
    {
        int number;
    }Number;
    
    int fun(Number num1,Number num2)
    {
        return (num1.number-num2.number);
    }

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by Jesius View Post
    Code:
    typedef struct
    {
        int number;
    }Number;
    
    int fun(Number num1,Number num2)
    {
        return (num1.number-num2.number);
    }
    Really? Are you serious? Did you even read the requirements?
    If you understand what you're doing, you're not learning anything.

  10. #10
    Registered User
    Join Date
    Nov 2010
    Posts
    6

    Question

    Quote Originally Posted by itsme86 View Post
    Really? Are you serious? Did you even read the requirements?
    The point is not to use "-" and "--", becouse it (the code) should look like a simulation of the subtraction in the processor. So bitwise operators sholud be used. I have created funkcions that can substract 2 binary numbers , like the processor does. The problem I need to figure out is how to implement that operation on two numbers whos base is larger than 16, becouse than i have to implement the substraction of two numbers on each digit( if the base is 16 it is easy becouse each digit in those numbers can not exceed F,(15)). When the Base is i dont know 145, the largest digit that can be found in a number with a base 145 is 144!!! It can not be represented by one character. I will have to extract each number, and represent it as sequence of bits, where every byte symbolizes a digit. here are the functions i`ve got:
    Code:
    /*the function takes a bit from data x , from position number i , and puts it in to the lowest bit in data on which points b.*/
    void get_bit(unsigned x, int i, unsigned * b)
    {
      *b = (x >> i) & 1;
    }
    
    /*I will not explain each function , you will understand it.*/
    void set_bit(unsigned * x, int i, unsigned b)
    {
      *x = (b << i) | (*x & ~(1 << i));
    }
    
    void half_substract(unsigned x, unsigned y, unsigned * S, unsigned * C)
    {
      *S = x ^ y;
      *C = (x ^ 1) & y;
    }
    
    void full_substract(unsigned x, unsigned y, unsigned pc,
    		    unsigned * S, unsigned * C)
    {
      unsigned S1, C1, C2;
      
      
      half_substract(x, y, &S1, &C1);
      half_substract(S1, pc, S, &C2);
      *C = C1 | C2;
    }
    
    void sub_unsigned(unsigned x, unsigned y, unsigned * S, unsigned * C)
    {
      int i;
      unsigned bx, by;
      unsigned bs, bc, pc = 0;
      
      
      for(i = 0; i < 32; i++)
    {
          get_bit(x, i, &bx);
          get_bit(y, i, &by);
    
          full_substract(bx, by, pc, &bs, &bc);
          set_bit(S, i, bs);
          pc = bc;
        }
      
      *C = pc;
    }
    Every help and every advice can come good.
    Last edited by mr09082; 11-30-2010 at 04:29 PM. Reason: More mucking about with font size

  11. #11
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Is that just part of this code?

  12. #12
    Registered User
    Join Date
    Feb 2010
    Posts
    84
    Why so serious?

  13. #13
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    The problem I need to figure out is how to implement that operation on two numbers whos base is larger than 16, becouse than i have to implement the substraction of two numbers on each digit( if the base is 16 it is easy becouse each digit in those numbers can not exceed F,(15)). When the Base is i dont know 145, the largest digit that can be found in a number with a base 145 is 144!!! It can not be represented by one character. I will have to extract each number, and represent it as sequence of bits, where every byte symbolizes a digit.
    That's not really true. One character or one byte consists of 8 bits, and can have values (if unsigned) from 0-255. That means you could have up to a base 256 number represented by a single byte. Just because we commonly use decimal and hexadecimal doesn't mean we can't use a septadecimal system (base 17, digits 0123456789ABCDEFG) or anything else we make up, even using silly symbols we made up. It's just unconventional, that's all. You will have to note that not all values from 0-255 print out nicely though, so if you try to envision a base 256 number printed out, you're going to have to have an ASCII chart handy and be prepared for some wonky looking strings of "digits".

    It really doesn't matter what symbols we use to represent a number, they're just place holders for the actual quantity. It's just happens that many people are accustomed to base 10 naturally (not all human number systems are base 10 though), and base 16 works so nice for computers because a base 16 digit is exactly 4 bits, and 2 of them make exactly one byte, a common unit of consumption for a computer. Think about how 10 in decimal and 10 in hex are different quantities. Think about roman numerals. Then, check out this article for a little fun if you have the time: Way Off Base

  14. #14
    Registered User
    Join Date
    Nov 2010
    Posts
    6
    Quote Originally Posted by rags_to_riches View Post
    Is that just part of this code?
    How have you found it?

  15. #15
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Where else...Google.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to execute other program within c program?
    By KoYoungSuk in forum C Programming
    Replies: 7
    Last Post: 06-07-2010, 05:08 AM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

Tags for this Thread