0

An assignment in the K&R C Programming 2nd edition says that I have to write a program to print a histogram of the lengths of words in its input. I believe I know how to do so, but when running a test program on arrays, which I have only just learned about, all I get is "8", no matter what I input. This is the program so far:

#include <stdio.h>

/* write a program to print a histogram
of the lengths of words in its input */
main()
{
      int wl[11];
      int cc, c;

      while ((c=getchar()) != EOF);
      {
            if (c != ' ')
               ++cc;
            if (c == ' ' && cc == '1')
            {
               ++wl[0];
               c = 0;
            }
       putchar(wl[0]);
      }
}

It may be just because I'm a total beginner in programming, but I honestly cannot see where I went wrong here. Any help would be appreciated.

4

3 に答える 3

2

Start by initializing your variables:

int wl[11] = {0};
int cc = 0;

By default, memory contains garbage in C.

EDIT: Beyond the Obvious

  • the comparison cc == '1' does not do what you expect. It should probably be cc == 1
  • my guess is that ++wl[0] should be ++wl[cc]. This assumes a maximum word length of 11.
  • c = 0 should be resetting cc instead - c is the current character, cc is the current word length
  • putchar needs a character not an int so you will need to solve that as well

Good start though.

于 2012-07-08T00:58:41.947 に答える
1

ここには多くの問題があります。

main()

当時は戻り値の型を省略するのが一般的でしたが、今はそうではありません。おそらく、これを次のように書きたいと思うでしょう:

int main() 

その代わり。

{
      int wl[11];

@D.Shawleyがすでに指摘しているように、これにはガベージが含まれます。

      int cc, c;

彼が指摘するのを忘れていたのは、これらもそうなるということでした. これは、 ではあまり問題になりませんが、 では重要cですcc

      while ((c=getchar()) != EOF);
      {
            if (c != ' ')
               ++cc;

ここでは をインクリメントccしていますが、最初に既知の値に設定することはありません。

            if (c == ' ' && cc == '1')

'1'私はあなたが本当にここにいたくないと推測しています1. つまりcc、 digit と比較するの'1'ではなく、値 1 と比較します。

            {
               ++wl[0];
               c = 0;

無害ですが、ゼロ化cはここではあまり効果がありませんcc。あなたが本当に何をしようとしているのかはわかりません。おそらく、どちらもインクリメントしたくないでしょうwl[0]

            }
       putchar(wl[0]);

おそらくここでも必要ありませんputchar。おそらく int として書き出すwl[0]か、(ヒストグラムを作成するために) '*' などのカウントとして使用して出力したいと思うでしょう。

于 2012-07-08T01:06:46.757 に答える
0

ここではargcとargvを使用します(これはCブックで調べてください)。これの良いところは、単語数が表示され、入力を自分で処理する必要がないことです(OSと標準ライブラリに処理させてください)。サイズ配列(ヒストグラムバケット)をmallocすることもできますが、英語の単語の大部分を取得する「合理的な」仮定を行うこともできます(ドイツ語ではないことを願っています:-))。

#include <string.h>
#include <stdio.h>

// assume longest word is "Supercalifragilisticexpialidocious"    
#define MAX_WORD_LENGTH    (35)

int main(int argc, char* argv[])
{
  size_t sizes[MAX_WORD_LENGTH] = {0};

  for (int i=0; i<argc; ++i)
  {
    size_t len = strlen(argv[i]);

    if (len < MAX_WORD_LENGTH)
    {
      ++sizes[len]; // increment bucket
    }
    else
    {
      ++sizes[0]; // too long, put in default bucket
    }
  }

  // print the buckets
  for (int i=1; i<MAX_WORD_LENGTH; ++i)
  {
    printf("%d ", sizes[i]); 
  }

  // print the default bucket
  printf("%d", sizes[0]);
}
于 2012-07-08T01:48:53.203 に答える