Applying Array elements to New Array?

Dec 27, 2016 at 4:26am
How can I save my original array as well as the new changes I made to it into another (bigger) array called NewArray[6][6]? I was trying various things without any good success. Thanks.

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
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

int main ()
{
	int r, c;
	int MyArray[3][3] = {
				{1,2,3},
			        {4,5,6},
				{7,8,9}
			    };

	int NewArray[6][6];


	for (r = 0; r < 3; r++)
	{
		for (c = 0; c < 3; c++)
	{
		cout << MyArray[r][c] << ",";
	}
		cout << endl;
	}

	for (r = 0; r < 3; r++)
	{
		for (c = 0; c < 3; c++)
	{
		MyArray[r][c] *= 2;
	}
	}

	for (r = 0; r < 3; r++)
	{
		for (c = 0; c < 3; c++)
	{
		cout << MyArray[r][c] << ",";
	}
		cout << endl;
	}

	system("pause");
	return 0;
}
Dec 27, 2016 at 4:31am
The old array will only fill part of the bigger one. That was intended, right?

You can just update it when you're updating the old one

1
2
3
4
5
6
7
8
for (r = 0; r < 3; r++)
{
  for (c = 0; c < 3; c++)
  {
    MyArray[r][c] *= 2;
    NewArray[r][c] = MyArray[r][c];
  }
}
Dec 27, 2016 at 4:37am
JayhawkZombie,

With NewArray[6][6]
I wanted to fill in the first 3 rows and columns with the original array.
But then fill in rows and columns 4-6 with the changes I made to the original array which was to multiply the elements in the original by 2.
Basically I want NewArray[6][6] to store the following.

1,2,3,
4,5,6,
7,8,9,
2,4,6,
8,10,12,
14,16,18

I want store it like this, to do later manipulation to all of the element values at once.
Dec 27, 2016 at 4:54am
You should reconsider you NewArray size, though. What you're getting is 36 elements when it appears you really want 12.

Maybe 3 rows and 6 columns would be better.
int NewArray[6][3];

Then you could do something like
1
2
3
4
5
6
7
8
9
  for (r = 0; r < 3; r++)
  {
    for (c = 0; c < 3; c++)
    {
      NewArray[r][c] = MyArray[r][c];
      MyArray[r][c] *= 2;
      NewArray[3 + r][c] = MyArray[r][c];
    }
  }
Dec 27, 2016 at 5:24am
JayhawkZombie,

You are absolutely right. I guess I just doubled the array rows and columns to get [6][6].

You are totally right, 3 rows and 6 columns is much better.
Your example is excellent.

Thanks so much! :)
Topic archived. No new replies allowed.