简体   繁体   English

最大值始终设置为8

[英]Max Value always setting to 8

I was writing a program that takes the input, and then prints the maximum value of the frequency of each letter in it. 我正在编写一个接受输入的程序,然后输出其中每个字母的频率的最大值。 For this program, I have decided to take the input only as these 5 letters: a,b,c,d and e. 对于该程序,我决定仅将输入作为这5个字母:a,b,c,d和e。 The program goes like this. 该程序是这样的。

#include <stdio.h>

int main(){
    int i, c , nchar[5];
    int maxvalue;

   for(i=0;i<5;i++){
    nchar[i]=0;
   }

    /*COLLECTING AND SETTING THE DATA*/
    while((c=getchar())!=EOF){
        if (c=='a')
            nchar[0]++;

        else if (c=='b')
            nchar[1]++;

        else if (c=='c')
            nchar[2]++;

        else if (c=='d')
            nchar[3]++;

        else if (c=='e')
            nchar[4]++;

    }
    printf("%d",setMax(nchar,maxvalue));


}
int setMax(int a[5], int maxv){
    if ( a[0]> a[1] &&  a[0]> a[2] &&  a[0]>a[3] &&  a[0]> a[4])
        a[0]=maxv;

    else if ( a[1]> a[0] && a[1]> a[2] && a[1]> a[3] && a[1]> a[4])
        a[1]=maxv;

    else if ( a[2]> a[0] && a[2]> a[1] && a[2]> a[3] && a[2]> a[4])
        a[2]=maxv;

    else if ( a[3]> a[0] && a[3]> a[2] && a[3]> a[1] && a[3]> a[4])
        a[3]=maxv;

    else if ( a[4]> a[0] && a[4]> a[2] && a[4]> a[3] && a[4]> a[1])
        a[4]=maxv;

        return maxv;

}

Now, for example I write the input as 'aaabc', it should print the value 3 because the maximum frequency is of letter 'a' which is 3. But, it is printing the value 8. Not only this input, but anything I write as input, it always prints 8. Can someone tell me what mistake have I done? 现在,例如,我将输入写为“ aaabc”,它应该打印值3,因为最大频率是字母“ a”(即3)。但是,它打印的是值8。输入作为输入,它将始终打印8.有人可以告诉我我犯了什么错误吗?

You have your logic backward. 你的逻辑倒退了。

Instead of 代替

if ( a[0]> a[1] &&  a[0]> a[2] &&  a[0]>a[3] &&  a[0]> a[4])
    a[0]=maxv;

you need 你需要

if ( a[0]> a[1] &&  a[0]> a[2] &&  a[0]>a[3] &&  a[0]> a[4])
    maxv = a[0];

General Improvement 总体改进

Change the name of the function setMax() to getMax() and change its signature to: 将函数setMax()的名称更改为getMax()并将其签名更改为:

int getMax(int a[5]);

Change the usage to: 将用法更改为:

printf("%d", getMax(nchar);

And change the implementation to: 并将实现更改为:

int getMax(int a[5]){

   int maxv = 0;
    if ( a[0]> a[1] && a[0]> a[2] &&  a[0]>a[3] &&  a[0]> a[4])
        maxv = a[0];

    else if ( a[1]> a[0] && a[1]> a[2] && a[1]> a[3] && a[1]> a[4])
        maxv = a[1];

    else if ( a[2]> a[0] && a[2]> a[1] && a[2]> a[3] && a[2]> a[4])
        maxv = a[2];

    else if ( a[3]> a[0] && a[3]> a[2] && a[3]> a[1] && a[3]> a[4])
        maxv = a[3];

    else if ( a[4]> a[0] && a[4]> a[2] && a[4]> a[3] && a[4]> a[1])
        maxv = a[4];

    return maxv;
}

Update 更新

An updated version of getMax() that fixes the problem when two values are equal. 当两个值相等时, getMax()的更新版本解决了该问题。

int getMax(int a[5])
{
   if ( a[0] >= a[1] && a[0] >= a[2] &&  a[0] >= a[3] &&  a[0] >= a[4] )
      return a[0];

   // a[0] is not the max. it has has be a[1], a[2], a[3], or a[4]
   if ( a[1] >= a[2] && a[1] >= a[3] && a[1] >= a[4] )
      return a[1];

   // Similarly, the max has to be a[2], a[3], or a[4]
   if ( a[2] >= a[3] && a[2] >= a[4] )
      return a[2];

   // Similarly, the max has to be a[3] or a[4]
   if ( a[3] >= a[4] )
      return a[3];

   // At this point, a[4] has to be the max value.
   return a[4];
}

do maxval = 0 and just return a[0] a[1] etc which ever condition is satisfied. maxval = 0 ,只要满足条件就返回a [0] a [1]等。 Dont assign the value to maxval. 不要将值分配给maxval。

Or if you want to do like this, Here is the code for setMax() func: 或者,如果您想这样做,这是setMax()函数的代码:

int setMax(int a[5], int maxv){

int i=0;
maxval = a[0];
for (i=1;i<5;i++)
 if (maxval < a[i])
  maxval = a[i];
return maxval;
}

The variable maxvalue has an undefined value when you call setMax() . 调用setMax()时,变量maxvalue具有未定义的值。

This gives undefined behavior. 这给出了未定义的行为。

Also I don't think setMax() makes sense. 我也不认为setMax()有道理。 I think it should do return a[0] in the first if , and so on. 我认为它应该在第一个if return a[0] ,依此类推。

The variable maxvalue is uninitialized, and it's value will be indeterminate. 变量maxvalue初始化,其值将是不确定的。 Using it in calculations will lead to undefined behavior. 在计算中使用它会导致不确定的行为。

Besides, you always return it (or rather maxv ) from the setMax function without modifications , so you will always print the same value. 此外,您始终 无需修改就可以从setMax函数返回它(或者说是maxv ),因此您将始终打印相同的值。

As already pointed my Mr. Unwind and Mr. JP, the usage of maxvalue in current code invokes undefined behaviour , as you're using an uninitialized automatic variable value. 正如我的Unwind先生和JP先生所指出的那样,在当前代码中使用maxvalue引起未定义的行为 ,因为您正在使用未初始化的自动变量值。

Based on the usage of setMax() function in your code, I think, what you want to do is (are) 根据代码中setMax()函数的使用情况,我认为您想要做的是

  • Point 1 You don't need to define maxvalue in main() and pass that as a parameter to setMax() . 点1您无需在main()定义maxvalue并将其作为参数传递给setMax()

  • Point 2 Inside setMax() function, define a local maxvalue and change a[0]=maxv; 点2setMax()函数中,定义一个局部maxvalue并更改a[0]=maxv; and series to maxv = a[0]; 并与maxv = a[0];串联maxv = a[0]; and likewise. 同样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM