2016-11-27 2 views
-2

Ich versuche, ein Array zu drehen, die wie folgt aussieht:Array Rotation in C

a a a a 
    a b a a 
    b b b a 
    a a a a 

Ich soll es 5 mal 90 Grad drehen. Es soll in C gemacht werden.

Ich schätze jede Hilfe, weil ich nur ein Anfänger bin und auf diesem festhalte.

Vielen Dank im Voraus.

#include <stdio.h> 

int main() 
{ 
    char array_1[4][4] = { {'-','-','-','-'}, 
          {'-','o','-','-'}, 
          {'o','o','o','-'}, 
          {'-','-','-','-'}}; 

    char array_2[4][4] = { {'-','-','-','-'}, 
          {'-','o','o','-'}, 
          {'o','o','-','-'}, 
          {'-','-','-','-'}}; 

    char array_3[4][4] = { {'-','-','-','-'}, 
          {'-','o','-','-'}, 
          {'-','o','-','-'}, 
          {'-','o','o','-'}}; 

    char array_4[4][4] = { {'-','-','o','-'}, 
          {'-','-','o','-'}, 
          {'-','-','o','-'}, 
          {'-','-','o','-'}}; 

    int counter = 0; 
    int counter_1 = 0; 


    for(counter = 0; counter < 4; counter++) 
    { 
     for(counter_1 = 0; counter_1 < 4; counter_1++) 
     { 
      printf("%c ",array_1[counter][counter_1]); 
     } 
     printf(" "); 

     for(counter_1 = 0; counter_1 < 4; counter_1++) 
     { 
      printf("%c ",array_2[counter][counter_1]); 
     } 
     printf(" "); 

     for(counter_1 = 0; counter_1 < 4; counter_1++) 
     { 
      printf("%c ",array_3[counter][counter_1]); 
     } 
      printf(" "); 

     for(counter_1 = 0; counter_1 < 4; counter_1++) 
     { 
      printf("%c ",array_4[counter][counter_1]); 
     } 
     printf(" "); 

     printf("\n"); 
    } 

    printf("\n"); 

    for(counter= 0; counter < 4; counter++) 
    { 
     for(counter_1 = 3; counter_1 >= 0; counter_1--) 
     { 
      printf("%c ",array_1[counter_1][counter]); 
     } 
     printf(" "); 
     for(counter_1 = 3; counter_1 >= 0; counter_1--) 
     { 
      printf("%c ",array_2[counter_1][counter]); 
     } 
     printf(" "); 
     for(counter_1 = 3; counter_1 >= 0; counter_1--) 
     { 
      printf("%c ",array_3[counter_1][counter]); 
     } 
     printf(" "); 
     for(counter_1 = 3; counter_1 >= 0; counter_1--) 
     { 
      printf("%c ",array_4[counter_1][counter]); 
     } 
     printf(" "); 

     printf("\n"); 
    } 
    printf("\n"); 
+1

Bitte zeigen Sie, was Sie bisher geschrieben haben. –

+0

Bitte zeigen Sie das gewünschte Ergebnis an. –

+0

Ok, Ihre Rohcodierung scheint die Arbeit für die 4 getesteten Arrays zu erledigen. Also, was genau brauchst du? –

Antwort

1

wie folgt aus:

#include <stdio.h> 

typedef struct point { int x, y; } Point; 

void rotate(int n, char array[n][n]){ 
    //rotate right 90 degrees 
    if(n == 1) return ; 
    int times = n/2; 
    for(int i = 0; i < times; ++i){ 
     Point base = { i, i }; 
     for(int j = 0; j < n - 1; ++j){ 
      Point transition[4] = { {j, n-1}, {n-1,n-1-j},{n-1-j,0},{0,j} }; 
      char curr = array[base.x][base.y+j];//base + {0,j} 
      for(int k = 0; k < 4; ++k){ 
       char temp = array[base.x + transition[k].x][base.y + transition[k].y]; 
       array[base.x + transition[k].x][base.y + transition[k].y] = curr; 
       curr = temp; 
      } 
     } 
     n -= 2; 
    } 
} 

void display(int n, char array[n][n]){ 
    for(int i = 0; i < n; ++i){ 
     for(int j = 0; j < n; ++j){ 
      if(j) 
       putchar(' '); 
      putchar(array[i][j]); 
     } 
     putchar('\n'); 
    } 
    putchar('\n'); 
} 

int main(void){ 
    //demo 
    char array4[4][4] = { 
     {'1','2','3','4'}, 
     {'5','6','7','8'}, 
     {'9','A','B','C'}, 
     {'D','E','F','0'} 
    }; 
    display(4, array4); 
    int n = 4; 
    while(n--){ 
     rotate(4, array4); 
     display(4, array4); 
    } 
    char array5[5][5] = { 
     {'A','B','C','D','E'}, 
     {'F','G','H','I','J'}, 
     {'K','L','M','N','O'}, 
     {'P','Q','R','S','T'}, 
     {'U','V','W','X','Y'} 
    }; 
    display(5, array5); 
    n = 4; 
    while(n--){ 
     rotate(5, array5); 
     display(5, array5); 
    } 
} 
+0

[DEMO] (http://ideone.com/E19ewJ) – BLUEPIXY