Thread: Help for a serious program

1. 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.

2. 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.

3. HElp

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

4. 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. 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 */
}```

6. 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. Originally Posted by mr09082
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. Code:
```typedef struct
{
int number;
}Number;

int fun(Number num1,Number num2)
{
return (num1.number-num2.number);
}```

9. Originally Posted by Jesius
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?

10. Originally Posted by itsme86
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.

11. Is that just part of this code?

12. Why so serious?

13. 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. Originally Posted by rags_to_riches
Is that just part of this code?
How have you found it?