0

The problem is to find symmetricity in a matrix of characters(char logo[N][N]) where logo contains some characters(only 0 or 1).

Note that, in the output of the code, once i get all the 4 logo elements as 0 i.e. if(0==0==0==0), then else code is also executed.

I have implemented the symmetricity checking code and found that the only problem is occurring at the given block of code where else block is executed and it gives wrong result (NO).

code snippet:(N is size of the square matrix)

int YES=1;
for(j=N-1;j>((N-1)>>1);j--) /* symmetricity condition particular to the problem*/
    {
        for(k=0;k<((N-1)>>1);k++)
        {
            printf("%c %c %c %c\n",logo[j][k],logo[N-1-j][k],logo[j][N-1-k],logo[N-1-j][N-1-k]); // used this line for debugging
            if(logo[j][k]==logo[N-1-j][k]==logo[j][N-1-k]==logo[N-1-j][N-1-k])continue;   //checking symmetricity
            else  // here else is executed when all 4 logo elements are 0
            {
                YES=0;
                break;
            }
        }
    }
    (YES==1)?printf("YES\n"):printf("NO\n");

I expect the output to be "YES" as every time the 4 logo element is checked found to be same (1111 or 0000) but here you can see result is NO after i get 4 zeros.

1st line is number of test case, second line is N, then N*N matrix OUTPUT :1st line is number of test case, second line is N, then N*N matrix

you can see when all logo elements are 1111 then it reenters loop but after 0000 it breaks and prints NO.

Vagish
  • 2,520
  • 19
  • 32
  • 2
    `if(logo[j][k]==logo[N-1-j][k]==logo[j][N-1-k]==logo[N-1-j][N-1-k])` ? You will end up comparing previous comparison result `true or false` `(((logo[j][k]==logo[N-1-j][k])==logo[j][N-1-k])==logo[N-1-j][N-1-k])` – kiran Biradar May 16 '19 at 10:51
  • 8
    You can't really chain conditionals like that. Use AND (`&&`) instead. – 001 May 16 '19 at 10:51
  • 2
    With `0 == 0 == 0 == 0` you compare the true/false result of comparisons against the integers `0`. – Some programmer dude May 16 '19 at 10:54
  • And your final conditional expression is really bad style. Using it like that makes your code harder to read, understand and maintain. Please don't write code like that. – Some programmer dude May 16 '19 at 10:55
  • 1
    `YES` a variable name? nonsense. – KBlr May 16 '19 at 10:56
  • Possible [duplicate question](https://stackoverflow.com/questions/6961643/chaining-multiple-greater-than-less-than-operators). – Weather Vane May 16 '19 at 11:03
  • `==` doesn’t work like that - instead of `a == b == c == d`, use `a == b && b == c && c == d`. – John Bode May 16 '19 at 11:16
  • 1
    @Vagish: The elements of `logo` being compared are not 0. They are `'0'`. You can see this because the output of printing them with `%c` is “0” rather than the null character. Thus the expression is `'0'=='0'=='0'=='0'`, which evaluates as `1=='0'=='0'`, then `0=='0'`, then `0`. The text in the question correctly refers to the value as the character **0** initially, but its later reference to the value as 0 is incorrect or sloppy. – Eric Postpischil May 16 '19 at 11:49

2 Answers2

1

Replace your if-statement by this:

if((logo[j][k]==logo[N-1-j][k]) && 
   (logo[N-1-j][k]==logo[j][N-1-k]) &&
   (logo[j][N-1-k]==logo[N-1-j][N-1-k]))
  continue;   //checking symmetricity

Current implementation does the following:

logo[j][k]==logo[N-1-j][k] // returns TRUE
==logo[j][N-1-k]           // returns FALSE (I imagine that logo[j][N-1-k] does not equal TRUE
==logo[N-1-j][N-1-k]       // returns FALSE (I imagine that logo[N-1-j][N-1-k] does not equal FALSE
Dominique
  • 16,450
  • 15
  • 56
  • 112
  • Question is why `else` is getting executed. Please demonstrate how `if(0==0==0==0)` wont work. – Vagish May 16 '19 at 11:30
  • It looks like Vagish incorrectly down-voted this answer. Per my comment above, the elements being compared are `'0'`, not 0, and `'0'=='0'=='0'=='0'` evaluates to 0. – Eric Postpischil May 16 '19 at 11:59
  • @EricPostpischil: Vagish did not downvoted incorrectly: I've edited my answer after his comment :-). – Dominique May 16 '19 at 12:20
  • @Dominique: I believe Vagish voted on the premise that the expression evaluated to 1 (true), and therefore the `else` clause would not be executed—not that Vagish voted because this answer did not explain the evaluation. – Eric Postpischil May 16 '19 at 13:21
1
logo[j][k]==logo[N-1-j][k]==logo[j][N-1-k]

== is left associative. It returns 0 or 1. This means that the 2nd == will compare either 0 or 1 with logo[j][N-1-k]. You do not want that, so you need to insert correct operands by duplicating some of them and using && operator.

alinsoar
  • 15,386
  • 4
  • 57
  • 74
  • 1
    It looks like Vagish incorrectly down-voted this answer. Per my comment above, the elements being compared are `'0'`, not 0, and `'0'=='0'=='0'=='0'` evaluates to 0. – Eric Postpischil May 16 '19 at 11:59