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);
}
Numis 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,cbothrandcare evaluated, and then the result ofris discarded and the result ofr,cisc.That means your expression
Num[r,c]is reallyNum[c]. AndNum[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,
is practically worthless, and you could remove it completely.