Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C数组练习的直观方法;这本书;。[附表1-13]_C_Arrays - Fatal编程技术网

C数组练习的直观方法;这本书;。[附表1-13]

C数组练习的直观方法;这本书;。[附表1-13],c,arrays,C,Arrays,我想根据用户的输入创建一个符号直方图,详细说明其段落的字数: 我试图找到每个单词的len,并将其记录在一个数组中(++表示每个单词是1-3个字母,3-5个字母,等等),然后将它们打印出来 /* Write a program to print a histogram of the lengths of words in its input. It is easy to draw the histrogram with bars, horizontal; a vertical oreientati

我想根据用户的输入创建一个符号直方图,详细说明其段落的字数:

我试图找到每个单词的len,并将其记录在一个数组中(++表示每个单词是1-3个字母,3-5个字母,等等),然后将它们打印出来

/* Write a program to print a histogram of the lengths of words in its input. It is easy to draw the histrogram with bars, horizontal; a vertical oreientation is more challenging */
#include <stdio.h>
int main()
{
    int wordCountHistogram[5];
    int len = -1;
    char c;
    while ((c = getchar()) != EOF) {
        ++len;
        if (c == '\t' || c == '\n' || c == ' '){
            if (0 <= len <= 3)
                ++wordCountHistogram[0];

            if (4 <= len <= 6)
                ++wordCountHistogram[1];

            if (7 <= len <= 8)
                ++wordCountHistogram[2];

            if (9 <= len <= 13)
                ++wordCountHistogram[3];

            if (len > 14)
                ++wordCountHistogram[4];

            /*if (12 <= len <= 14)
                wordCountHistogram[5]++;

            if (14 <= len < 15)
                wordCountHistogram[6]++;

            if (16 <= len < 17)
                wordCountHistogram[7]++;

            if (17 <= len < 18)
                wordCountHistogram[8]++;

            if (19 <= len < 9999)
                wordCountHistogram[9]++;
        */
            printf("%d", len);
            len = -1;
    }
}

    for(int i = 0; i < 5 ;i++){
        printf("%d \n",wordCountHistogram[i]);
        for(int n = wordCountHistogram[i];n >= 0;n--){//Histogram sizeof(array) / sizeof(array[0]))
            printf("+");}
        printf("\n");
    }

}
/*编写一个程序来打印输入中单词长度的直方图。它很容易用横条画出历史程序;垂直定向更具挑战性*/
#包括
int main()
{
int-wordCountHistogram[5];
int len=-1;
字符c;
而((c=getchar())!=EOF){
++len;
如果(c='\t'| | c='\n'| | c=''){

如果(0未初始化数组,则假定将使用值填充该数组。这可能会导致在ctrl+D打印“+”符号时数组中出现垃圾。我建议在声明时定义它:

int wordCountHistogram[5] = {0};
我做出了改变,同时也改变了你的评估:

if ((0 <= len) && (len  <= 3))
if((0几件事:

  • 您需要将直方图数组初始化为0:
    int wordCountHistogram[5] = {0};

  • 像<代码> 0的表达式,当我尝试打印数组(这只是单词LeN的计数——更像是一个计数——我正在获取内存地址)。你能显示你正在接收的错误吗?考虑使用数学而不是一堆代码< > < /Cord>语句:<代码> WordPosithI图[长度/ 4 ]。
  • 例如,或
    int spot=length/4;if(spot>4){spot=4}
    或类似的东西对任何人来说都不是有用的描述。是编译器错误吗?运行时错误吗?你能描述一下吗?缩小范围吗?谢谢你的提示,@user3386109。我已经更新了,使我的代码符合要求。
    c
    对于
    isspace
    /或者对于char,必须转换为无符号char。@AnttiHaapala:应该是
    int
    查看
    getchar()
    的结果。
    if ( isspace( c ) )
    {
      // update histogram
    }
    
    int bin( int len )
    {
      if ( 0 <= len && len <= 3 )
        return 0;
      if ( 4 <= len && len <= 6 )
        return 1;
      if ( 7 <= len && len <= 8 )
        return 2;
      ...
    }
    int main( void )
    {
      ...
      if ( isspace( c ) )
        ++wordCountHistogram[ bin( len ) ];
      ...
    }