How to sum two int arrays ?

like for example if

Num1[0]=2
Num2[0]=3

this is not working x= Num1[0] + Num2[0];

is there any other way ?

THANKS !
Why does not

x= Num1[0] + Num2[0];

work?

I do not see the reason that prevents to add two objects if the operation + is well defined.
Remember how they taught you to add numbers in grade school.
Don't forget to carry.

If you are adding only two numbers at a time (as you should be), then the carry can never be more than one digit.

Hope this helps.
@vlad


is there any other way ? cause my program that I am working on takes two strings like "1234567" and "654321" and puts them into two int arrays which are Num1[] and Num2[] ,

so here for example Num1[0]= 7 (I reversed them so i can add them later, so it is the last number)
and Num2[0]=1

when i am adding them together like x= Num1[0] + Num2[0];

it is giving me a very weird number (-8.589...e) something weird. and every time the same number whether Num1[0] or Num1[1]

So is there any other way ?

Thanks :)
Last edited on
@Duoas. I did not forget about the carry, i actually have it in my loop.... but like i said the initial sum is not working from the first place it is giving me a weird number.
Personally, this is what I think I would do.

1
2
3
4
5
6
7
8
9
std::string str1 = "012345";
std::string str2 = "543210";

int* sum = new int[str1.size()];

for(int i = 0; i < str1.size(); ++i)
   sum[i] = (static_cast<int>(str1[i])-48) + (static_cast<int>(str2[i])-48);

delete [] sum; //when finished with dynamic variable 


Output:
5 5 5 5 5 5
well i did the exact same thing to enter the string into the array but I wanted to create a function for putting them in the arrays, and another for summing them, in the other function there is no need to put them in an array again, i just need to sum them.
but thanks anyway.
any other ideas?
Last edited on
@pholotic
plus this is wrong because string.size() might be different :)
Last edited on
I don't see why that bit of code wouldn't be able to accommodate a varying string size.

This is what I have based off of your description. Same output as before.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

void input(std::string in1, std::string in2)
{
   //Global or class variables
   myStr1 = in1; 
   myStr2 = in2;
}
template<class T>
std::vector<T> sumStrings(std::string in1, std::string in2)
{
   std::vector<T> _array;
   for(int i = 0; i < in1.size(); ++i)
      _array.push_back((static_cast<int>(in1[i])-48) + (static_cast<int>(in2[i])-48));
   return _array;
}

int main()
{
   std::vector<int> mySum;
   input("012345", "543210"); // Can be variable input
   mySum = sumStrings<int>(myStr1, myStr2);
   return 0;
}
I'm not sure why you are using a vector to store the results of two strings.

          1     1     1     1
 9999   9999   9999   9999   9999   9999
+  99  +  99  +  99  +  99  +  99  +  99
-----  -----  -----  -----  -----  -----
           8     98    098   0098  10098

So your function should look something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
bool sum( 
  char a[], int alen,  // c = a + b
  char b[], int blen, 
  char c[], int clen )
  {
  // It helps if alen >= blen
  if (alen < blen) return sum( b, blen, a, alen, c, clen );

  // Also, clen must be greater than or equal to (alen + 1)
  if (clen <= alen) return false;

  // Now you are ready to add
  int carry = 0;
  int i;

  // First, add the stuff that is in both arrays
  for (i = 0; i < blen; i++)
    {
    }

  // Next, keep adding carry through the longer array (a)
  for (; i < alen; i++)
    {
    }

  // Next, carry until no carry remains
  if (carry != 0)
    c[ clen - 1 - i ] = carry + '0';

  return true;
  }

Notice how I index the arrays as x[ xlen - 1 - i ]. Remember, you have to work from least-significant digit to most-significant.

Hope this helps.
Why did you start a new thread on this subject?

See: http://www.cplusplus.com/forum/general/107101/
Cause i started figuring it out on my own....and more questions came up lol
Topic archived. No new replies allowed.