![]() |
|
[C] Subtracting arbitrary large integers in C - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [C] Subtracting arbitrary large integers in C (/showthread.php?tid=5913665) |
Subtracting arbitrary large integers in C - kplishere - Nov 29, 2018 Hi, long post ahead. I much appreciate any kind of help. I'm doing my home assignment which includes performing arithmetics with arbitrary large numbers that are too long to fit in basic data types such as int or long long. Now, this means that if I want to do arithmetics with these numbers, I need to write my own functions to do it, which are based on the methods used to perform addition/subtraction on paper. Right now, I'm struggling with subtraction. Please note: I'm a beginner to C. What I do is very likely not to be the most efficient solution, but this is what I could figure out so far. I store a number in a structure like this: Code typedef struct Arbitrary_Large_IntegerI know that it could be easier to use char arrays instead of a structure with a single member which barely makes sense, but I'm forced to put structures in my code this time (that's a requirement for my assignment). About the function: I want to know the difference of number n and a, both stored in char arrays in ALI structures. Basically, what I'm doing is initialising two integers (temp_n and temp_a) with the current digits of n and a, subtracting them and placing the result in a new ALI instance named k. If the j-th digits of a is greater than the i-th digit of n, then I add 10 to the digit if n, finish the subtraction, and in the next turn, I increase temp_a by one. The value of number a certainly falls between 1 and n - 1 (that's given). If a is shorter than n, as soon as I reach the last digits of a, I put the remaining digits of n to the result array k. And I do this all backwards, so the initialising value of i would be the size of n -1. So far, that's primary school maths. I wrote a code which is supposed to perform this, yet I get incorrect results every time. Here is the function: Code ALI *subtraction(ALI n, ALI a, int nLength, int aLength)First "turn": imgur.com/5a9ksmZ.png can't post embededd images until I reach 50 points... It seems like when the array is passed to the function, its value changes for some reason. I can't figure out what's wrong with it. This is a quite long thread, so I'd be more than grateful if one could help me. I don't speak native English, I hope I could draw up my problem clearly. Thanks! I managed to solve the problem RE: Subtracting arbitrary large integers in C - Europa - Jan 10, 2019 Code typedef struct Arbitrary_Large_IntegerFirst off typedef is not a valid standard C structure. The Flexible Array Member (FAM) .digits must be accompanied by at least one more prior named member in addition to the flexible array member. It's recommend to put .nLength as the first member. Since your code is using a non-standard C, watch out for nLength sizeof(ALI) may be the same as nLength 0. You're also attempting to use .digits as a string with _strrev(), themallloc() is too small by 1, at least. Also malloc(0)? |