c++ 2 dimension array rotation
up vote
0
down vote
favorite
I am writing a program in C++ to display a 2 dimensional array and then rotate it 90, 180, and 270 degrees. The array is a capital 'E' made of E's and blanks. I have written the program but when I run it it does print the E's rotated but vertically (top to bottom), my professor wants them rotated but to print horizontally (left to right).
By my logic I would have to edit my code to do:
-> print array row 1
-> rotate90
-> print array row 1
-> rotate90 (to revert to original)
-> rotate180
-> print array row 1
-> rotate180 (original)
-> rotate270
-> print array row 1
-> rotate 270 (original) ->
...continue 7 times until all rows are printed.
I know there is an easier way but I can't wrap my head around how to do it.
#include <iostream>
using namespace std;
char eArray[7]{
{'E','E','E','E','E','E','E'},
{'E',' ',' ',' ',' ',' ',' '},
{'E',' ',' ',' ',' ',' ',' '},
{'E','E','E','E','E',' ',' '},
{'E',' ',' ',' ',' ',' ',' '},
{'E',' ',' ',' ',' ',' ',' '},
{'E','E','E','E','E','E','E'},
};
int n = 7;
void rotateArray90(char a[7]){
for (int i = 0; i < n; i += 1) {
for (int j = i+1; j < n; j += 1) {
swap(eArray[i][j], eArray[j][i]);
}}}
void flipArray180(char a[7]){
for (int i = 0; i < n; i += 1) {
for (int j = 0; j < n/2; j += 1) {
swap(eArray[i][j], eArray[i][n-1-j]);
}}}
void rotateArray270(char a[7]){
for (int i = 0; i < n; i += 1) {
for (int j = 0; j < n/2; j += 1) {
swap(eArray[i][j], eArray[n-1-i][j]);
}}}
void printArray(char a[7]){
for (int i = 0; i < n; ++i){
for (int j = 0; j < n; ++j){
cout << eArray[i][j] <<" ";
}cout << endl;
}}
int main(){
printArray(eArray);
cout <<"n";
rotateArray90(eArray);
printArray(eArray);
cout <<"n";
rotateArray90(eArray);
flipArray180(eArray);
printArray(eArray);
cout <<"n";
flipArray180(eArray);
rotateArray270(eArray);
printArray(eArray);
cout <<"n";
rotateArray270(eArray);
}
Thank you for the help.
c++ multidimensional-array
add a comment |
up vote
0
down vote
favorite
I am writing a program in C++ to display a 2 dimensional array and then rotate it 90, 180, and 270 degrees. The array is a capital 'E' made of E's and blanks. I have written the program but when I run it it does print the E's rotated but vertically (top to bottom), my professor wants them rotated but to print horizontally (left to right).
By my logic I would have to edit my code to do:
-> print array row 1
-> rotate90
-> print array row 1
-> rotate90 (to revert to original)
-> rotate180
-> print array row 1
-> rotate180 (original)
-> rotate270
-> print array row 1
-> rotate 270 (original) ->
...continue 7 times until all rows are printed.
I know there is an easier way but I can't wrap my head around how to do it.
#include <iostream>
using namespace std;
char eArray[7]{
{'E','E','E','E','E','E','E'},
{'E',' ',' ',' ',' ',' ',' '},
{'E',' ',' ',' ',' ',' ',' '},
{'E','E','E','E','E',' ',' '},
{'E',' ',' ',' ',' ',' ',' '},
{'E',' ',' ',' ',' ',' ',' '},
{'E','E','E','E','E','E','E'},
};
int n = 7;
void rotateArray90(char a[7]){
for (int i = 0; i < n; i += 1) {
for (int j = i+1; j < n; j += 1) {
swap(eArray[i][j], eArray[j][i]);
}}}
void flipArray180(char a[7]){
for (int i = 0; i < n; i += 1) {
for (int j = 0; j < n/2; j += 1) {
swap(eArray[i][j], eArray[i][n-1-j]);
}}}
void rotateArray270(char a[7]){
for (int i = 0; i < n; i += 1) {
for (int j = 0; j < n/2; j += 1) {
swap(eArray[i][j], eArray[n-1-i][j]);
}}}
void printArray(char a[7]){
for (int i = 0; i < n; ++i){
for (int j = 0; j < n; ++j){
cout << eArray[i][j] <<" ";
}cout << endl;
}}
int main(){
printArray(eArray);
cout <<"n";
rotateArray90(eArray);
printArray(eArray);
cout <<"n";
rotateArray90(eArray);
flipArray180(eArray);
printArray(eArray);
cout <<"n";
flipArray180(eArray);
rotateArray270(eArray);
printArray(eArray);
cout <<"n";
rotateArray270(eArray);
}
Thank you for the help.
c++ multidimensional-array
4 copies of a 7x7 matrix isn't going to break the memory bank unless you're in the direst of extreme embedded systems. Make four copies, rotated 0º, 90º, 180º, 270º (you only need a 'rotate 90º function — you copy the original and rotate the copy by 90º; you copy the 90º-rotated matrix and rotate the copy by 90º (for 180º rotation); you copy the 180º-rotated matrix and rotate the copy by 90º (for 270º rotation). Now you arrange to print the lines of each of the 4 rotations across the page.
– Jonathan Leffler
Nov 7 at 22:16
Even if you're not allowed to make copies, you still only need the rotate 90º function. You print the first line of the unrotated matrix; then rotate 90º and print the first line of that; then rotate 90º more and print the first line of the now 180º rotated matrix; then you rotate 90º more and print the first line of the now 270º rotated matrix; then you rotate 90º more (to get back to the original) and move on printing the second row of the matrix after doing the rotation.
– Jonathan Leffler
Nov 7 at 22:19
With C++'s standard IO stream once you issue a newline, there's no 100% reliable way to go back. You can allocate a buffer that's 4 E's wide and copy the rotated Es into it as you generate them, spaced a bit apart for easier reading, and then print the big buffer to the screen.
– user4581301
Nov 7 at 22:19
You might note that to revert to the original after a 90º rotation, you need to do a 270º rotation (or a -90º rotation), and not another 90º rotation, and conversely for the 270º rotation. Two 180º rotations do get you back to where you started, of course.
– Jonathan Leffler
Nov 7 at 22:24
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am writing a program in C++ to display a 2 dimensional array and then rotate it 90, 180, and 270 degrees. The array is a capital 'E' made of E's and blanks. I have written the program but when I run it it does print the E's rotated but vertically (top to bottom), my professor wants them rotated but to print horizontally (left to right).
By my logic I would have to edit my code to do:
-> print array row 1
-> rotate90
-> print array row 1
-> rotate90 (to revert to original)
-> rotate180
-> print array row 1
-> rotate180 (original)
-> rotate270
-> print array row 1
-> rotate 270 (original) ->
...continue 7 times until all rows are printed.
I know there is an easier way but I can't wrap my head around how to do it.
#include <iostream>
using namespace std;
char eArray[7]{
{'E','E','E','E','E','E','E'},
{'E',' ',' ',' ',' ',' ',' '},
{'E',' ',' ',' ',' ',' ',' '},
{'E','E','E','E','E',' ',' '},
{'E',' ',' ',' ',' ',' ',' '},
{'E',' ',' ',' ',' ',' ',' '},
{'E','E','E','E','E','E','E'},
};
int n = 7;
void rotateArray90(char a[7]){
for (int i = 0; i < n; i += 1) {
for (int j = i+1; j < n; j += 1) {
swap(eArray[i][j], eArray[j][i]);
}}}
void flipArray180(char a[7]){
for (int i = 0; i < n; i += 1) {
for (int j = 0; j < n/2; j += 1) {
swap(eArray[i][j], eArray[i][n-1-j]);
}}}
void rotateArray270(char a[7]){
for (int i = 0; i < n; i += 1) {
for (int j = 0; j < n/2; j += 1) {
swap(eArray[i][j], eArray[n-1-i][j]);
}}}
void printArray(char a[7]){
for (int i = 0; i < n; ++i){
for (int j = 0; j < n; ++j){
cout << eArray[i][j] <<" ";
}cout << endl;
}}
int main(){
printArray(eArray);
cout <<"n";
rotateArray90(eArray);
printArray(eArray);
cout <<"n";
rotateArray90(eArray);
flipArray180(eArray);
printArray(eArray);
cout <<"n";
flipArray180(eArray);
rotateArray270(eArray);
printArray(eArray);
cout <<"n";
rotateArray270(eArray);
}
Thank you for the help.
c++ multidimensional-array
I am writing a program in C++ to display a 2 dimensional array and then rotate it 90, 180, and 270 degrees. The array is a capital 'E' made of E's and blanks. I have written the program but when I run it it does print the E's rotated but vertically (top to bottom), my professor wants them rotated but to print horizontally (left to right).
By my logic I would have to edit my code to do:
-> print array row 1
-> rotate90
-> print array row 1
-> rotate90 (to revert to original)
-> rotate180
-> print array row 1
-> rotate180 (original)
-> rotate270
-> print array row 1
-> rotate 270 (original) ->
...continue 7 times until all rows are printed.
I know there is an easier way but I can't wrap my head around how to do it.
#include <iostream>
using namespace std;
char eArray[7]{
{'E','E','E','E','E','E','E'},
{'E',' ',' ',' ',' ',' ',' '},
{'E',' ',' ',' ',' ',' ',' '},
{'E','E','E','E','E',' ',' '},
{'E',' ',' ',' ',' ',' ',' '},
{'E',' ',' ',' ',' ',' ',' '},
{'E','E','E','E','E','E','E'},
};
int n = 7;
void rotateArray90(char a[7]){
for (int i = 0; i < n; i += 1) {
for (int j = i+1; j < n; j += 1) {
swap(eArray[i][j], eArray[j][i]);
}}}
void flipArray180(char a[7]){
for (int i = 0; i < n; i += 1) {
for (int j = 0; j < n/2; j += 1) {
swap(eArray[i][j], eArray[i][n-1-j]);
}}}
void rotateArray270(char a[7]){
for (int i = 0; i < n; i += 1) {
for (int j = 0; j < n/2; j += 1) {
swap(eArray[i][j], eArray[n-1-i][j]);
}}}
void printArray(char a[7]){
for (int i = 0; i < n; ++i){
for (int j = 0; j < n; ++j){
cout << eArray[i][j] <<" ";
}cout << endl;
}}
int main(){
printArray(eArray);
cout <<"n";
rotateArray90(eArray);
printArray(eArray);
cout <<"n";
rotateArray90(eArray);
flipArray180(eArray);
printArray(eArray);
cout <<"n";
flipArray180(eArray);
rotateArray270(eArray);
printArray(eArray);
cout <<"n";
rotateArray270(eArray);
}
Thank you for the help.
c++ multidimensional-array
c++ multidimensional-array
edited Nov 7 at 22:09
Jonathan Leffler
555k886621014
555k886621014
asked Nov 7 at 22:01
Kyle Bading
12
12
4 copies of a 7x7 matrix isn't going to break the memory bank unless you're in the direst of extreme embedded systems. Make four copies, rotated 0º, 90º, 180º, 270º (you only need a 'rotate 90º function — you copy the original and rotate the copy by 90º; you copy the 90º-rotated matrix and rotate the copy by 90º (for 180º rotation); you copy the 180º-rotated matrix and rotate the copy by 90º (for 270º rotation). Now you arrange to print the lines of each of the 4 rotations across the page.
– Jonathan Leffler
Nov 7 at 22:16
Even if you're not allowed to make copies, you still only need the rotate 90º function. You print the first line of the unrotated matrix; then rotate 90º and print the first line of that; then rotate 90º more and print the first line of the now 180º rotated matrix; then you rotate 90º more and print the first line of the now 270º rotated matrix; then you rotate 90º more (to get back to the original) and move on printing the second row of the matrix after doing the rotation.
– Jonathan Leffler
Nov 7 at 22:19
With C++'s standard IO stream once you issue a newline, there's no 100% reliable way to go back. You can allocate a buffer that's 4 E's wide and copy the rotated Es into it as you generate them, spaced a bit apart for easier reading, and then print the big buffer to the screen.
– user4581301
Nov 7 at 22:19
You might note that to revert to the original after a 90º rotation, you need to do a 270º rotation (or a -90º rotation), and not another 90º rotation, and conversely for the 270º rotation. Two 180º rotations do get you back to where you started, of course.
– Jonathan Leffler
Nov 7 at 22:24
add a comment |
4 copies of a 7x7 matrix isn't going to break the memory bank unless you're in the direst of extreme embedded systems. Make four copies, rotated 0º, 90º, 180º, 270º (you only need a 'rotate 90º function — you copy the original and rotate the copy by 90º; you copy the 90º-rotated matrix and rotate the copy by 90º (for 180º rotation); you copy the 180º-rotated matrix and rotate the copy by 90º (for 270º rotation). Now you arrange to print the lines of each of the 4 rotations across the page.
– Jonathan Leffler
Nov 7 at 22:16
Even if you're not allowed to make copies, you still only need the rotate 90º function. You print the first line of the unrotated matrix; then rotate 90º and print the first line of that; then rotate 90º more and print the first line of the now 180º rotated matrix; then you rotate 90º more and print the first line of the now 270º rotated matrix; then you rotate 90º more (to get back to the original) and move on printing the second row of the matrix after doing the rotation.
– Jonathan Leffler
Nov 7 at 22:19
With C++'s standard IO stream once you issue a newline, there's no 100% reliable way to go back. You can allocate a buffer that's 4 E's wide and copy the rotated Es into it as you generate them, spaced a bit apart for easier reading, and then print the big buffer to the screen.
– user4581301
Nov 7 at 22:19
You might note that to revert to the original after a 90º rotation, you need to do a 270º rotation (or a -90º rotation), and not another 90º rotation, and conversely for the 270º rotation. Two 180º rotations do get you back to where you started, of course.
– Jonathan Leffler
Nov 7 at 22:24
4 copies of a 7x7 matrix isn't going to break the memory bank unless you're in the direst of extreme embedded systems. Make four copies, rotated 0º, 90º, 180º, 270º (you only need a 'rotate 90º function — you copy the original and rotate the copy by 90º; you copy the 90º-rotated matrix and rotate the copy by 90º (for 180º rotation); you copy the 180º-rotated matrix and rotate the copy by 90º (for 270º rotation). Now you arrange to print the lines of each of the 4 rotations across the page.
– Jonathan Leffler
Nov 7 at 22:16
4 copies of a 7x7 matrix isn't going to break the memory bank unless you're in the direst of extreme embedded systems. Make four copies, rotated 0º, 90º, 180º, 270º (you only need a 'rotate 90º function — you copy the original and rotate the copy by 90º; you copy the 90º-rotated matrix and rotate the copy by 90º (for 180º rotation); you copy the 180º-rotated matrix and rotate the copy by 90º (for 270º rotation). Now you arrange to print the lines of each of the 4 rotations across the page.
– Jonathan Leffler
Nov 7 at 22:16
Even if you're not allowed to make copies, you still only need the rotate 90º function. You print the first line of the unrotated matrix; then rotate 90º and print the first line of that; then rotate 90º more and print the first line of the now 180º rotated matrix; then you rotate 90º more and print the first line of the now 270º rotated matrix; then you rotate 90º more (to get back to the original) and move on printing the second row of the matrix after doing the rotation.
– Jonathan Leffler
Nov 7 at 22:19
Even if you're not allowed to make copies, you still only need the rotate 90º function. You print the first line of the unrotated matrix; then rotate 90º and print the first line of that; then rotate 90º more and print the first line of the now 180º rotated matrix; then you rotate 90º more and print the first line of the now 270º rotated matrix; then you rotate 90º more (to get back to the original) and move on printing the second row of the matrix after doing the rotation.
– Jonathan Leffler
Nov 7 at 22:19
With C++'s standard IO stream once you issue a newline, there's no 100% reliable way to go back. You can allocate a buffer that's 4 E's wide and copy the rotated Es into it as you generate them, spaced a bit apart for easier reading, and then print the big buffer to the screen.
– user4581301
Nov 7 at 22:19
With C++'s standard IO stream once you issue a newline, there's no 100% reliable way to go back. You can allocate a buffer that's 4 E's wide and copy the rotated Es into it as you generate them, spaced a bit apart for easier reading, and then print the big buffer to the screen.
– user4581301
Nov 7 at 22:19
You might note that to revert to the original after a 90º rotation, you need to do a 270º rotation (or a -90º rotation), and not another 90º rotation, and conversely for the 270º rotation. Two 180º rotations do get you back to where you started, of course.
– Jonathan Leffler
Nov 7 at 22:24
You might note that to revert to the original after a 90º rotation, you need to do a 270º rotation (or a -90º rotation), and not another 90º rotation, and conversely for the 270º rotation. Two 180º rotations do get you back to where you started, of course.
– Jonathan Leffler
Nov 7 at 22:24
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53198526%2fc-2-dimension-array-rotation%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
4 copies of a 7x7 matrix isn't going to break the memory bank unless you're in the direst of extreme embedded systems. Make four copies, rotated 0º, 90º, 180º, 270º (you only need a 'rotate 90º function — you copy the original and rotate the copy by 90º; you copy the 90º-rotated matrix and rotate the copy by 90º (for 180º rotation); you copy the 180º-rotated matrix and rotate the copy by 90º (for 270º rotation). Now you arrange to print the lines of each of the 4 rotations across the page.
– Jonathan Leffler
Nov 7 at 22:16
Even if you're not allowed to make copies, you still only need the rotate 90º function. You print the first line of the unrotated matrix; then rotate 90º and print the first line of that; then rotate 90º more and print the first line of the now 180º rotated matrix; then you rotate 90º more and print the first line of the now 270º rotated matrix; then you rotate 90º more (to get back to the original) and move on printing the second row of the matrix after doing the rotation.
– Jonathan Leffler
Nov 7 at 22:19
With C++'s standard IO stream once you issue a newline, there's no 100% reliable way to go back. You can allocate a buffer that's 4 E's wide and copy the rotated Es into it as you generate them, spaced a bit apart for easier reading, and then print the big buffer to the screen.
– user4581301
Nov 7 at 22:19
You might note that to revert to the original after a 90º rotation, you need to do a 270º rotation (or a -90º rotation), and not another 90º rotation, and conversely for the 270º rotation. Two 180º rotations do get you back to where you started, of course.
– Jonathan Leffler
Nov 7 at 22:24