rotate elements in an array..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
#include <iostream>
using namespace std;
void main (){
int x[3][3]={1,2,3,4,5,6,7,8,9};
for (int i=0;i<3;i++)
{
for (int j=0; j<3; j++)
{
cout << x[i][j] << " ";
}
cout << " " << endl;
}
}
| |
run this code, and you'll get...
how can i rotate this to become...
It's not a rotation at all, it's a series of swaps.
Yeap!
-----------------------[X O O]
Swap these two - [O O O]
-----------------------[O O X]
-----------------------[O X O]
Then these two - [O O O]
-----------------------[O X O]
-----------------------[O O X]
Then these two - [O O O]
-----------------------[X O O]
-----------------------[O O O]
Then these two - [X O X]
-----------------------[O O O]
And you're done!
EDIT:
Or you could just do this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
#include <iostream>
using namespace std;
int main()
{
int x[3][3]={1,2,3,4,5,6,7,8,9};
for (int i=2; i>=0; i--)
{
for (int j=2; j>=0; j--)
cout << x[i][j] << " ";
cout << " " << endl;
}
cin.get();
return 0;
}
| |
Last edited on
thanks...!!
=D
Topic archived. No new replies allowed.