first index to matrix is ignored

74 views Asked by At

I am trying to see if a 4x4 matrix is symmetric, and below is what i have so far. Upon compiling I receive the message:

pExam3p2.c:12:13: warning: expression result unused [-Wunused-value]  
                                if (Num[r,c]==Num[c,r]){  
                                        ^
pExam3p2.c:12:23: warning: expression result unused [-Wunused-value]
                                if (Num[r,c]==Num[c,r]).

I thought that adding an else statement would resolve this issue. But it does not. Any ideas is greatly appreciated. Thank you!

#include <stdio.h>
char isSymm (int Num[][4], int rows, int cols){
    int r,c, count=0;

    for (r=0; r<rows; r++){
        for (c=0; c<cols; c++){
            if (Num[r,c]==Num[c,r]){
                count=count+1;
            }
            else{
                count=count;
            }
        }
    }
    if (count==16){
        return 'y';
    }
    else {
        return 'n';
    }
}

int main (void){
    int Num[4][4];
    int c;
    int r;
    int size =4;

    for (r=0;r<size; r++){
        for (c=0; c<size; c++){
            printf("Enter your number: ");
            scanf("%d", &Num[r][c]); //NOTE THE &...
        }
    }

    char result= isSymm(Num, 4, 4);
    printf("%c", result);
}
5

There are 5 answers

0
Achal On

The problametic statement is Num[r,c]==Num[c,r] & why its so, its informed by compiler when you run gcc -Wall -Wstrict-prototypes -Werror test.c as

error: left-hand operand of comma expression has no effect [-Werror=unused-value]

In Num[r,c] there is comma operator between r and c and comma operator property is that it will solve all argument but consider only right hand operands, so Num[r,c] results in Num[c] and now you are comparing pointers Num[c] and Num[r] which will not yield in correct result.

You wanted to check if matrix is symmetric or not for that this

Num[r,c]==Num[c,r] ==> Num[r][c] == Num[c][r]

And since Num is symmetric matrix. You should calculate number of rows so that it works for any array, not hard coded like size=4.

size=4; ==> size = sizeof(Num)/sizeof(Num[0]);

And while comparing Num[r][c] and Num[c][r] the else part is useless, better remove it.

else{ /* not needed as if if block is false count value doesn't need to change */
    count=count;
}
0
Some programmer dude On

Num is not a "multi-dimensional" array (C doesn't have those), it's an array of arrays. So to get an element of it you need to do e.g. Num[r][c].

To explain what happens with Num[r,c], you need to learn about the comma operator.

The comma operator evaluates both sub-expressions, and throw away the result of the left-hand expression and the result is the result of the right-hand expression. So with r,c both r and c are evaluated, and then the result of r is discarded and the result of r,c is c.

That means your expression Num[r,c] is really Num[c]. And Num[c] is an array, which decays to a pointer to its first element, i.e. &Num[c][0], and you compare those two pointers.


On an unrelated note,

else{
    count=count;
}

is practically worthless, and you could remove it completely.

0
Yunnosch On

In order to access an array of arrays use

Num[r][c]

The , operator which you used does evaluate the part before the , but ignores the result. The result of the operator is the value of the second evaluated expression, the one after the ,.
That is why the compiler tells you that r is ignored.

0
Gor Asatryan On

Change (Num[r,c]==Num[c,r]) to (Num[r][c]==Num[c][r])

Also remove else statement.

else{
    count=count;
}
0
anoopknr On

An element in a two-dimensional array is accessed by using the subscripts, i.e., row index and column index of the array. For example :-

int val = a[2][3];

The above statement will take the 4th element from the 3rd row of the array.

So in your function char isSymm (int Num[][4], int rows, int cols) some changes are to be made:-

char isSymm (int Num[][4], int rows, int cols){
    int r,c, count=0;

    for (r=0; r<rows; r++){
        for (c=0; c<cols; c++){
            if (Num[r][c]==Num[c][r]){        // not  Num[r,c]==Num[c,r]
                count=count+1;
            }                                 // not needed count=count; else statement.
        }
    }
    if (count==16){
        return 'y';
    }
    else {
        return 'n';
    }
}

The comma-operator evaluates both sub-expressions, and throw away the result of the left-hand expression and the result is the result of the right-hand expression. So with r,c both r and c are evaluated, and then the result of r is discarded and the result of r,c is c.Here it becomes Num[c] which is invalid since Num is 2-Dimentional array.