CHAPTER 8 
ARRAYS

[IMAGE: An Quarter-Size Version of The Joy of C Front Cover]
This chapter introduces arrays. Our focus is on declaring arrays, accessing their elements, and passing them as parameters. We introduce arrays with a program that reverses its input and show how to pass arrays as parameters by rewriting this program, building it on top of a pair of user-defined functions. We further illustrate arrays by writing several useful functions: one reads values into an array until it encounters a sentinel, another searches an array for a particular value, and the last reads values into an array using a technique known as insertion sort. The chapter concludes with a case study that makes use of almost all the C features we've seen so far: a program that produces a histogram of its input.

Jump to: [Previous Chapter | Next Chapter]


  1. USING ARRAYS---A PROGRAM TO REVERSE ITS INPUT
  2. PASSING ARRAYS AS PARAMETERS
  3. SOME EXAMPLE PROGRAMS USING ARRAYS
  4. CASE STUDY: A HISTOGRAM PRODUCER

Objectives


 
  • ÇÁ·Î±×·¥¿¡¼­ »ç¿ëµÇ´Â °ªµéÀº ÄÄÇ»ÅÍÀÇ ¸Þ¸ð¸®¿¡ ÀúÀåµÈ´Ù.
  • ÄÄÇ»ÅÍÀÇ ¸Þ¸ð¸®´Â ÁÖ¼Ò(address)¸¦ »ç¿ëÇÏ¿© accessÇÑ´Ù.
  • ƯÁ¤ °ªÀ» ÀúÀåÇÒ ¼ö ÀÖ´Â º¯¼ö´Â ¸Þ¸ð¸®¸¦ ÇÒ´ç ¹Þ´Â´Ù.
  • ƯÁ¤ º¯¼ö¿¡ ÇÒ´çµÈ ¸Þ¸ð¸®ÀÇ ÁÖ¼Ò´Â ºÒº¯ÀÌ°í ³»¿ë¸¸ ¹Ù²ð ¼ö ÀÖ´Ù.
  • C´Â º¯¼ö¿¡ ÇÒ´çµÈ ¸Þ¸ð¸® ÁÖ¼Ò¸¦ ÂüÁ¶ÇÒ ¼ö ÀÖ´Â operator (&: address-of operator)¸¦ Á¦°øÇÑ´Ù.
  • °¢ ÀÚ·áÇü(data type)¸¶´Ù »ç¿ëÇÏ´Â ¸Þ¸ð¸® ¾çÀÌ ´Ù¸£¸ç ÀÌ´Â ¶Ç ÄÄÇ»ÅÍ ±âÁ¾¸¶´Ù ´Ù¸¦ ¼ö ÀÖ´Ù.
  • º¯¼ö³ª ƯÁ¤ ÀÚ·áÇüÀÌ »ç¿ëÇÏ´Â ¸Þ¸ð¸® ¾ç(bytes)À» ¾Ë·Á¸é sizeof operator¸¦ »ç¿ëÇÑ´Ù.

  •  

     
     
     

    Array´Â ¼ø¼­¸¦ °®´Â µ¿ÀÏÇÑ ÇüÀÇ ÀÚ·áµéÀÇ ¸ðÀÓÀÌ´Ù. (An ordered collection of homogeneous data)

    ArrayÀÇ °¢ ¿ä¼Ò¸¦ element¶ó ÇÑ´Ù.

    Array´Â element type°ú size·Î Á¤ÀǵȴÙ.

    Array´Â (element Çϳª¸¦ ÀúÀåÇÒ ¼ö ÀÖ´Â ¸Þ¸ð¸®) * (array size) ¸¸Å­ÀÇ ¸Þ¸ð¸®¸¦ ÇÒ´ç¹Þ´Â´Ù.

    Array name (¹è¿­¸í)Àº ÇÒ´ç¹ÞÀº ¸Þ¸ð¸®ÀÇ ½ÃÀÛ ÁÖ¼Ò¿Í °°´Ù.
     
     

    ¿¹) 32 bit compile °æ¿ì

             int ary[5];         // integer 5°³¸¦ ÀúÀåÇÒ ¼ö ÀÖ´Â ¹è¿­ ary Á¤ÀÇ
             sizeof (int) ==> 4
             sizeof (ary) ==> 20
     

     

    ArrayÀÇ °¢ element´Â index¸¦ »ç¿ëÇÏ¿© ÂüÁ¶ÇÑ´Ù.

    C¿¡¼­ arrayÀÇ Ã¹¹ø° elementÀÇ index´Â 0ºÎÅÍ ½ÃÀÛÇÑ´Ù.
     

    »ç¿ë ¿¹) ÀÔ·ÂÀ» °Å²Ù·Î Ãâ·ÂÇÏ´Â ÇÁ·Î±×·¥ (revintp2.c)
    ÀÔ·Â: 10 20 18 68 1
    Ãâ·Â: 1 68 18 20 10

    À§ ÇÁ·Î±×·¥ÀÇ for-loop version: (revint.c)
     
     

    ´ÙÂ÷¿ø ¹è¿­ (Multi-dimensional array)

            int    ary1[10][10];       // not   ary1[10, 10];
            double ary2[10][10][10];
    
    
    
     

    Function argument·Î array¸¦ Àü´ÞÇÒ ¶§ array element¸¦ Á¤È®È÷ ÁöÁ¤ÇÒ ¼ö ÀÖµµ·Ï Á¤ÀÇÇÏ¿©¾ß ÇÑ´Ù.

    func(int ary[]) { . . . }        // func´Â 1Â÷¿ø ¹è¿­À» parameter·Î »ç¿ë
    func (int ary[10][10]) { . . . }
    func(int ary[][10]) { . . . }
    »ç¿ë ¿¹) ÀÔ·Â ¹ÝÀü ÇÁ·Î±×·¥ÀÇ º¯Çü (revint2.c, tabfill.c)
     
     

    ¹è¿­ÀÇ ÃʱâÈ­

    (Á¾·¡ÀÇ C¿¡¼­´Â static array¿¡¸¸ Àû¿ë °¡´ÉÇßÀ¸³ª ANSI C¿¡¼­´Â ±×·¯ÇÑ Á¦ÇÑÀÌ ¾ø¾îÁ³À½)
    int ary[5] = { 1, 2, 3, 4, 5 };
    int ary[] = { 1, 2, 3, 4, 5 };
    char ch[5] = { 'a', 'b', 'c', 'd', 'e' };
    int ary[3][3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    int ary[][3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    int ary[3][] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; // Wrong
    int ary[3][3] = {
                     { 1, 2, 3 },
                     { 4, 5, 6 },
                     { 7, 8, 9 }
                    };
    char ch[10][5] = {{ 'O', 'n', 'e' }, { 'T', 'w', 'o' }};
    
    
     

    ½Ç½À 1) n°³ÀÇ integer °ªÀ» ÀÔ·Â ¹Þ¾Æ array¿¡ ÀúÀåÇÏ°í ÀúÀåµÈ °ªµéÀÇ Æò±ÕÀ» printÇÏ´Â program ÀÛ¼º

    ½ÇÇà ¿¹:
               n = 3
               [1] = 10
               [2] = 20
               [3] = 30
               Average = 20
    
    
    
     

    ½Ç½À 2) ¹è¿­¿¡ ÀúÀåµÈ °ªÀ» Å©±â¼øÀ¸·Î Á¤·ÄÇÏ´Â ÇÁ·Î±×·¥ ÀÛ¼º

    Insertion sort ¹æ¹ý »ç¿ë: (isort.c, tabins.c)
     


    Assignment #6 (±â°£: 1ÁÖÀÏ)

    1¿¡¼­ 100 »çÀÌÀÇ ¼ö¸¦ ÀÔ·ÂÇÏ¿© °¢ ¹üÀ§ÀÇ ¼ö °¹¼ö¸¦ È÷½ºÅä±×·¥À¸·Î Ãâ·ÂÇÏ´Â ÇÁ·Î±×·¥ ÀÛ¼º (-1À» ÀÔ·ÂÇϸé ÀÔ·Â Á¾·á)

    ÀÔ·Â ¿¹)
    89  87  56  89  67  78  79  80 
    85  97 100 100  23  45  12  14 
    87  88  84  84  84  84  77  77 
    72  73  68  69  69 100 100  11 
    75  71  71  72  79  84  71  72 
    70  74  75  73  71  79  72  55 
    55  68  65  67  72  71  75  73 
    71  74  71  71  78  79  80 100 
    91  92  66  61  61  57  57  65 
    71  74  78  78  78  78  78 -1
    Ãâ·Â ¿¹)
      0-  9 |                         
     10- 19 |**                        (3)
     20- 29 |*                         (1)
     30- 39 |                         
     40- 49 |*                         (1)
     50- 59 |***                       (5)
     60- 69 |*******                   (11)
     70- 79 |************************* (37)
     80- 89 |********                  (13)
     90- 99 |**                        (3)
    100-100 |***                       (5)

    [ Table Of Contents | Previous Chapter | Next Chapter]