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.










share|improve this question
























  • 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

















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.










share|improve this question
























  • 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















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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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




















  • 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



















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Xamarin.form Move up view when keyboard appear

JBPM : POST request for execute process go wrong