| hellfire1 (13) | |||
|
Hello there! To start, this thread may not be quite "General Programming" Material, however I posted it on the beginner forums a while ago, and apparently only 1 person is either willing or able to give advice, and he seems to be offline. I will give a breif explanation here of my problem, and then point you to the thread that the entire problem is posted in, as it's simpler to open a new tab then to re-paste everything here. I am having a problem currently trying to add two arrays of integers together. These arrays are up to 100 digits long each, and I'm hitting a wall with carried digits. I clearly have the code at least somewhat correct, as adding little things like 1555 and 1555 work, it outputs 3110 in another array. However, I get errors when I add things like 500 and 500, it outputs 000, or if I add 999 and 2, I get 231?! Clearly there are some major errors, but I've been searching for them, and cannot find them for the life of me... I suspect the problem is both with my output function, and my add function, as I don't think my output function will output my expanded array, but I cannot figure out how to correct this. The most up to date version of my code is here:
Sorry to ask you to open a new tab up, but the thread that explains all of this in much more detail is here: http://v2.cplusplus.com/forum/beginner/85207/ Please, please, please can someone have a serious look at this and help me with these errors? I've been at a standstill the whole day, and it's very frustrating. Many thanks, -Drew | |||
|
|
|||
| kg1992 (30) | |
|
Hello. It seems size3 needs to be bigger. I tested it. 1 + 9 gives 0 to me. 99 + 99 gives me 98. The most significant digit is keep missing. Hope it was helping. ================================================= Wait, Wait... I'm sorry. I missed something. [size2 - number] -> This goes out of range. size of array might be the number of digits. because number starts from 0, the first thing the program will read is [size2] which is one ahead of last element. | |
|
Last edited on
|
|
| kg1992 (30) | |||
You better initialize them first. Notice that I attached "= {0}" for all arrays. This will initizlie all the elements of the array to 0. | |||
|
Last edited on
|
|||
| kg1992 (30) | |||
I know that it is so bad thing to do, but it got really hard to continue on your code, so I just show my own answer to you. Hope it helps you. Idea is this: you can just add everything at once, and then think about carry. Consider following example: 99 + 999 = 1098 0 9 9 9 9 9+ --------- 9 18 18 (Pass carry from third digit to second digit) 9 19 8 (Pass carry from second digit to first digit) 10 9 8 (Pass carry from first digit to new first digit) 1 0 9 8 | |||
|
Last edited on
|
|||
| hellfire1 (13) | |||
|
Actually, this also has an error. If you add long numbers, like 15555555555555555555555555555555 by itself for example, the program freaks out. I'll look and see if I can find why, and where... See:
Here's another example of error:
It seems to have issues on very large numbers, even though it now works for smaller ones. | |||
|
Last edited on
|
|||
| hellfire1 (13) | |
|
Ok, I've found that the exact number of digits that start to return errors are when you have a number of OVER 50 digits being added to something. Then the addition part returns crazy values.... why is it doing this??! EDIT: Ok, it seems to be related to MAX_DIGITS... If MAX_DIGITS = 100, it accepts up to 50 digits correctly. If I alter MAX_DIGITS to 200, it now works with up to 100 digits, which is correct. However, this is NOT the way this is intended to work, and rather than rig it with duct tape so to speak, I'd really love if someone could help me find the error and solve this so MAX_DIGITS works correctly. I'm using a variant of the code kg1992 posted, but it works exactly the same as the one he posted, complete with the error I am having. | |
|
Last edited on
|
|
| kg1992 (30) | |||
|
Sorry my fault. Push to right is the actual problem. It will not work for big numbers. Using following pushVLIToRight will solve the problem.
| |||
|
|
|||
| hellfire1 (13) | |
|
Ah yes, thanks! :) Could you clarify what you did there^ ? I'm a bit unclear as to why the addition of the source[MAX_DIGITS], and what it is doing... if I understand it correctly, you're copying the VLI to source, and then wiping the original VLI? And then below, you re-assign the numbers from source back to VLI but from right to left? Why was this needed, rather than the original function? Sorry if all these questions are bothersome, but I like to understand what why things work. :) EDIT: Another question as well, sorry, this is more of me not understanding your code then an error, it's an easy fix if I knew how. Currently the problem works up to 99 digits, but flops at 100. I'm afraid I don't quite understand what you've done with your code well enough to try to edit it to accept 100 digits, without changing the MAX_DIGITS again to 101... But I'm sure if I stare at this in the morning I'll see it, it's late and it's a horrible time to be working on programming :P Thanks so much for the help, if you get to these questions, awesome, but if you miss them for some reason, no big deal, I can work though this. :) | |
|
Last edited on
|
|
| kg1992 (30) | |||||
|
Hello. It is not bad thing to understand, and I feel a little sorry about giving you terrible explanation about what I did. What happened with original pushVLIToRIght is somehow similar with this:
The function needed some information inside the vli. But because it rewrites some values in the vli before it can read it, problem happened. So I created new array that will mirror the original vli, and than I changed vli. "source" in the function does similar thing with "temp" in this:
| |||||
|
Last edited on
|
|||||