2D array finding the most frequent element

Oct 31, 2016 at 10:23am
I am doing a homework about greyscale bitmap and I am going to find the most frequently occuring greyscale level in an image.
value 0=black
255=pure white

however my code will only show the value of 255
am I writing sth wrong of the for loop or the variable of the int?
please help thanks!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
int get_most_frequent(int workspace[][WORKSPACE_DIM], int image_dim)
{
    int greyscale=0;
    int Count=0;
    int tempgreyscale;
    int tempCount;

    for(int y=0; y<image_dim-1; y++){
        for(int x=0; x<image_dim-1; x++){
            tempgreyscale = workspace[y][x];


            for(int k=0; k<image_dim-1; k++){
                for(int p=0; p<image_dim-1; p++){
                    if(workspace[k][p]==tempgreyscale){
                        tempCount=0;
                        tempCount++;}
                       
                }
            }


            if(tempCount>Count){
                greyscale=tempgreyscale;
                Count=tempCount;
            }
            if(tempCount=Count){
                if(tempgreyscale>=greyscale){
                    greyscale=tempgreyscale;
                    Count=tempCount;
                }
            }

        }
    }



    return greyscale;
}
Oct 31, 2016 at 12:32pm
Look at line 27:

if(tempCount=Count){

You're using the assignment operator, when you presumably intended this to be a comparison.
Nov 1, 2016 at 2:23am
so do I need to change to if(tempCount==Count){ ?
I have tried but it still cannot find the right value
Nov 1, 2016 at 3:41am
oh I figered it out. The problem is where the tempCount=0; is placed.

my original code, the tempCount=0; is placed at line 16, so the tempCount will keep ++ without return to 0 when start counting another element.



tempCount=0; should place at line 11, so when it start counting the another element, the tempCount will return to 0.

Also thankyou MikeyBoy pointing out another problem.
Topic archived. No new replies allowed.