The curriculum must be written without the use of third-party libraries containing long arithmetic. Write wrote, the program works, but for some reason it crashes after execution, and somehow accidentally, I did not see any dependency in the input data, at which the completion occurs. Tell me, please, where there may be a mistake.

#include <iostream> #include <fstream> #include <cstring> #include <cstdio> #include <cstdlib> using namespace std; void ConvertFromStringToIntArray(int number1[], int number2[], string str1, string str2) //Π±Π΅Ρ€Π΅ΠΌ ΠΈΠ· Ρ„Π°ΠΉΠ»Π° числа Π² Π²ΠΈΠ΄Π΅ строк ΠΈ ΠΏΠ΅Ρ€Π΅Π²ΠΎΠ΄ΠΈΠΌ Π² динамичСскиС массивы { char SomeCharForAtoi[2]; for(int i = str1.size()-1; i >= 0; i--) { SomeCharForAtoi[0] = str1[i]; SomeCharForAtoi[1] = '\0'; number1[str1.size()-1-i] = atoi(SomeCharForAtoi); //заполняСм элСмСнты массива Ρ†ΠΈΡ„Ρ€Π°ΠΌΠΈ числа Π² ΠΎΠ±Ρ€Π°Ρ‚Π½ΠΎΠΌ порядкС } for(int i = str2.size()-1; i >= 0; i--) { SomeCharForAtoi[0] = str2[i]; SomeCharForAtoi[1] = '\0'; number2[str2.size()-1-i] = atoi(SomeCharForAtoi); } } void show_LongNumber(int number[], int length) //Π²Ρ‹Π²ΠΎΠ΄ΠΈΠΌ число Π½Π° экран { for(int i = length-1; i > -1; i--) cout << number[i]; cout << endl; } void add_LongNumbers(int number1[], int number2[], int length1, int length2, int &ResultLength) { if(length1 > length2) { ResultLength = length1 + 1; //Π΄Π»ΠΈΠ½Π° числа послС слоТСния(максимальная) for(int i = length1; i < ResultLength; i++) //заполняю нулями Ρ€Π°Π·Π½ΠΈΡ†Ρƒ Π² разрядах number1[i] = 0; for(int i = length2; i < ResultLength; i++) number2[i] = 0; } else { ResultLength = length2 + 1; for(int i = length1; i < ResultLength; i++) number1[i] = 0; for(int i = length2; i < ResultLength; i++) number2[i] = 0; } for(int i = 0; i < ResultLength-1; i++) //сам Π°Π»Π³ΠΎΡ€ΠΈΡ‚ΠΌ слоТСния { number1[i] += number2[i]; number1[i + 1] += (number1[i] / 10); number1[i] %= 10; } if(number1[ResultLength-1] == 0) //Ссли пСрвая Ρ†ΠΈΡ„Ρ€Π° - ноль, смСщаСм число { ResultLength--; } } int main() { ifstream fin; ofstream fout; fin.open("input.txt"); fout.open("output.txt"); string number1; string number2; fin >> number1; fin >> number2; int ResultLength; int *FirstNumber = new int[number1.size()]; int *SecondNumber = new int[number2.size()]; int length1 = number1.size(); int length2 = number2.size(); ConvertFromStringToIntArray(FirstNumber, SecondNumber, number1, number2); add_LongNumbers(FirstNumber, SecondNumber, length1, length2, ResultLength); show_LongNumber(FirstNumber, ResultLength); delete [] FirstNumber; delete [] SecondNumber; fin.close(); fout.close(); return 0; } 

    1 answer 1

    See ... You are creating a FirstNumber array with length1 elements. ConvertFromStringToIntArray it to ConvertFromStringToIntArray .

    Assume length > length2 . Then you do

     ResultLength = length1 + 1; //Π΄Π»ΠΈΠ½Π° числа послС слоТСния(максимальная) for(int i = length1; i < ResultLength; i++) //заполняю нулями Ρ€Π°Π·Π½ΠΈΡ†Ρƒ Π² разрядах number1[i] = 0; 

    Those. you number1[length1] to refer to the element number1[length1] , which, in fact, no ... :(

    It seems to be so.

    • Yes, this element is not yet, but the array is dynamic. Can't I arbitrarily continue indexing? I specifically fill in one bit more than zero, so that later during the execution of the algorithm here: number1 [i + 1] + = (number1 [i] / 10); I didn’t have the garbage in the cell when I’ll add the latest number. Or am I wrong? - Nikita
    • Of course not. You have the right to work only with those elements that you have requested. They requested L elements - everything, beyond the limits of indices from 0 to L-1 - not a stupid foot! ... - Harry
    • I apologize, I already realized my stupidity on a couple of examples;) Apparently, therefore, it ends by chance, because I appeal to absolutely random cells in my memory. Thank. - Nikita
    • Well, if everything is clear and the answer suits you - close the question with the bird of the accepted answer :) - Harry