Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
405 views
in Technique[技术] by (71.8m points)

c - How to count how many maximum are in an array?

I have this function, and I want your help to find if there is more than one maximum. If more than one maximum exists, I want to print "no unique max".

The code below successfully finds the maximum, but the counter, which countes how many times a maximum appears is not working and I get this message:

suggest braces around empty body in c

int find_max(int b[N][N])
{
   int max = b[0][0];
   int x,y;
   int counter=0;
   int a=0,v=0,c=0;
   for (x = 0; x < N; x++)
   {
       for (y = 0; y < N; y++)
       {
           if (max < b[x][y])
           {
               max = b[x][y];
                a=x;
                v=y;
           }
       }
   }

    c=((a*10)+v);
    for (x = 0; x < N; x++)
   {
       for (y = 0; y < N; y++)
       {
           if(max);
           {
           counter++;
           }
       }
   }
 if(counter>1)
   printf("no uniqe max");
else
   return c;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I believe the problem (at least one problem) is the following line:

if(max);

If max is not zero, then this will always be true. I think you want something like:

if(b[x][y] == max)

(note: both the comparison and the removal of the ';')

The warning is (I suspect) coming from the ';' after your 'if', this terminates the if and the next section '{...}' is always executed, it is not associated with the if.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...