0

I am trying to implement the digitwise addition of unsigned ints, given by user input, with carryover and arrays. This is the general idea of the below code:

  • receive a,b from user input and allocate memory for 2 arrays for each digit/char in a, b
  • allocate memory for an array that can contain the max amount of digits of a,b + 1 in case of carryover into first digit
  • populate both arrays with for loops (i think my numbers will be flipped, since strings start from the left iirc - but thats the least of my problems and easily fixable once I can actually test it)
  • add the elements of the arrays containing the digits of a,b one by one + previous carryover and determine whether that addition caused carryover. Once the smaller of the two arrays is exceeded, populate only with elements of the larger one and add potential carryover once in the first step.
  • print out the elements of the array containing the digits of the sum.

I left out some details and this is only like 90ish% finished, but the general logic should be fine after double-checking and a few tests. Though, I am running into memory issues left and right, regardless of all the adjustments i made thus far [Segmentation fault (core dumped) regardless of input]. I can't even attempt to debug with cout's etc. since it doesn't even output a cout placed anywhere in the entire program.

As you can tell I am new to C++ and would appreciate advice on how to fix this.

#include <iostream>

using namespace std;

int main(){

    string a, b;
    unsigned int carryover;
    unsigned int i;

    cout<<"Natürliche Zahl a"<<endl;
    cin>>a;
    cout<<"Natürliche Zahl b"<<endl;
    cin>>b;

    unsigned int *arr_a = (unsigned int*) malloc((a.length()-1)*sizeof(unsigned int));
    unsigned int *arr_b = (unsigned int*) malloc((b.length()-1)*sizeof(unsigned int));
    unsigned int *arr_sum = (unsigned int*) malloc(max(a.length(),b.length())*sizeof(unsigned int));

    for(i = 0; i < a.length()-1; i++){

        arr_a[i] = stoi(a.substr(i,1));

    }

    for(i = 0; i < b.length()-1; i++){

        arr_b[i] = stoi(b.substr(i,1));

    }

    carryover = 0;

    for(i = 0; i < max(a.length(),b.length())-1; i++){

        if(i < a.length()-1 && i < b.length()-1){

            arr_sum[i] = (arr_a[0]+arr_b[0])%(10^i) + carryover;
            carryover = (arr_a[0] + arr_b[0] + carryover)/(10^i);

        }

        else if(i >= a.length()-1){

            arr_sum[i] = arr_b[i] + carryover;
            carryover = 0;
        }

        else{

            arr_sum[i] = arr_a[i] + carryover;
            carryover = 0;

        }

        arr_sum[max(a.length(),b.length())] = carryover;

    }

    for(i = max(a.length(),b.length()); i >= 0; i--){

        cout<<arr_sum[i];

    }   

    free(arr_a);
    free(arr_b);
    free(arr_sum);

    return 0;

}
Gérard
  • 215
  • 3
  • 8
  • @eraGerard Why are you using malloc instead of the operator new in a C++ program? – Vlad from Moscow Oct 22 '19 at 14:34
  • 2
    `10^i` doesn't do what you think it does. – Jeffrey Oct 22 '19 at 14:35
  • I took a beginner's course of C and that's how we used to allocate memory for dynamic arrays. I didn't know it is done differently in C++ which is what I derive from how you asked. Is this the cause of my problem? @Jeffrey care to elaborate? I'd understand the problem, if I knew what it actually does (, or so I think). – Gérard Oct 22 '19 at 14:37
  • Why `a.length()-1`? You don't want the last digit to be considered? – Daniel Langr Oct 22 '19 at 14:40
  • I was told that the strings are zero terminated and thus their length is one more than the amount of digits (17 in string is "17\0"). I thought this gets rid of that excess length and returns the actual amount of digits. – Gérard Oct 22 '19 at 14:41
  • @TeraGerard You need to distinguish between "plain C" strings and objects of class `std::string`. These are the very basics of C++. I would recommend to read some [good books about C++](https://stackoverflow.com/q/388242/580083). – Daniel Langr Oct 22 '19 at 14:48
  • 1
    @TeraGerard `^` is the bitwise `xor` operator, not the power function. – Max Langhof Oct 22 '19 at 14:49
  • 2
    And about the C/C++ thing: Understanding C is a very good basis for learning C++, as many concepts (e.g. pointers, `const`, functions) stem from and behave like in C. However, there are many small and large differences, and it is better to learn about them in a structured manner (e.g. a book) than discovering them by hand/accident (see string length here). – Max Langhof Oct 22 '19 at 14:52
  • 1
    The real problem in your code here is the check `i >= 0`. Since `i` is unsigned, this will always be true, and you keep looping forever (until you access the array out of bounds). – Max Langhof Oct 22 '19 at 14:55
  • Thank you very much, that helped me get rid of the error. It's a detail I missed after making i an unsigned int once the compiler complained about comparing ints to unsigned ints. I replaced >= 0 with i != (unsigned) 0 - 1 and that seemed to fix it. So now I can continue with actually fixing errors in the logic etc. – Gérard Oct 22 '19 at 15:03

0 Answers0