# 1.6 数组

这一节会有数组的使用和多路判断（if else）

<figure><img src="https://labspc.com/wp-content/uploads/2024/01/1705654802-word-image-262-1.png" alt=""><figcaption></figcaption></figure>

说实话我感觉 练习 1-13 难度还是有一点点大。

{% code title="1.6.1 print-input.c" lineNumbers="true" %}

```c
#include <stdio.h>
#define MAXHORI 15 // horizontal histogram
#define MAXWORD 11
#define IN 1
#define OUT 0

int main()
{
    int c, i, nc, state;
    int len; // length of each bar in histogram
    int maxvalue; // maximum value for wl[]
    int ovflow; // number of overflow words
    int wl[MAXWORD]; // word length counters
    state = OUT;
    nc = 0; // number of chars in a word
    ovflow = 0;

    // initialize wl[]
    for (i = 0; i < MAXWORD; ++i)
    {
        wl[i] = 0;
    }

    while ((c = getchar()) != EOF)
    {
        ++nc;
        if (c == ' ' || c == '\n' || c == '\t')
        {
            state = OUT;
            if (nc > 0)
            {
                if (nc < MAXWORD)
                {
                    ++wl[nc];
                }
                else
                {
                    ++ovflow; // overflow words
                }
            }
            nc = 0;
        }
        else if (state == OUT)
        {
            state = IN;
            nc = 1;
        }
        else
        {
            ++nc; // inside a word 正在读取一个单词的内部
        }
    }

    maxvalue = 0;
    // 过找出最大值，我们可以确定直方图的高度，使得直方图的高度与最长的单词的长度相对应
    for (i = 1; i < MAXWORD; ++i)
    {
        if (wl[i] > maxvalue)
        {
            maxvalue = wl[i];
        }
    }

    for (i = 1; i < MAXWORD; ++i)
    {
        printf("%5d - %5d : ", i, wl[i]);
        if (wl[i] > 0)
        {
            if ((len = wl[i] * MAXHORI / maxvalue) <= 0)
            {
                len = 1;
            }
        }
        else
        {
            len = 0;
        }

        while (len > 0)
        {
            putchar('*');
            --len;
        }
        putchar('\n');
    }
    if (ovflow > 0)
    {
        printf("There are %d words >= %d\n", ovflow, MAXWORD);
    }
    return 0;
}
```

{% endcode %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://labspc.gitbook.io/cnippets/chap1.-dao-yan/1.6-shu-zu.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
