HOW DO YOU ADD TWO ARRAYS WITH DIFFERENT LENGTH

I know how to add them if they have the same length, but my problem is how do I add two arrays that has two different lengths?


#include <iomanip>
#include <iostream>

using namespace std;

int main()
{

int array1 = {2, 3, 4, 5};
int array2 = {1, 2};






}
what do you mean by "add"? Adding arrays doesn't really make sense.

EDIT: I assume you mean you want to add the array contents. There is no one way to do this -- there are many ways.

The question is, how do you want the arrays to be added? Given the two arrays you gave in your example, what would the output be after adding them?
Last edited on
Like for example


2 3 4 5
+ 1 2
______
=???
Assuming you want to add 12 to the 45 (last two indices of the first array) I would go about it like so.

Find # of indices for smaller array. for loop to keep adding as long as counter is less than number of indices in small array. Add contents of last index in small array to last index in big array. Reloop, if counter is less than number of indices, add contents of second to last index of small array to second to last index in big array.

You can also take the smaller array and put it into an array the same size as the bigger one and pad it with zeroes in the front. That way the actual operation won't have to be changed.
2 3 4 5
+ 1 2
______
=???


If you don't know what the result of this should be, then why do you want to do it?

This operation makes no sense. I mean there are any number of possibilities.

You might get 3 5 4 5, you might get 2 3 5 7, you might get 3 5 5 7, you might get 3 3 4 7. Which do you want?
Last edited on
Mathematics:
The sum A+B of two m-by-n matrices A and B is calculated entrywise:
(A + B)i,j = Ai,j + Bi,j, where 1 ≤ i ≤ m and 1 ≤ j ≤ n.

So you can't add them if they don't have the same m-by-n size.

But something is different in programming:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main()
{

int array1[] = {2, 3, 4, 5}; //array1[0]=2,array1[1]=3,array1[2]=4,array1[3]=5
int array2[] = {1, 2}; //array2[0]=1,array2[1]=2
int array3[7];

array3[0]=array1[0]+array2[0];
array3[1]=array1[1]+array2[1];
array3[2]=array1[2];
array3[3]=array1[3];

//Or you can do something...

return 0;
} 
Last edited on
I think he's using the arrays as bignums.
closed account (10oTURfi)
I SAW CAPS PARTY AND I JUST HAD TO POST LOL!




Sorry for useless post
Last edited on
So, if array2 is meant to be 12 and array1 is meant to be 2345, then I'd say the easiest solution is:

1
2
3
4
5
6
7
8
9
10
11
12
13
int size1 = sizeof(array1[])/sizeof(array1[0]);
int size2 = sizeof(array2[])/sizeof(array2[0]);
int sum = 0;

for (i = max(size1, size2); i > 0; i--)
{
    if (size1>0)
        sum += array1[size1]*pow(10,size1);
    if(size2>0)
        sum+= array2[size2]*pow(10,size2);

    size1--; size2--;
}


I think this would work.
Stewbond: using a single int to represent the number defeats the point of having a bignum lib, though. The whole idea is an int won't be large enough to hold the number.

But that doesn't matter because the odds of this being for a bignum lib are doubtful. If that were the case he wouldn't have asked this:

2 3 4 5
+ 1 2
______
=???


The fact that he doesn't know what the sum would be is the problem, here. He's trying to do something that makes no sense. It's like he's asking "how do you add trees and milk together?". Yeah we can come up with an answer if we really want, but the question is pointless.
Topic archived. No new replies allowed.