-
Matrix
Ok heres my problem i need to take a matrix like these below and rotate the numbers.... How might i do this ?? i figure the code would have 4 for loops and each one would change the row and cololum of each varible.
Here is an example Matrix:
123
456
789
And this is what I want to do....
412
753
896
Code:
void Array2::rotate()
{
int temp;
for(int r=0; r < n; r++)
{
for(int c=c-1; c<n; c++)
{
temp=A[r][c];
A[r][c]=A[c][r];
A[c][r]= temp;
}
for(int r=r+1; r< n; r++)
{
temp=A[c][r];
A[r][c]=A[c][r];
A[r][c]=temp;
}
for(int c=r; c<n; c++)
{
temp=A[c][r];
A[r][c]=A[c][r];
A[r][c]=temp;
}
for(int r=c; r<n; r++)
{
temp=A[c][r];
A[r][c]=A[c][r];
A[r][c]=temp;
}
}
I have another question but until i find that i can do this one I will not post it..
Any help would be appreciated....
Thanks Achilles
-
Code:
#include <iostream>
#include <conio.h>
using namespace std;
int Array[][4] = { { 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 } };
const int MatrixSize = 3;
void RotateArray(bool);
int main(void)
{
for(int x = 0; x < MatrixSize; x++)
{
for(int y = 0; y < MatrixSize; y++)
{
cout<<Array[x][y]<<"\t";
}
cout<<endl;
}
cout<<endl;
RotateArray(true);
for(x = 0; x < MatrixSize; x++)
{
for(int y = 0; y < MatrixSize; y++)
{
cout<<Array[x][y]<<"\t";
}
cout<<endl;
}
getch();
return 0;
}
void RotateArray(bool Clockwise)
{
int TempSize = MatrixSize, c = 0;
int TempArray[MatrixSize][MatrixSize] = { 0 };
while(TempSize > 1)
{
for(int x = 0; x < TempSize; x++)
{
for(int y = 0; y < TempSize; y++)
{
switch (Clockwise)
{
case true:
if(x == 0 && y < TempSize - 1)
{
TempArray[x + c][y + 1 + c] = Array[x + c][y + c];
} else if (x == TempSize - 1 && y > 0) {
TempArray[x + c][y - 1 + c] = Array[x + c][y + c];
} else if (x > 0 && y == 0) {
TempArray[x - 1 + c][y + c] = Array[x + c][y + c];
} else if (x < TempSize - 1 && y == TempSize - 1) {
TempArray[x + 1 + c][y + c] = Array[x + c][y + c];
}
break;
case false:
if(x == 0 && y > 0)
{
TempArray[x + c][y - 1 + c] = Array[x + c][y + c];
} else if (x == TempSize - 1 && y < TempSize - 1) {
TempArray[x + c][y + 1 + c] = Array[x + c][y + c];
} else if (x < TempSize - 1 && y == 0) {
TempArray[x + 1 + c][y + c] = Array[x + c][y + c];
} else if (x > 0 && y == TempSize - 1) {
TempArray[x - 1 + c][y + c] = Array[x + c][y + c];
}
break;
}
}
}
TempSize -= 2;
c++;
}
if(MatrixSize % 2 == 1)
{
TempArray[MatrixSize / 2][MatrixSize / 2] = Array[MatrixSize / 2][MatrixSize / 2];
}
for(int x = 0; x < MatrixSize; x++)
{
for(int y = 0; y < MatrixSize; y++)
{
Array[x][y] = TempArray[x][y];
}
}
}
-
Is there any purpose to flipping a matrix like that or is it just something to attempt to program?
-
This shouldn't be so complicated. Just
do something like this
Code:
int col[] = {0, 1, 2, 2, 2, 1, 0, 0};
int row[] = {0, 0, 0, 1, 2, 2, 2, 1};
int tmp = A[1][0];
for (int i = 0; i < 8; ++i)
{
int r = row[i];
int c = col[i];
A[r][c] = tmp;
tmp = A[r][c];
}
-
Well i didn't get it to work i want to use 4 for loops.. that makes it simple enough... and i was told that using 4 for loops would be the best option also here is my second question.. how could i take the two matrices above
123 123
456 * 456
789 789
and multipy it by r times coloum???
ex 1*1+ 2*4 + 3* 7 then ouput that answer in r zero and coloum 0 and so and then take the next coloum and mutliply it by same row ??? anyone know how to help me in this situation ???