2D array sum

Any help or hint how to sum 2 matrix and form third matrix.
for ex i want to sum a[i][j] and b[p][q] and store the sum in c[w][v].
sure, I'm assuming they are all the same size?

1
2
3
4
5
for (int first = 0; first < DEMENSION_ONE; first++){
  for (int second = 0; second < DEMENSION_TWO; second++){
    c at first,second = a at first,second + b at first,second
  }
}


easy enough?
not the same size mate...i'll input the values...i dont know hot to sum the a11 value form a matrix with a11 from b matrix and that store in a11 in c matrix.
Last edited on
Okay then make the third array the size of the largest dimensions of both arrays, and when you add just make an exception case so you don't go beyond the bounds of either array.
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
#include <iostream>
using namespace std;

int main ()
{

	int m, n,i,j;

	cout<<"Rows: ";
	cin>>m;
	cout<<"Col. : ";
	cin>>n;

	int a[100][100];
	int chlen;

	for (i=0; i<m; i++) {
		for (j=0; j<n; j++) {
			cout<<"Enter value: "<<i<<","<<j<<endl;
			cin>>chlen;
			a[i][j]=chlen;
			cout<<"\n\n";
		}
	}

	int p,q, r,k;

	cout<<"Rows 2: ";
	cin>>p;
	cout<<"Col. 2: ";
	cin>>q;

	int b[100][100];
	int clen;

   for (r=0; r<p; r++) {
	   for (k=0; k<q; k++) {
		   	cout<<"Enter value: "<<r<<","<<k<<endl;
			cin>>clen;
			b[r][k]=clen;
			cout<<"\n\n";

	   }
   }

   int c[100][100];



   cin.get(); cin.get();

   return 0;
}
now how to do the sum? :)
Wait a second, do you want the sum of every number in each array?! or do you just want the sum of each individual cell?
Here is my idea:

1 2 3 | 1 1 1| 2 3 4
4 5 6 | + 1 1 1 | = 5 6 7
7 8 9| 1 1 1 | 8 9 10
Last edited on
Doing the sum in this case doesn't seem to be the problem. What do you wish to return if one cell doesn't have a value defined and another tries to add to it? You could prepopulate the entire array with 0 using setfill (http://www.cplusplus.com/reference/iostream/manipulators/setfill/) but is that what you intend to do?
404 Page Not Found
just get rid of the extra )
Topic archived. No new replies allowed.