matrix

Im having trouble with getting this function to work. It is supposed to return true if 3 elements in row in the matrix are the same(vertical or horizontal). Can somebody tell me what im doing wrong.

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
bool compare(int matrix[][NUM], int SIZE)
{
    for (int i = 0; i < NUM; i++)
    {
        for (int j= 0; j < NUM; j++)
        {
            if (j == i)
            {
                for (int j= 0; j < NUM; j++)
                {
                    if (j==i)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
        }
                
    }
        
        
        return false;
}
Last edited on
first i dont see u comparing any elements. i only see comparison of indexes. i == j .

when u say
It is supposed to return true if 3 elements in row in the matrix are the same
do you mean if 3 different elements occur more than once?
no if 4 would 3 times in a row in the matrix vertical or horizontal

like [3 4 4 4 5]
Use this.

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
bool compare(int matrix[][SIZE], int SIZE)
{
    for(int y = 0; y < SIZE; y++)
    {
        for(int x = 0; x < SIZE; x++)
        {
            if(x ==y)   //at diagonal element
            {
           
//check for any element accors the diagonal row and diagonal column that repeats itself 3 times.
                for(int i = 0; i < SIZE; i++)  //this can be a row or column, depends on the if statments below
                {
                    int rowCount = 0, colCount = 0;
                    for(j = 0; j < SIZE; j++)   //search the line row or column for that matrix[i] and count its occurance
                    {
                        if(matrix[i][y] == matrix[j][y])    //search row, NOTE that y is constant to mean the same row
                            ++rowCount;
                        if(matrix[x][i] == matrix[x][j])    //search col
                            ++colCount;
                    }
                    if(rowCount == 3 || colCount == 3) //no need to explain
                        return true;
                }
            }
        }
    }
    return false;
}


Last edited on
did this work for you? because it always returns true
Topic archived. No new replies allowed.