sum 2 arrays into 3rd array?!?

Ok, I've tried the text, I've tried the search function.. I've found all types of info but I'm having trouble piecing it together.

I have a program that displays 2 arrays with matrices. Now I want to use a nested for loop to sum corresponding values in each of the arrays to display a 3rd 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>
#include <iomanip>
using namespace std;

const int COLS = 4;			//Global constants
const int TBL1_ROWS = 3;
const int TBL2_ROWS = 3;

void showArray(const int[][COLS], int);		//Function prototype

int main()		//Main
{
	int table1[TBL1_ROWS][COLS] = {{16, 18, 23, 34},
								   {54, 91, 11, 48},
								   {18, 27, 9, 81}};
	int table2[TBL2_ROWS][COLS] = {{24, 52, 77, 31},
								   {16, 19, 59, 67},
								   {73, 60, 7, 12}};
	
	showArray(table1, TBL1_ROWS);		//Displaying arrays
	showArray(table2, TBL2_ROWS);

	/*		HERES MY PROBLEM, clearly wrong. clearly failing to grasp a few concepts here,
	I've been reading but this isn't covered in the text specifically.. adding 2 arrays into 1, 
	displayed as matrices.. I have a hard time understanding initializing an array with no values 
	predetermined, then the actual math with the locations in each array is a little over my head, 
	I semi-understand using integers to hold values and incrementing but it's just not clicking...

	int myArray[3][4];
	
	for (int i = 0; i < COLS; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			myArray[i][j] = table1[i][j] + table2[i][j];
		}
	}
	cout << myArray;*/

	system("pause");
	return 0;
}

void showArray(const int numbers[][COLS],  int rows)		//"Displaying arrays" function
{
	for (int x = 0; x < rows; x++)
	{
		for (int y = 0; y < COLS; y++)
		{
			cout << setw(4) << numbers[x][y] << " ";
		}
		cout << endl;
	}
}


If anyone could kinda steer me in the right direction, I'm going to review arrays again, but as I stated, the text I'm using (at least up to this point in the course) doesn't cover how to actually manipulate or merge the arrays. The assignment is something that the instructor made up outside of the textbook problems. Any help is greatly appreciated!
Replace line 38 with a call to ShowArray.
closed account (48T7M4Gy)
line 31 should be i < 3 and line 33 j < 4 i.e. you've got rows and columns around the wrong way.
line 38 should be showArray(myArray, TBL1_ROWS)

lines 5,6 and 7 should be inside main because global constants are not required and not good programming, to be avoided.

for addition matrices must be compatible sizes so naming for the number of rows and columns can be simplified and rationalised. Using capitals for COLS and lowercase for rows is untidy and using numbers as in line 33 is bad.

Make variable names so that code is self explanatory. eg noRows, noCols or ROWS, COLS

Last edited on
thx for the replies,

awesome to have it at least running and duly noted, these aren't actual habits of mine.. I'm not skilled enough to have habits yet lol I was literally copying straight from the text tidbits of code, how they set their example programs up, I've seen that quite a few of the things we're learning in the text get flagged on here or even by our instructor in class, thx again guys
Topic archived. No new replies allowed.