如何计算有多少是最大的数组中的? [英] How to count how many maximum are in an array?
问题描述
我有这个功能,我想你帮忙找,如果有一个以上的最大值。如果有多个最大存在,我想打印没有独特的最大值。
下面的成功code发现的最大,但计数器,这countes最大出现多少次不能正常工作,我得到这个消息:
建议在C左右空身括号
块引用>INT find_max(INT B〔N] [N])
{
INT最大= B [0] [0];
INT X,Y;
INT计数器= 0;
诠释一个= 0,V = 0,C = 0;
为(X = 0; X&下; N; X ++)
{
对于(Y = 0; Y< N; Y ++)
{
如果(MAX< B [X] [Y])
{
最大= B [X] [Y]
A = X;
V =ÿ;
}
}
} C =((A * 10)+ V);
为(X = 0; X&下; N; X ++)
{
对于(Y = 0; Y< N; Y ++)
{
如果(最大值);
{
反++;
}
}
}
如果(计数器→1)
的printf(无最大潮头);
其他
返回℃;
}
解决方案我相信问题(至少一个问题)是以下行:
如果(最大值);
如果最大值不为零,那么这将始终是真的。我想你想是这样的:
如果(B [X] [Y] ==最大值)
(注意:这两个比较和去除';')
该警告是(我怀疑)从即将到来的;你的如果之后,这个终止如果和下一节{...}始终执行,它不与相关联的若
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; }
解决方案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.
这篇关于如何计算有多少是最大的数组中的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!