Rotate the matrix 90 degrees clockwise

Source: Internet
Author: User

Data Structure Analysis: Two-dimensional arrays are used for Matrix storage. However, to store the converted matrix, two-dimensional arrays must be defined for storage because the rows and columns cannot be equal.

Question Analysis: The key lies in column-to-column conversion. First, the rows in the original matrix are converted to converted columns. The columns in the original matrix are converted to converted rows and must be rotated 90 degrees, therefore, after the original row is converted into a column, the value should be mac_row-row-1, mac_row is the total number of rows in the original matrix, and row is the row where the element to be converted is located, since row starts from 0, we need to subtract 1.

#include <iostream>#define MAC_ROW 3#define MAC_COL 4using namespace std;int main(){//define original matrix and set default value 0int OriMatrix[MAC_ROW][MAC_COL] = {0};//define converted matirxint ConvMatrix[MAC_COL][MAC_ROW] = {0};int elem = 0;//initialint row = 0;for(; row < MAC_ROW; row++){for(int col = 0; col < MAC_COL; col++){cin>>elem;OriMatrix[row][col] = elem;ConvMatrix[col][MAC_ROW - row - 1] = OriMatrix[row][col];}}//cout original matrixrow = 0;cout<<"original matrix:"<<endl;for(; row < MAC_ROW; row++){for(int col = 0; col < MAC_COL; col++){cout<<OriMatrix[row][col]<<"\t";}cout<<endl;}//cout converted matrixcout<<"converted matrix:"<<endl;row = 0;for(; row < MAC_COL; row++){for(int col = 0; col < MAC_ROW; col++){cout<<ConvMatrix[row][col]<<"\t";}cout<<endl;}system("pause");return 1;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.