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);
}
The problametic statement is
Num[r,c]==Num[c,r]
& why its so, its informed by compiler when you rungcc -Wall -Wstrict-prototypes -Werror test.c
asIn
Num[r,c]
there is comma operator betweenr
andc
and comma operator property is that it will solve all argument but consider only right hand operands, soNum[r,c]
results inNum[c]
and now you are comparing pointersNum[c]
andNum[r]
which will not yield in correct result.You wanted to check if matrix is
symmetric
or not for that thisNum[r,c]==Num[c,r]
==>Num[r][c] == Num[c][r]
And since
Num
is symmetric matrix. You should calculate number ofrows
so that it works for any array, not hard coded likesize=4
.size=4;
==>size = sizeof(Num)/sizeof(Num[0]);
And while comparing
Num[r][c]
andNum[c][r]
theelse
part is useless, better remove it.