0

As the title indicates, I want to concatenate two arrays into one larger array. for example:

array1= 1  2  3
        4  5  6
        7  8  9
array1= 10  20  30
        40  50  60
        70  80  90
array3= 1  2  3  10  20  30
        4  5  6  40  50  60
        7  8  9  70  80  90

So, I wrote this code:

#include <iostream>

using namespace std;

int main()
{
    int array1[3][3],array2[3][3],array3[3][6];
    int i,j;

    //Matrix 1 input
    cout<<"Enter matrix 1\n";
    for (i=0;i<3;i++)
    {
        for (j=0;j<3;j++)
        {
            cin>>array1[i][j];
            array3[i][j]=array1[i][j]; //Assigning array1 values to array3
        }
    }

    //array2 input
    cout<<"Enter matrix 2\n";
    for (i=0;i<3;i++)
    {
        for (j=3;j<6;j++)
        {
            cin>>array2[i][j];
            array3[i][j]=array2[i][j]; //Assigning array2 values to array3
        }
    }

    //array3 output
    cout<<"New matrix is\n";
    for (i=0;i<3;i++)
    {
        for (j=0;j<6;j++)
        {
            cout<<array3[i][j]<<"\t";
        }
    cout<<"\n";
    }
}

But when I execute it, I ended up with last row of array2 being the (2,3), (2,4) and (2,5) elements (which is right), but also being the (0,1), (0,2) and (0,3) elemnts (which should be [1 2 3]).

array3= 70  80  90  10  20  30
        4   5    6  40  50  60
        7   8    9  70  80  90

So, what's happening here?

EDIT: I did the following:

for (i=0;i<3;i++)
{
    for (j=0;j<3;j++)
    {
        cin>>array2[i][j];
        array3[i][j+3]=array2[i][j]; //Assigning array2 values to array3 and adding 3 to j
    }

And it went ok. Is the method I used "professional"? }

2
  • Have you tried debugging your code ? Commented Jan 23, 2015 at 6:49
  • In your array2 input part, j is [3,6) while array2 is array2[3][3] Commented Jan 23, 2015 at 6:51

2 Answers 2

1

This is undefined behavior:

for (i=0;i<3;i++)
    for (j=3;j<6;j++)
        array3[i][j]=array2[i][j];

array2 is 3x3 but you're indexing into it starting with [0][3] which is an error. You could get runtime checking of this sort of thing if you used C++11 std::array and its .at() method instead of raw C arrays.

Sign up to request clarification or add additional context in comments.

2 Comments

GCC's -Warray-bounds caught this on compile.
@user657267: Good point, though on my system it only detects this if compiling with -O2 or higher. A simple g++ -Wall -O2 does the trick.
0

array2 is a 3x3 array and you use j indices between 3 and 6 : you access outside of your matrix anywhere in memory.

You should instead write (eg) :

//array2 input
cout<<"Enter matrix 2\n";
for (i=0;i<3;i++)
{
    for (j=0;j<3;j++)
    {
        cin>>array2[i][j]; // j is correct for array2
        array3[i][j+3]=array2[i][j]; //Assigning array2 values to array3
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.