Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rotate a matrix to 90 degrees? [duplicate]

Tags:

c++

Possible Duplicate:
How do you rotate a two dimensional array?

Beginner in C++ , Need to rotate to 90 degrees, I already tried to do with the help of others posts here , but without luck. Sorry for English

 # define D 9

int Ta [D][D];
short i, j;
short c=1;

for ( i=0; i < D ; i++)  {
    for ( j = 0 ;  j < D; j++)


    if ((j>i) && (j<D-i-1))  Ta[i][j]=c++; 
    else if((j>D-i-1) && (j<i)) Ta[i][j]=c++;
        else Ta[i][j]=0;  

}

for ( i = 0; i < D; i++) {
    for ( j= 0; j < D; j++) {
        printf("%3d",Ta[i][j]);
    }
    printf("\n");
}
like image 637
user1798042 Avatar asked Dec 04 '25 16:12

user1798042


1 Answers

the answer was : Thanks to Adam Liss

int r[D][D];

for (i=0; i<D; ++i) {
    for (j=0; j<D; ++j) {
        r[i][j] = t[D-j-1][i];
    }
}
like image 146
user1798042 Avatar answered Dec 07 '25 12:12

user1798042



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!