Matrix addition

Assignment + code:
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
62
63
64
65
66
67
68
69
70
71
72
73
74

#include <iostream>
#include <cstdlib>
#define SIZE 3
using namespace std;

/*Write a function that takes two, 2-dimensional arrays as arguments. 
The function should ensure they are the same size and then print to the 
screen an array created by summing the elements in each array that are 
in the same row and column as in the example below.
     1 2 3   2 4 6   3 6 9
     4 5 6 + 2 2 3 = 6 7 9
     1 2 3   3 7 1   4 9 4
*/
//declare matrix addition function
int MatrixAdd (int matrix1[][SIZE], int matrix2[][SIZE]);

//populate two, 2d matrices, print to the screen
int main (void)
{
int myarray1 [SIZE][SIZE] = {
							{1,1,1},
							{2,2,2},
							{3,3,3},
							};
int myarray2 [SIZE][SIZE] = {
							{1,1,1},
							{2,2,2},
							{3,3,3},
							};
for (int i = 0; i < SIZE ; i++)
	{
	for (int j = 0; j < SIZE; j++)
		{	
		cout << myarray1 [i][j] << "\t";
		}
		cout << endl;
	}
	cout << endl;
for (int i = 0; i < SIZE ; i++)
	{
	for (int j = 0; j < SIZE; j++)
		{	
		cout << myarray2 [i][j] << "\t";
		}
		cout << endl;
	}
//function call
cout << MatrixAdd (myarray1, myarray2) << endl;
return 1;
}
//matrix addition function
int MatrixAdd (int* matrix1, int* matrix2)
{
//ensure matrices are of equal size
if( SIZE < 2 )
  {
    cout << "Error: Array too small" << endl;
    return 1;
  }
//create a third matrix for the matrices to be added into
int Matrix3 [SIZE][SIZE];
for (int i = 0; i < SIZE ; i++)
	{
	for (int j = 0; j < SIZE; j++)
		{
		Matrix3 [i][j] = (matrix1, int SIZE + matrix2, int SIZE);
		cout << Matrix3 [i][j] << "\t";
		}
		cout << endl;
	}
	cout << endl;
return (Matrix3[SIZE][SIZE]);
}


with output:
set8ex5.cpp: In function ‘int MatrixAdd(int*, int*)’:
set8ex5.cpp:63: error: expected primary-expression before ‘int’
set8ex5.cpp:63: error: expected `)' before ‘int’


So, initially, I'm sure my syntax is poor for Matrix3 [i][j] = (matrix1, int SIZE + matrix2, int SIZE); but I've been searching and I can't really find the proper way to add two matrices together.

I've seen matrix matrix::operator +(matrix) but I don't really understand that. Since I've included namespace std, I don't really need to call the operator each time, right?
Did it all on my own. Here is some code that creates two, 2d arrays, populates them with random integers from 0-20, then adds the two arrays. The caveat is that each one of these operations is a function by itself, lending much more to object oriented programming than one huge program string.

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
62
63
64
#include <iostream>
#include <cstdlib>
#define SIZE 3
using namespace std;

/*Write a function that takes two, 2-dimensional arrays as arguments. 
The function should ensure they are the same size and then print to the 
screen an array created by summing the elements in each array that are 
in the same row and column as in the example below.
     1 2 3   2 4 6   3 6 9
     4 5 6 + 2 2 3 = 6 7 9
     1 2 3   3 7 1   4 9 4
*/

int Random (int)
{
int i =rand () %20;
return i;
}

void MatrixAdd (int myarray1[SIZE][SIZE], int myarray2[SIZE][SIZE])
{
int myarray3 [SIZE][SIZE];
for (int i = 0; i < SIZE ; i++)
	{
	for (int j = 0; j < SIZE; j++)
		{	
		myarray3 [i][j] = myarray1 [i][j] + myarray2 [i][j];
		cout << myarray3 [i][j] << "\t";
		}
		cout << endl;
	}
cout << endl;
return;
}

int main (void)
{
srand (time(NULL));
int myarray1 [SIZE][SIZE];
int myarray2 [SIZE][SIZE];
for (int i = 0; i < SIZE ; i++)
	{
	for (int j = 0; j < SIZE; j++)
		{
		myarray1[i][j] = Random (0);	
		cout << myarray1 [i][j] << "\t";
		}
		cout << endl;
	}
cout << endl;
for (int i = 0; i < SIZE ; i++)
	{
	for (int j = 0; j < SIZE; j++)
		{	
		myarray2[i][j] = Random (0);	
		cout << myarray2 [i][j] << "\t";
		}
		cout << endl;
	}
cout << endl;
MatrixAdd (myarray1, myarray2);
return 1;
}


If someone with more experience combs through this and decides they have a simpler or more elegant solution, please let me know!
The function should ensure they are the same size


A requirement you haven't implemented.

If you change line 40 to int myArray1[SIZE-1][SIZE] your code will happily compile and run on arrays of different sizes without complaint.

[edit: It may crash, but it won't complain!]
Last edited on
A requirement you haven't implemented.


Factual, for this version. I had an if statement that wouldn't let the MatrixAdd function if the two matrices passed were of different sizes, but it's not in this code that I copied over.

As always, thanks cire!
Topic archived. No new replies allowed.