basic math operations with one dimensional arrays

hey pals,

need your help in here,

how to add, subtract, multiply and divide 2 arrays(one dimensional) with different number of elements.

Example:

a[i]= 1 2 3 4 5
b[i]= 1 2 3 4 5 6 7

I got the point that in order to do some math operations with these 2 vectors, we need the 3-rd one,

c[i], and the result should be something like :

a[i]= 1 2 3 4 5
+
b[i]= 1 2 3 4 5 6 7
=
option a) c[i]= 2 4 6 8 10 6 7
or
option b) c[i]- 2 4 6 8 10

I mean both results should be true as they add all elements from vector a and b, the difference as you see is that the second option doesn't show the 2 last different elements ( 6 and 7)

that's what I've done so far, need your help pls:

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
  #include <iostream>
  #include <cstdlib>
  #include <ctime>
  #include <conio.h>
  #include <iomanip>
 
  using namespace std;

  
  int main()
  {
        srand(time(0));
      
        int na,nb;
              
      cout<<"Enter A -> n =";
      cin>>na;
      cout<<"Enter B -> n =";
      cin>>nb;
     
      int a[1000],b[1000];
     
      for(int i=0; i<na; i++)
      {
          a[i]=rand()%100;
      }
      for(int i=0; i<nb; i++)
      {
          b[i]=rand()%100;
      }
     
      for(int i=0; i<na; i++)
      {
          cout<<setw(4)<<a[i];
      }

      cout<<endl;
      for(int i=0; i<nb; i++)
      {
          cout<<setw(4)<<b[i];
      }
     
      int c[1000], nc;
     
      if( na > nb ) nc = na;
      else nc = nb;
     
      for(int i=0; i<nc; i++)
      {
           if( i< ?) c[i]=a[i]+b[i];
            else c[i] = ?
      }
       
      cout<<endl;
      for(int i=0; i<nc; i++)
      {
          cout<<setw(4)<<c[i];
      }
     
      return 0;
  }
Last edited on
what does it mean to add two "vectors" that have different lengths?
This is not allowed in 'math vectors', right?
you can set both to zero initially:
int a[1000] = {0};
and then add past the end:
int end = max(a_length, b_length);
for(int i = 0; i < end; i++)
c[i] = a[i]+b[i]; //whichever one runs dry is zeros, so you just get 0+other
or you could use min, and only add them out as far as both are valid, and skip/ignore/ the extras on one or the other. This would cover both of your examples, I believe.


side note, try to avoid stuff like this:
if( i< ?) c[i]=a[i]+b[i];
else c[i] = ?
it is going to be inefficient. find a way, if you can, to have the logic ingrained so it just does the work without checking anything if possible (my suggestions would both do this, but I dunno if they are what YOU wanted to do).
Last edited on
@max,
The first thing you have to do before racing off to code is to decide wtf your brand of new mathematics is supposed to do.

If there are 2 options for the answer then code them. Maybe show how users are supposed to decided how the options are used.

If there aren’t 2 options then just code the one left.

I can hardly wait to read what use there is for it/them.

@againtry @jonin, my bad because of the poor translation, actually the correct name should be :

one dimensional array not vectors as specified by me above.

So, my purpose is to add 2 arrays with different nr. of elements in it.
MaxGreen wrote:
So, my purpose is to add 2 arrays with different nr. of elements in it.


Max, you don't appear to get the point. It is irrelevant whether they are "vectors" or "arrays" or any other sort of container with multiple elements. You need to define, mathematically, how they should be added if they have different numbers of elements, because there is no obvious preferred way of doing it. And as for division ...
@lastchance, consider my lack of math in here but i've got some other approach to this example,

lets consider that there are 2 arrays:

a[10] and b[10], they can simply be added using the for loop

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
   int main()
  {
        srand(time(0));
      
        int na,nb;
              
      cout<<"Enter A -> n =";
      cin>>na;
      cout<<"Enter B -> n =";
      cin>>nb;
     
      int a[1000],b[1000];
     
      for(int i=0; i<na; i++)
      {
          a[i]=rand()%100;
      }
      for(int i=0; i<nb; i++)
      {
          b[i]=rand()%100;
      }
     
      for(int i=0; i<na; i++)
      {
          cout<<setw(4)<<a[i];
      }
      cout<<endl;
      for(int i=0; i<nb; i++)
      {
          cout<<setw(4)<<b[i];
      }
    
// to add 2 arrays I created the third one, 

      int c[1000], nc;
     
      for(int i=0; i<na; i++)
      {
           c[i]=a[i]+b[i];
      }
      nc=na;
     
      cout<<endl;
      for(int i=0; i<nc; i++)
      {
          cout<<setw(4)<<c[i];
      }

      return 0;
  }


now, what about when we've 2 arrays with different number of elements,

a[10] and b[12], aren't they possible to be added? why not?
a[10] and b[12], aren't they possible to be added? why not?


Because, as both @Againtry and I have said, there is no defined way of doing so. You have identified two possible choices - neither of which make any sense from a pure maths (vector spaces) point of view. A third way would, in fact, be simply to append them, making an array of 22 elements. None of those methods satisfy the usual rules of "addition"; (look up "commutative groups").

So, there you have it. Unless you define what you particularly want to do ... there is no a priori reason for choosing any particular one of those three.

I suspect this is an XY problem. Perhaps you can say what you are going to do with your new maths when you have got it.
Last edited on
@ lastchance , thank you so much for explanation and patience!
Topic archived. No new replies allowed.