//This program will demonstrate flipping a two-dimensional matrix //author: Vicky Fang (modified K. Shomper) //date: Oct 6th, 2008 (3 Feb 2009) #include using namespace std; int main(){ // initialize variables const int SIZE = 10; char square[SIZE][SIZE] = {{'-','-','-','-','-','-','-','-','-','-'}, {'-','-','-','-','-','-','-','-','-','-'}, {'-','-','-','-','-','-','-','-','-','-'}, {'-','-','-','-','-','-','-','-','-','-'}, {'-','-','-','-','-','-','-','-','-','-'}, {'-','-','-','-','-','-','-','-','-','-'}, {'-','-','-','-','-','-','-','-','-','-'}, {'-','-','-','-','-','-','-','-','-','-'}, {'-','-','-','-','-','-','-','-','-','-'}, {'-','-','-','-','-','-','-','-','-','-'}}; // display the original matrix cout << "The original square matrix is .." << endl; for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { cout << square [i][j] << ' '; } cout << endl; } // fill the lower traingular with a pattern for (int i = 0; i < SIZE; i++) { for (int j = 0; j < i; j++) { square[i][j] = 'l'; } } // fill the diagonal with a pattern for (int i = 0; i < SIZE; i++) { square[i][i] = 'D'; } // fill the upper triangular with a pattern for (int i = 0; i < SIZE; i++) { for (int j = i+1; j < SIZE; j++) { square[i][j] = 'u'; } } // display the newly filled matrix cout << "The new square matrix is .." << endl; for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { cout << square [i][j] << ' '; } cout << endl; } // flip the matrix (as you swap the value also change the case to show // the value moved) for (int i = 0; i < SIZE; i++) { for (int j = 0; j < i; j++) { int temp = islower(square[i][j]) ? toupper(square[i][j]) : tolower(square[i][j]); square[i][j] = islower(square[j][i]) ? toupper(square[j][i]) : tolower(square[j][i]); square[j][i] = temp; } } // display the flipped matrix cout << "The flipped square matrix is .." << endl; for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { cout << square [i][j] << ' '; } cout << endl; } return 0; }