the original two dimensional array was
21 26 31 36
41 46 51 56
61 66 71 76
81 86 91 96
has to look like this after the function runs
96 26 31 38
41 71 66 56
61 51 46 76
36 86 91 21
if you look between the two ... you can see what is meant by the reverse diagonal part ....
i have pondered over this for a few days and all the different thigns i try fail misserably ...
can anyone help ...???
Please ....
How to write a function (reverseDiagonal) that reverses both diagonals of a two dimensional array (in c++) ?
Here is how you would walk one diagonal. The other is just an simple modification of the first walk.
for (int r =0, c = 0; r %26lt; HEIGHT/2 %26amp;%26amp; c %26lt; WIDTH/2; r++, c++){
swap( array[r][c] , array[HEIGHT - r - 1][ WIDTH - c - 1] );
}
For the second you do essentially the same thing but you would initialize it differently, invert the test for c, and decrement c instead of incrementing it.
for (int r = 0, c = WIDTH -1; r %26lt; HEIGHT/2; c %26gt; WIDTH/2; r++, c--){
swap( array[r][c], array[HEIGHT - r - 1][ WIDTH - c ];
}
You could avoid some of the math by adding two other variables that started at the opposited corner and then moved in. Your test would then be to see when their values crossed.
Here's how that would look with the first loop
for (r_0 = 0, c_0 = 0, r_1 = HEIGHT - 1, c_1 = WIDTH - 1; r_0 %26gt; r_1 %26amp;%26amp; c_0 %26gt; c_1; r_0++, c_0++, r_1--, c_1--){
swap(array[r_0][c_0], array[r_1][c_1]);
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment