int main (void) 
{
    std::array <std::array<int, 3>, 3> A =  {{ {{1,2,3}}, {{4,5,6}}, {{7,8,9}} }};
    std::array <std::array<int, 4>, 4> B =  {{ {{0,1,2,3}}, {{4,5,6,7}}, {{8,9,10,11}}, {{12,13,14,15}} }};
    print_spiral(A);
    print_spiral(B);
    return 0;
}
Exemple #2
0
void print_spiral(int mat[][N_MAX], int m, int n, int k) {
  if (m <= 0 || n <= 0)
    return;
  if (m == 1) {
    for (int j = 0; j < n; j++)
      cout << mat[k][k+j] << " ";
    return;
  }
  if (n == 1) {
    for (int i = 0; i < m; i++)
      cout << mat[k+i][k] << " ";
    return;
  }
  // print from top left
  for (int j = 0; j < n - 1; j++)
    cout << mat[k][k+j] << " ";
  // print from top right
  for (int i = 0; i < m - 1; i++)
    cout << mat[k+i][k+n-1] << " ";
  // print from bottom right
  for (int j = 0; j < n - 1; j++)
    cout << mat[k+m-1][k+n-1-j] << " ";
  // print from bottom left
  for (int i = 0; i < m - 1; i++)
    cout << mat[k+m-1-i][k] << " ";
 
  print_spiral(mat, m-2, n-2, k+1);
}
Exemple #3
0
    vector<vector<int> > generateMatrix(int n) {
        if(n <= 0) return {};
        
        vector<vector<int>> result(n, vector<int>(n, 0));
        int x1 = 0, y1 = 0, x2 = n-1, y2 = n-1;
        int flag = true;
        int ind = 1;
        
        while(x1 <= x2 && y1 <= y2){
            if(flag) {
                print_spiral(result, x1, y1, x2, y2, flag, ind);
                x1++;
                y2--;
            }else{
                print_spiral(result, x1, y1, x2, y2, flag, ind);
                y1++;
                x2--;
            }

            flag = !flag;
        }
        
        return result;
    }
Exemple #4
0
int main(int argc,char **argv){
    FILE *file = fopen(argv[1], "r");
    char line[1024];
    int n,m,i;
    Matr *matr;
    char *app;
    int *array;
    while (fgets(line,1024, file)) {
		i=0;
		app = strtok(line,"; ");
		n=atoi(app);
		app = strtok(NULL,"; ");
		m=atoi(app);
		matr = (Matr *) malloc(n*m*sizeof(Matr));	
		fill_in_matr(matr,app,n,m);
		print_spiral(matr,n,m);
    }
    return 0;

}
Exemple #5
0
void print_spiral_helper(int mat[][N_MAX], int m, int n) {
  print_spiral(mat, m, n, 0);
}