Advertisement
Wojtekd

Cross [Rekurencja]

Apr 20th, 2015
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.03 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define N 11
  5. #define M 11
  6.  
  7. void cross(int **tab, int start_w, int start_k, int stop_w, int stop_k);
  8.  
  9. int main ( void )
  10. {
  11.     int** t = (int**)calloc(N,sizeof(int*));
  12.    
  13.     for(int i = 0; i < N; i++)
  14.     {
  15.         t[i] = (int*)calloc(M,sizeof(int));
  16.     }  
  17.     cross(t,0,0,N-1,M-1);
  18.    
  19.     for(int i = 0; i < N; i++)
  20.     {
  21.         printf("\n");
  22.         for(int j = 0; j < M; j++)
  23.         {
  24.             printf("%c  ",t[i][j]);
  25.         }
  26.     }
  27.    
  28.     return 0;
  29. }
  30. void cross(int **tab, int start_w, int start_k, int stop_w, int stop_k)
  31. {
  32.     if((stop_w - start_w <= 1) || (stop_k - start_k <= 1))
  33.     {
  34.         return;
  35.     }  
  36.    
  37.     int half_k = (start_w + stop_w) / 2;
  38.     int half_w = (start_k + stop_k) / 2;
  39.    
  40.     for(int i = start_w; i <= stop_w; i++)
  41.     {
  42.         tab[i][half_w] = '*';
  43.     }
  44.     for(int i = start_k; i <= stop_k; i++) 
  45.     {
  46.         tab[half_k][i] = '*';
  47.     }
  48.     cross(tab, start_w, start_k, half_k-1, half_w-1);
  49.     cross(tab, half_k+1, half_w+1, stop_w, stop_k);
  50.     cross(tab, start_w, half_w+1, half_k-1, stop_k);
  51.     cross(tab, half_k+1,start_k,stop_w,half_w-1);
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement