# 1.6.1. 실습 예제 2개

<p class="callout info">**실습 전 이론**</p>

**히스토그램 - 표로 되어 있는 도수 분포를 정보 그림으로 나타낸 것이다.**

[![image.png](https://www.ochidstudy.com/uploads/images/gallery/2025-11/scaled-1680-/v7kd8Kl246VeBR1d-image.png)](https://www.ochidstudy.com/uploads/images/gallery/2025-11/v7kd8Kl246VeBR1d-image.png)

**히스토그램에 대한 정의를 잘 몰라서 찾아봤다. 근데 어떤식으로 진행하면 될지에 대한 정보까지 봐버려서; 좀 아쉽네**

**\* 코드는 안보고 어떤식으로 그래프를 표현하는지에 대한 정보를 봤다.**

<p class="callout info">**실습 예제**</p>

##### **1번 문제 입력된 단어의 길이에 대한 히스토그램을 작성하는 프로그램을 작성하라**

```c
#include <stdio.h>
#define IN 1
#define OUT 0
#define UPPER 10

int main(){
    int c, i;
    int ndigit[30];
    int wordLength = 0;
    int state;

    /*배열 안에 요소들을 0으로 지정해주나봄; 이거 일일히 해야되는구나; */
    for (i = 0; i < UPPER; ++i){
        ndigit[i] = 0;
    }

    state = IN;
    while ((c = getchar()) != EOF){
        if (state = IN && c != '\n' && c != '\t' && c != ' '){
            ++wordLength; 
        }
        else {
            ++ndigit[wordLength];
            wordLength = 0;
            state = OUT;
        }

    }
    printf("단어의 길이가 1인 단어의 갯수는 %d\n", ndigit[1]);

}
```

**한번에 만들기는 너무 복잡해서 일단 테스트용으로 만들었다. 1개 길이의 단어만 입력하고 출력을 테스트했다.**

[![image.png](https://www.ochidstudy.com/uploads/images/gallery/2025-11/scaled-1680-/uYmhEROT3FVrAiup-image.png)](https://www.ochidstudy.com/uploads/images/gallery/2025-11/uYmhEROT3FVrAiup-image.png)

**정상적으로 동작하는 것을 확인**

```c
#include <stdio.h>
#define IN 1
#define OUT 0
#define UPPER 10

int main(){
    int c, i;
    int ndigit[30];
    int wordLength = 0;
    int text;
    int state;
    int index;

    /*배열 안에 요소들을 0으로 지정해주나봄; 이거 일일히 해야되는구나; */
    for (i = 0; i < UPPER; ++i){
        ndigit[i] = 0;
    }

    state = IN;
    while ((c = getchar()) != EOF){
        if (state = IN && c != '\n' && c != '\t' && c != ' '){
            ++wordLength; 
        }
        else {
            ++ndigit[wordLength];
            wordLength = 0;
            state = OUT;
        }

    }

    for (index = 0; index < UPPER; ++index){
        printf("[%d]", index);
        for (text = 0; text < ndigit[index]; ++text){
            printf("=");
        }
        printf("\n");
    }
    
}
```

**위 테스트를 통해 길이 별 단어의 갯수를 파악해 = 문자를 반복하여 출력해서 그래프 모양으로 만들어줬다.**

[![image.png](https://www.ochidstudy.com/uploads/images/gallery/2025-11/scaled-1680-/Co9NzNu45LWX1Dn8-image.png)](https://www.ochidstudy.com/uploads/images/gallery/2025-11/Co9NzNu45LWX1Dn8-image.png)

##### **2번 문제 - 입력된 문자들의 사용빈도를 나타내는 히스토그램을 출력하는 프로그램을 작성하라**

**이건 ndigit\[\] 배열의 첨자를 미리 지정하지 않고 새로운 단어가 들어갈때마다 추가해줘야 될까?**

**그럼 입력 값이 이미 ndigit 배열 안에 들어가 있는지를 확인하는 코드 부터 짜봐야겠다.**

**그러려면 내가 입력한 단어를 배열안에 넣는 코드부터 짜야겠네**

**근데 getchar 는 한 글자씩 가져오는데.. 단어를 어떻게 뭉칠까..**

**배열을 통해 단어를 하나하나 첨자에 넣어야하나.. 근데 첨자에 넣으면 그걸 합치는 방법은 아직 안가르쳐줬는데..**

**\* 아 지금보니까 단어의 사용빈도가 아니라 문자의 사용빈도네; a 몇번썼는지 b 몇번썼는지..**

**이러고 생각보다 쉽다고 생각했는데 이거 좀 생각해보니까 문자를 ascii로 치환해서 하는게 나을것같다.**

[![image.png](https://www.ochidstudy.com/uploads/images/gallery/2025-11/scaled-1680-/P8iBoPpDuMvB4kTg-image.png)](https://www.ochidstudy.com/uploads/images/gallery/2025-11/P8iBoPpDuMvB4kTg-image.png)

**조금 간결하게 하기 위해 소문자만 사용해서 하자**

**소문자의 갯수는 26개 그러면 ndigit\[26\]으로 세팅해두고 시작하자**

```c
#include <stdio.h>


int main(){
    int c, i;
    int ndigit[26];
    int text;
    int word;
    
    for (i = 0; i < 26; ++i){
        ndigit[i] = 0;
    }

    while((c = getchar()) != EOF){
        printf("%d\n", c);
    }

}
```

**테스트용 코드 a 를 입력하고 Enter를 입력해주었다.**

[![image.png](https://www.ochidstudy.com/uploads/images/gallery/2025-11/scaled-1680-/FZrK85fOTvd95aEl-image.png)](https://www.ochidstudy.com/uploads/images/gallery/2025-11/FZrK85fOTvd95aEl-image.png)

**a 는 ascii에서 97 이고 ascii에서 10은 개행이다. Enter 키가 10으로 출력됐다고 보면 된다.**

**ndigit 배열이 0부터 25까지 존재하니까 c에 들어오는 값에서 97씩을 빼고 넣어준 뒤 갯수를 하나씩 늘려주면 되겠지**

**근데 개행까지 들어가니까 입력 받은 값이 소문자 알파벳인지 식별하는 코드도 짜주자**

```c
while((c = getchar()) != EOF){
        if(c >= 97 && c <= 122){
            c = c-'a';
            printf("%d\n", c);
        }
    }
```

**if 문으로 해결, 이제 들어오는 값의 ndigit 인덱스를 1씩 더해주면 되겠다.**

```c
#include <stdio.h>


int main(){
    int c, i, e;
    int ndigit[26];
    int index;
    int range;
    int alphabet;
    /*배열 안에 요소들을 0으로 지정해주나봄; 이거 일일히 해야되는구나; */
    
    for (i = 0; i < 26; ++i){
        ndigit[i] = 0;
    }
    index = 0;
    while((c = getchar()) != EOF){
        if(c >= 97 && c <= 122){
            index = c-'a';
            ++ndigit[index];
            index = 0;
        }
    }


    for (range = 0; range < 26; ++range){
        alphabet = range + 97;
        printf("\n[%d]", alphabet);
        for (e = 0; e < ndigit[range]; e++){
            printf("=");
        }
    }
    printf("%d\n", ndigit[0]);


}
```

**어느정도 만들어서**

[![image.png](https://www.ochidstudy.com/uploads/images/gallery/2025-11/scaled-1680-/WrL8VGUJX4gJFho5-image.png)](https://www.ochidstudy.com/uploads/images/gallery/2025-11/WrL8VGUJX4gJFho5-image.png)

**이런 느낌까진 오긴했는데... 97을 다시 어떻게 아스키에서 뜻하는 문자로 바꾸지..?**

**이후에 방법을 알면 저 부분만 고치면 될것같다.**

<p class="callout info">**실습 후 이론**</p>