简体   繁体   中英

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. 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?

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:

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.

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. Dont assign the value to maxval.

Or if you want to do like this, Here is the code for setMax() func:

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() .

This gives undefined behavior.

Also I don't think setMax() makes sense. I think it should do return a[0] in the first if , and so on.

The variable maxvalue is uninitialized, and it's value will be indeterminate. 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.

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.

Based on the usage of setMax() function in your code, I think, what you want to do is (are)

  • Point 1 You don't need to define maxvalue in main() and pass that as a parameter to setMax() .

  • Point 2 Inside setMax() function, define a local maxvalue and change a[0]=maxv; and series to maxv = a[0]; and likewise.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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