90 degree rotation of a 2D array in c++

In summary, the conversation discusses an issue with rotating a 2D array of type string. The program provided includes a function for rotation and the main code for input, rotation, and output. The error is identified as the program not properly rotating rectangular matrices. The solution is to create a new output matrix with the correct size and copy the rotated elements from the original matrix.
  • #1
Absolutism
28
0

Homework Statement



I am trying to rotate a 2D array of type string. I am not sure what the problem is with my function. Flipping does occur but for some reason, it's not by 90.


Homework Equations





The Attempt at a Solution


The program consists of a function and the main. Here's the code (without all the declarations in the beginning)

function:
void rotate90(string &a, string &b, string &c, string &d)
{
string temp = a;
a = b;
b = c;
c = d;
d = temp;
}

Main: (rotation + output)

for(int i=0; i<arr_row/2; i++)
for(int j=0; j<(arr_col+1)/2; j++)
rotate90(a[j], a[arr_row-1-j], a[arr_row-1-i][arr_col-1-j], a[j][arr_col-1-i]); //error is here

//output of a 90 degree rotation
cout<<"the matrix rotation"<<endl;
for (int i = 0; i< arr_row; i++) {
for (int j = 0; j< arr_col; j++) {
cout << a[j] << " ";

can someone figure out where the error is in the rotation part? Thank you
 
Physics news on Phys.org
  • #2
Hi Absolutism! :wink:

What is the error that you get?
Which matrices did you try?
 
  • #3
I didn't get an error, but I guess they won't rotate because they're rectangular?
 
  • #4
I think it does sort of rotate, but perhaps not the way it's supposed to.

Perhaps you could try the program with a couple of matrices?
Say a 2x2 one?
Or a 3x4 one?
 
  • #5
yes, it worked for square matrices, but not for rectangular ones for some reason L:
 
  • #6
What happened to a rectangular one?
 
  • #7
if I input..
7 3 6
8 1 2

I get
1 8 7
2 6 3
 
  • #8
Which matrix would you like to have had?
 
  • #9
7 3 6
8 1 2

87
12
26
 
  • #10
Okay.
Your program "rotates" the matrix in place, keeping the number of rows and columns the same.
But the matrix you just wrote down has a different number of rows and columns (exchanged actually)...

Are you supposed to rewrite the program?
Or are you supposed to find and fix bugs in it?
 
  • #11
No, I am supposed to rewrite it
 
  • #12
In that case, shouldn't you create a new output matrix with the proper number of rows and columns?
 
  • #13
Yes, I tried that and compiled it, but it results in
73
81
62

so, it wouldn't rotate any more
 
  • #14
How does that program look?

Btw, perhaps you could put your program between [ code ]...[ /code ] tags (without the spaces)?
 
  • #15
The program looks quite messy L: However, the part in bold is the matrix rotation part.
Code:
#include <iostream>      
#include <fstream>
#include <string>
#include <sstream>
#include <vector>

using  namespace  std;

[B]void rotate90(string a, string b, string c, string d)
{
   string temp = a;
   a = b;
   b = c;
   c = d;
   d = temp;
}
[/B]


int calc_occ (vector<vector<string> >a, vector<vector<string>> p )
{
int  a_rows = a.size( );
int  a_cols = a[0].size( );
int  p_rows = p.size( );
int  p_cols = p[0].size( );

int counter=0;
bool flag=false;
int x=0; int y=0; 
int i=0; int j=0;

while (a_cols!= i+x && p_cols!= j+y)
{
for (int i=0; i<p_rows; i++){
	for (int j=0; j<p_cols; j++)
{
	if (a[x+i][y+j]==p[i][j])
		flag = true; 
	if (flag = false){x++; y++;  break;} //outer loop increment x until rows then x=0 then y until last dimention 
}
	if (flag == true) {counter++; x= + p_rows; y= + p_cols; }
}

}
return counter; 
}

int  main ( )  {


ifstream infile1;
ifstream infile2;
infile1.open ("Array.txt");
infile2.open ("Pattern.txt");
if (!infile1||!infile2) {cout<<"error"; return -1;}

int arr_col, arr_row, pat_col, pat_row;
cout<<"Enter number of rows in your file (array)"<<endl;
cin>> arr_row;
cout<<"Enter Number of columns in your file (array)"<<endl;
cin>> arr_col;
   
string line; string word;
vector<vector<string> > a;

while(!infile1.eof())
{
getline(infile1, line);
istringstream stringline(line);
vector<string> b;
while(stringline >> word) {
b.push_back(word);
}
a.push_back(b);
}
//output
for (int i = 0; i< arr_row; i++) {                   
            for (int j = 0; j< arr_col; j++) {
                cout << a[i][j] << " ";
            }
            cout << endl;
    }

[B]//rotation

for(int i=0; i<arr_row/2; i++)
   for(int j=0; j<(arr_col+1)/2; j++)
       rotate90(a[i][j], a[arr_row-1-j][i], a[arr_row-1-i][arr_col-1-j], a[j][arr_col-1-i]);

//output of a 90 degree rotation
cout<<"the matrix rotation"<<endl;
for (int i = 0; i<arr_col; i++) {                   
            for (int j = 0; j<arr_row; j++) {
                cout << a[i][j] << " ";
            }
            cout << endl;
    }[/B]

cout<<"Enter number of rows in your file (pattern)"<<endl;
cin>> pat_row;
cout<<"Enter Number of columns in your file (pattern)"<<endl;
cin>> pat_col;


vector<vector<string> > p;
while(!infile2.eof())
{
getline(infile2, line);
istringstream stringline(line);
vector<string> c;
while(stringline >> word) {
c.push_back(word);
}
p.push_back(c);
}

for (int i = 0; i< pat_row; i++) {                    //output of array
            for (int j = 0; j< pat_col; j++) {
                cout << p[i][j] << " ";
            }
            cout << endl;
    }




}
 
  • #16
Well, your rotate90() function exchanges elements in the matrix "a".
But a has the wrong size for the output.

So it seems to me that you should create a new output matrix (which I don't see in your current code), and you should copy entries from the matrix "a" to this output matrix in a different position.
 

1. What is a 90 degree rotation of a 2D array in c++?

A 90 degree rotation of a 2D array in c++ is a process of rearranging the elements of a 2D array by rotating it clockwise by 90 degrees. This means that the elements in the first row of the original array will become the last column, the elements in the second row will become the second-to-last column, and so on.

2. Why do we need to rotate a 2D array in c++?

Rotating a 2D array in c++ can be useful in various applications such as image processing, matrix operations, and game development. It can also help in optimizing the use of memory and improving the performance of certain algorithms.

3. How do you rotate a 2D array in c++?

To rotate a 2D array in c++, you can use a nested for loop to traverse through the elements of the array. Within the loop, you can swap the elements in a specific pattern to achieve the 90 degree rotation. The specific swapping pattern will depend on whether the array is square or rectangular.

4. Can a 2D array be rotated by any angle in c++?

No, a 2D array cannot be rotated by any angle in c++. It can only be rotated by 90, 180, or 270 degrees using the swapping method. If you need to rotate the array by a different angle, you will need to use other techniques such as matrix multiplication or trigonometric functions.

5. How can I rotate a 2D array in c++ without using extra space?

To rotate a 2D array in c++ without using extra space, you can implement an in-place rotation algorithm. This involves swapping the elements in a specific pattern without using any additional arrays or variables. This method can be more efficient in terms of memory usage but may be more complex to implement compared to using extra space.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
723
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
713
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
936
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
Back
Top