Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
i have this function, and i want you're help to find if there is more than one maximum i want to print:
C#
No unique max.


What I have tried:

C#
int find_max(int b[N][N])
{
   nt 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((b[x][y]) == max);
           counter++;
       }
   }
 if(counter>1)
   printf("no uniqe max");
else
   return c;
}
Posted
Updated 12-Jun-16 16:36pm
v6
Comments
George Jonsson 12-Jun-16 18:52pm    
printf returns the number of characters written, so this line will be confusing for you.
return(printf("No unique max."));
George Jonsson 12-Jun-16 19:53pm    
After your change in the code it will not compile, because all paths doesn't have a return value.
Patrice T 12-Jun-16 19:45pm    
Give an example with more than 1 maximum
molan77 12-Jun-16 20:03pm    
there is many examples , why ?
Patrice T 13-Jun-16 0:39am    
Put an example in question.
Use Improve question to update your question.
So that everyone can pay attention to this information.

1 solution

First of all, there is exactly one maximum. More exactly, an arbitrary functions which may have infinite number of points, which values have relationship of order defined, may or may not have a maximum, but for the arrays, which are have finite number of elements, there is one maximum. What is multiple? The set of array indices on which the maximum value of element is achieved. How to count them?

One obvious solution is: in first loop (in your case, this is a nested loop) you find the maximum value itself. When it is done, traverse all the values again in the similar nested loop, and count the number of cases (combination of x, y pairs of indices) where the maximum is achieved.

I hope the bug in your existing algorithm is quite obvious. Let's see: when you assign new value to max, you always increment the counter. But, in general case, the value of max is not equal to the maximum value yet, so you increment the counter more times that you really should.

As I can see, you have quote enough knowledge to fix your code using my suggestion. Please do it.

—SA
 
Share this answer
 
v2
Comments
molan77 12-Jun-16 18:59pm    
i want to ask, this function return int, can i return this sentense: No unique max.
if i found moe than one max ?
Sergey Alexandrovich Kryukov 12-Jun-16 22:32pm    
Why? Do yourself a favor, write a function returning number. And the calling number will output the string.
It's up to you though. Return whatever you want. Found points where the maximum is achiever — that's all what matters. Do whatever you want with it.
—SA
molan77 12-Jun-16 22:38pm    
ok but i am having this problem where i get the comment:"suggest braces around empty body in c", in the second loop
Sergey Alexandrovich Kryukov 12-Jun-16 22:57pm    
I don't see wring braces. I can see the following bug:

if((b[x][y]) == max); // ';' should be removed
counter++;

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900