This looks pretty good so far, except that your declaration of A limits matrices
to 5x5.
You can actually implement the whole thing in 2 manipulation functions, because
reversing diagonals, rows, or columns can be achieved with a single function with a couple parameters and cycling rows and columns can be achieved with a second function.
I'll help you write the reversing functions, then you should be able to write the rest.
Let's think about reversing a simple array of integers. Let's take { 0, 1, 2, 3, 4 } as
the example. I'll number the elements 0 through 4 since C/C++ is zero based.
In the above example, you need to swap the 0 and 4, and then the 1 and 3. The
2 is in the middle, so it doesn't move. Note that if I swap 2 with itself, it doesn't move.
Let's take a second example: { 0, 1, 2, 3, 4, 5 }. Even number of elements this time. Swap the 0 and 5, 1 and 4, then 2 and 3. All elements get swapped because there is no "middle" element.
Your problem is two-dimensional, but the same rules apply.
How do you swap two numbers/elements? You probably covered it in class. First, you make a copy of element #1. Then copy element #2 over element #1. Then copy the temporary over element #2. As an example of swapping two integers:
1 2 3 4 5
|
void swap( int* a, int* b ) {
int temp = *a;
*a = *b;
*b = temp;
}
| |
(Note, there are at least three better ways to do this, but I don't know if you've
covered references, templates, or the STL in your class. Let me know.)
Ok. Reversing major diagonal. The major diagonal has coordinates (0,0), (1,1),
(2,2) ... (n,n). So:
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
|
// Handy little function to swap two elements. Again, lots of better ways to do
// this, but I'm giving you the way you were probably taught.
void Swap( char* a, char* b ) {
char temp = *a;
*a = *b;
*b = temp;
}
// Note: you'll need to pass in the actual size of the matrix to the function.
// Also, not sure you need the input file since you're already read it.
void Rev_Major( char A[][cols], int size, ofstream& fout ) {
// We want to swap element (0,0) with element (size-1,size-1)
// We want to swap element (1,1) with element (size-2,size-2), etc.
// Stop when we've gotten halfway through, because we've already
// swapped all the elements. If we kept going, we'd swap everything
// right back into original position!
for( int p = 0; p < size/2; ++p )
Swap( &A[p][p], &A[size-p-1][size-p-1] );
}
// Reversing minor diagonal is the same algorithm, just different elements.
void Rev_Minor( char A[][cols], int size, ofstream& fout ) {
// We want to swap element (size-1,0) with element (0,size-1)
// We want to swap element (size-2,1) with element (1,size-2), etc.
// Stop when we've gotten halfway through, because we've already
// swapped all the elements. If we kept going, we'd swap everything
// right back into original position!
for( int p = 0; p < size/2; ++p )
Swap( &A[size-p-1][p], &A[p][size-p-1] );
}
// Reversing columns is easy. Just pass in the column#.
void Rev_Col( char A[][cols], int size, int column, ofstream& fout ) {
// We want to swap element (column,0) with element (column,size-1)
// We want to swap element (column,1) with element (column,size-2), etc.
// Stop when we've gotten halfway through, because we've already
// swapped all the elements. If we kept going, we'd swap everything
// right back into original position!
for( int p = 0; p < size/2; ++p )
Swap( &A[column][p], &A[column][size-1-p] );
}
// Reversing rows is just as easy. Just pass in the row#.
void Rev_Col( char A[][cols], int size, int row, ofstream& fout ) {
// We want to swap element (0,row) with element (size-1,row)
// We want to swap element (1,row) with element (size-2,row), etc.
// Stop when we've gotten halfway through, because we've already
// swapped all the elements. If we kept going, we'd swap everything
// right back into original position!
for( int p = 0; p < size/2; ++p )
Swap( &A[p][row], &A[size-1-p][row] );
}
| |
[Note: if you've covered any of the STL algorithms, swap() is already provided for you if you #include <algorithm>, which means you wouldn't need to write Swap() and you'd just replace the calls to (upper-case) Swap() with (lower-case) swap().]