달팽이 알고리즘

 숫자를 입력하면 N * N 크기의 행렬이 만들어지고, 숫자가 달팽이 등껍질처럼 입력되는 재미있는 알고리즘.

Snail_Algotithms (Language : c)
  1.    1.
  2.       /***************************************************************************
  3.    2.
  4.       *            Snail_Algorithms.c
  5.    3.
  6.       *
  7.    4.
  8.       *  Wed Jan  2 16:45:57 2008
  9.    5.
  10.       *  Copyright  2008  pchero21
  11.    6.
  12.       *  pchero21@gmail.com
  13.    7.
  14.       ****************************************************************************/
  15.    8.
  16.        
  17.    9.
  18.       /*
  19.   10.
  20.       *  This program is free software; you can redistribute it and/or modify
  21.   11.
  22.       *  it under the terms of the GNU General Public License as published by
  23.   12.
  24.       *  the Free Software Foundation; either version 2 of the License, or
  25.   13.
  26.       *  (at your option) any later version.
  27.   14.
  28.       *
  29.   15.
  30.       *  This program is distributed in the hope that it will be useful,
  31.   16.
  32.       *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  33.   17.
  34.       *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  35.   18.
  36.       *  GNU General Public License for more details.
  37.   19.
  38.       *
  39.   20.
  40.       *  You should have received a copy of the GNU General Public License
  41.   21.
  42.       *  along with this program; if not, write to the Free Software
  43.   22.
  44.       *  Foundation, Inc., 59 Temple Place – Suite 330, Boston, MA 02111-1307, USA.
  45.   23.
  46.       */
  47.   24.
  48.        
  49.   25.
  50.       #include <stdio.h>
  51.   26.
  52.       #include <math.h>
  53.   27.
  54.        
  55.   28.
  56.       void array_rotation(int size, int (*array)[size]);
  57.   29.
  58.       void snail_algorithms(int size, int (*array)[size]);
  59.   30.
  60.        
  61.   31.
  62.       int main()
  63.   32.
  64.       {
  65.   33.
  66.           int size;
  67.   34.
  68.           int tmp, i, j, k;
  69.   35.
  70.           int first, n, size_tmp;
  71.   36.
  72.        
  73.   37.
  74.           printf(ninsert number : “);
  75.   38.
  76.           scanf(“%d”, &size);
  77.   39.
  78.        
  79.   40.
  80.           int array[size][size];
  81.   41.
  82.              
  83.   42.
  84.           snail_algorithms(size, array)   ;
  85.   43.
  86.        
  87.   44.
  88.           for(i = 0; i < size; i++) {
  89.   45.
  90.               for(j = 0; j < size; j++)
  91.   46.
  92.                   printf(“%dt, array[i][j]);
  93.   47.
  94.              
  95.   48.
  96.               putchar(n);
  97.   49.
  98.           }
  99.   50.
  100.       }
  101.   51.
  102.        
  103.   52.
  104.       /*********************************************************************************************
  105.   53.
  106.       * FUNCTION : array_rotation
  107.   54.
  108.       * DESCRIPTION : 행렬의 방향을 90도 바꾸어 주는 함수
  109.   55.
  110.       *********************************************************************************************/
  111.   56.
  112.       void array_rotation(int size, int (*array)[size])
  113.   57.
  114.       {
  115.   58.
  116.           int i, j;
  117.   59.
  118.           int array_tmp[size][size];
  119.   60.
  120.        
  121.   61.
  122.           // array copy
  123.   62.
  124.           for(i = 0; i < size; i++)
  125.   63.
  126.               for(j = 0; j < size; j++)
  127.   64.
  128.                   array_tmp[i][j] = array[i][j];
  129.   65.
  130.        
  131.   66.
  132.           // rotate
  133.   67.
  134.           for(i = 0; i < size; i++)
  135.   68.
  136.               for(j = 0; j < size; j++)
  137.   69.
  138.                   array[i][j] = array_tmp[j][size – i – 1];
  139.   70.
  140.              
  141.   71.
  142.       }
  143.   72.
  144.        
  145.   73.
  146.       /*********************************************************************************************
  147.   74.
  148.       * FUNCTION : snail_algorithms
  149.   75.
  150.       * DESCRIPTION : 달팽이 알고리즘. 행렬의 숫자 배열을 달팽이 껍질처럼 입력한다.
  151.   76.
  152.       *********************************************************************************************/
  153.   77.
  154.       void snail_algorithms(int size, int (*array)[size])
  155.   78.
  156.       {
  157.   79.
  158.           int first;
  159.   80.
  160.           int number;
  161.   81.
  162.           int size_tmp;
  163.   82.
  164.           int i, j, k;
  165.   83.
  166.        
  167.   84.
  168.           first = 0;
  169.   85.
  170.           number = 1;
  171.   86.
  172.           k = 0;
  173.   87.
  174.        
  175.   88.
  176.           for(i = 0; i <= ceil(size / 2); i++) {
  177.   89.
  178.               for(j = 0; j < 4; j++) {
  179.   90.
  180.                   if(first != 0)  // 행의 첫부분인지 검사
  181.   91.
  182.                       k = i + 1;
  183.   92.
  184.                   else   
  185.   93.
  186.                       k = i;
  187.   94.
  188.        
  189.   95.
  190.                   if(j % 3 == 0 && j != 0)        // 3번의 로테이션 마다 사이즈가 1씩 줄어든다.
  191.   96.
  192.                       size_tmp = size – i – 1;
  193.   97.
  194.                   else
  195.   98.
  196.                       size_tmp = size – i;
  197.   99.
  198.                  
  199.  100.
  200.                   for(; k < size_tmp; k++) {  // 실제적으로 배열에 값을 입력하는 부분
  201.  101.
  202.                       array[i][k] = number;
  203.  102.
  204.                       number++;
  205.  103.
  206.                       first = 1;
  207.  104.
  208.                   }
  209.  105.
  210.               array_rotation(size, array);
  211.  106.
  212.               }
  213.  107.
  214.               first = 0;
  215.  108.
  216.           }
  217.  109.
  218.       }

XBb3Eb0blW.c

BACKWARD_WARNING..

 컴파일 도중 이상한 경고가 발생했다…

#warning This file includes at least one deprecated or antiquated header.
Please consider using one of the 32 headers found in section 17.4.1.2 of the
C++ standard. Examples include substituting the <X> header for the <X.h>
header for C++ includes, or <iostream> instead of the deprecated header
<iostream.h>. To disable this warning use -Wno-deprecated.

 내용인즉…

 iostream.h 를 쓰고 싶다면 -Wno-deprecated 옵션과 함께 써라…하는 내용이다.

 왜 그럴까…답은 표준에 있었다.

 iosteram.h 는 표준헤더가 아니다.

 표준헤더가 아니라서 컴파일러가 경고를 알려주는 것이다.

 표준헤더 사용법은…

 

(Language : cpp)
  1. using namespace std;
  2.  
  3. #include <iostream>

라고 입력하면 된다.

이렇게 입력하니 문제가 해결되었다.

#include 사용시 생기는 에러

 유닉스 프로그래밍책을 보고 열심히 따라 치는중….이상한 에러를 발견했다.

 
#include <sys/filio.h> – 그런파일을 찾을 수 없음.

 분명히 책에는 이렇게 입력하라고 나와있었다.

 하지만 컴파일을 하니 발생하는 에러…

 무엇이 문제일까…답은 금방 찾았다.

 sys/filio.h 파일은 Sun 혹은 Unix 에서 사용하는 파일이라고 한다.

 해결책은
#include <sys/filio.h> ->
#include <sys/ioctl.h> 으로 바꾸어주면 된다. 혹은…

 

(Language : c)
  1. ifdef HAVE_SYS_FILIO_H
  2. #include <sys/filio.h>
  3. #endif
  4. #include <sys/ioctl.h>

이렇게 하면 된다.