khalidxa

Hi guys,

I am working on a two dimensional array to represent a matrix.

``int matrix[i][j];``

I need to swap all the values of matrix[1][1] to matrix[1][2] .

I am not sure how to do the swap, I did try to use for loop and matrix[1][j+1] , but it did not work.

regards

khalidxa

Salem 5,138

Well that is basically how you do it.

Post what you tried, provide it with an example input matrix, print the result and tell us how that differs from what you expected to see.

khalidxa

I have a matrix [3][2]={{1,0}{0,0}{1,1}};
My task is to swap the value of matrix[1][2]={1,0} with matrix[2][2]={0,0}and change the value of matrix[3][2]={1,1} to matrix[1][2]={1,0}.

So I need some sort of for loop to go through array read them, save them then swap switch the values. when I use loop,

``````for (int m=0;m<3; m++){

for(int k=0;k<2;k++)
{
int  swap[m][k]=matrix[m][k];
matrix[m][k]=matrix[m+1][k];

matrix[m+1][k]=swap[m][k];
cout<<""<<swap[m][k];

cout<<""<<matrix[m][k];

}``````

I get error for m++, even if I change the place for m++ I still get errors.

regards

WaltP 2,905

My advice is to tell us what the errors are. the more you explain the problem, the better (and faster) we can help you solve it.

Ancient Dragon 5,243

line 5: swap should not be an array but a simple integer, like this:

``int  swap = matrix[m][k];``

then line 8:

``matrix[m+1][k] = swap;``

and don't forget to change line 9 too.

khalidxa

If I use this method

``````swap=matrix[m][k];
matrix[m][k]=matrix[m+1][k];
matrix[m+1][k]=swap;``````

I get the error :
cannot convert from 'int' to 'int [3][2]'

khalidxa

I also get the error for using the matrix[m+1][k]

the m+1 is causing error in for loop.

Ancient Dragon 5,243

post more code including how you declared matrix and swap variables.

khalidxa

``````for (int m=0;m<3; m++){

for(int k=0;k<2;k++)
{
int  swap=matrix[m][k];
matrix[m][k]=matrix[m+1][k];
matrix[m+1][k]=swap;

cout<<""<<swap[m][k];

cout<<""<<matrix[m][k];

}

}``````

here is the code I get a runtime error when I run this code, I think it must be because of the m+1 in the array

Lerner 582

int swap=matrix[m][k];
cout<<""<<swap[m][k];

In line one swap is declared as an int and is initialized with an int value.

In line two swap is used as a 2 dimensional array. swap can't be both an int and a 2 dimensional array. swap was declared as an int and never as a 2 dimensional array. Therefore the compiler is telling you it can't be both and it can't convert from one to the other.

khalidxa

Even if i remove the cout<<swap[m][k];
i still have the runtime error. i think the [m+1] jumps the loop resriction of <10 , because I say m+1 it goes to 9+1 , but I don't know how to solve this probelm of for loop withh ++

I didn't get ur question correctly, do u want to swap values of one matrix to other or in the same matrix?

khalidxa

I didn't get ur question correctly, do u want to swap values of one matrix to other or in the same matrix?

I need to swap it inside the same matrix. I am working on an algorithm where I need to swap the values inside the same matrix with each other but I get the the run time error.

Lerner 582

If matrix is declared as

int matrix[3][2];

and you use index m+1 where m ranges from 0 to less the 3 then you are indeed overwriting the end of the array with m + 1 as an index. To fix it prevent m from ever being the same size as the last valid index for m. So in this case make the outer loop go from 0 to 2. Say something like this:

const int MAXROW = 3;
const int MAXCOL = 2;
int matrix[MAXROW][MAXCOL];
for(in m = 0; m < MAXROW - 1; ++m)
etc;

i got the answer for ur question (swaping matrix elements)
following is the code

``````int a[10][10];
int t[10];
//take the values into the matrix
for(j=0;j<c;j++)
{
t[j]=a[0][j];
}
for(i=0;i<r-1;i++)
{
for(j=0;j<c;j++)
{
a[j]=a[i+1][j];
}
}
for(j=0;j<c;j++)
{
a[j]=t[j];
}
//then print the matrix
``````

In this code r is the no of rows in the matrix
& c is the no. of columns in the matrix.
`t[]` is the one dimension array for storing elements temporarily.

you try this code......