-4

i need help, the code below does not work as it returns nothing when i run it. I'm trying to add big number so large that the numbers won’t be able to be represented in the standard C++ integer data structures

mission.cpp

void Big2Add(const char * num1, const char * num2, char * result)
    {
        string a = num1;
        string b = num2;
        int min = (a.length() < b.length() ? a.length():b.length());
       int max = (a.length() < b.length() ? b.length():a.length());

       int *n1 = new int[max];
       int *n2 = new int[max];

       for (unsigned int i=0; i < a.length(); i++)
       {
          n1[i] = a.at(a.length() - 1 -i) - 48;
       }

       cout << a << endl;

       for (unsigned int i=0; i < b.length(); i++)
       {
          n2[i] = b.at(b.length()-1 -i) - 48;
       }

       cout << b << endl;

       int carry = 0;

       int* sum = new int[max];

       int k=0;
       for (k = 0; k < max; k++)
       {
          sum[k] = (n1[k] + n2[k] + carry) % 10;

          if ( (n1[k] + n2[k] + carry) >= 10)
             carry = 1;
          else carry = 0;
       }
       sum[max] = carry;

       for (int j= max; j >= 0; j--)
       {

            *result = sum[j];
          }
       }

main.cpp

 char result[10];
        const char * num1 = "10";
        const char * num2 = "10";
        Big2Add(num1, num2, result);
        cout << "Part 3" << endl;
        cout << "The addition of " << num1 << " and " << num2 << " is " <<     result << endl;
        cout << endl;
glence
  • 1
  • 2

2 Answers2

1

First obvious bug:

   int* sum = new int[max];

...

   sum[max] = carry;

You need to allocate max+1 in order to use position max.

Second and third obvious bugs:

    *result = sum[j];

You forgot to advance result and you forgot to add '0'

Fourth, you forgot to null terminate the string.

Try:

   for (int j= max; j >= 0; j--)
   {

        *(result++) = sum[j] + '0';
   }
   *result = 0;
JSF
  • 5,281
  • 1
  • 13
  • 20
0

ok it works now tks but my main problem now is that is that there is a extra zero in front of my answer if let say i do 10 plus 10 it will give me 010 or 100 with 10 it will give me 0110 but if i plus 99 with 99 it will give 198 y?

void Big2Add(const char * num1, const char * num2, char * result)
    {

        string a = num1;
        string b = num2;
       int max = (a.length() < b.length() ? b.length():a.length());

       int *n1 = new int[max];
       int *n2 = new int[max];

       unsigned int i;
        for (i=0; i < a.length(); i++)
        {
            n1[i] = a.at(a.length() - 1 -i) - 48;
        }
        for (int j = i; j < max; ++j) 
        {
            n1[j] = 0;
        }



        for (i=0; i < b.length(); i++)
        {
            n2[i] = b.at(b.length()-1 -i) - 48;
        }
        for (int j = i; j < max; ++j) 
        {
            n2[j] = 0;
        }

       int carry = 0;


       int* sum = new int[max];

       int k=0;

       for (k = 0; k < max; k++)
       {
          sum[k] = (n1[k] + n2[k] + carry) % 10;

          if ( (n1[k] + n2[k] + carry) >= 10)
             carry = 1;
          else carry = 0;
       }
       sum[max] = carry;


       for (int j= max ; j >= 0; j--)
       {
            if(sum[0] == 0)
            {

            }
            *(result++) = sum[j] + '0';
       }


       *result = 0;
    }
glence
  • 1
  • 2
  • If you don't want the leading zero, test carry after the addition loop. If carry is nonzero store it in `sum[max]` (as you do now) otherwise decrement max. – JSF Aug 02 '15 at 23:27
  • tks it work perfectly now tks for taking your time to solve my error – glence Aug 04 '15 at 05:09