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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
#include<iostream>
bool** allocMtx(int size);
void print(bool** mtx, int size);
void rotateRight(bool** original, bool** rotated, int size);
void freeMtx(bool** mtx, int size);
int main(){
const int size = 5;
bool** one = allocMtx(size);
bool** two = allocMtx(size);
one[1][1] = one[1][3] = one[3][0] = one[4][1] = one[4][2] = one[4][3] = one[3][4] = true;
std::cout << "The original matrix:\n";
print(one, size);
std::cout << "\n\n";
rotateRight(one, two, size);
freeMtx(one, size);
freeMtx(two, size);
return 0;
}
bool** allocMtx(int size){
bool** mtx = new bool*[size];
if(mtx){
for(int i = 0; i < size; i++){
mtx[i] = new bool[size];
if(!mtx[i]){
exit(1);
}
}
}else{
exit(1);
}
return mtx;
}
void rotateRight(bool** original, bool** rotated, int size){
rotated = original;
for(int i = 0; i < size/2; i++){
for(int j = i; j < size-i-1; j++){
int temp = rotated[i][j];
rotated[i][j] = rotated[size-1-j][i];
rotated[size-1-j][i] = rotated[size-1-i][size-1-j];
rotated[size-1-i][size-1-j] = rotated[j][size-1-i];
rotated[j][size-1-i] = temp;
}
}
std::cout << "\nThe rotated matrix:\n";
print(rotated, size);
}
void print(bool** mtx, int size){
int counter = 0;
std::cout << ' ';
for(int i = 0; i < size; i++){
std::cout << i;
}
std::cout << '\n';
for(int i = 0; i < size; i++){
std::cout << i;
for(int j = 0; j < size; j++){
if(mtx[i][j] == false){
std::cout << '.';
}else{
std::cout << 'X';
}
counter++;
if(counter == size){
std::cout << '\n';
counter = 0;
}
}
}
}
void freeMtx(bool** mtx, int size){
for(int i = 0; i < size; i++){
delete[] mtx[i];
}delete[] mtx;
}
|