Click here to Skip to main content
15,903,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey guys , i want to write a function (one function) that tells if a nxn matrix is symmetric 'or' anti symmetric , i had problems with 'return' . this would be easy if it was in 2 different functions but it's just One in this case . can you please help ? thanks . ( C language )

What I have tried:

C
int vrf_sym_anti(const int  **M,const int  n);
int vrf_sym_anti(const int  **M,const int  n)
{
    int i,j,c=0,sym=0,antisym=0;
    for(i=0; i<n; i++)
        for(j=0; j<n; j++)
        {
            if(M[i][j]!=(M[j][i] || -M[j][i]))
               c++;
               else
                if(M[i][j]==M[j][i])
                sym++;
                else
                    if((M[i][j]==-M[j][i]) && M[i][i]==0)
                    antisym++;
        }
 if(c!=0)
    return 0; ///Ni sym ni antsym
 if(sym==(pow(n,2)-n)/2)
    return 1; ///sym
 if(antisym==(pow(n,2)+2)/2)
    return -1; ///antiym
}
Posted
Updated 31-Dec-17 2:56am
v3
Comments
OriginalGriff 31-Dec-17 4:20am    
What problem?

And do yourself a favour: sort out your indentation!
Outh_Mane 31-Dec-17 7:46am    
It doesnt work , it always shows it's not sym or antisym
OriginalGriff 31-Dec-17 7:51am    
And?
What have you done to find out why?
Outh_Mane 31-Dec-17 8:54am    
i type in matrixs that i know their types , i use this function in a full code . but it's okay i found a solution to this probleme . i changed the conditions in "if" and it gave me the correct answer

You have an incomplete if clause at the end, which the compiler does not like:
C++
 if(c!=0)
    return 0; ///Ni sym ni antsym
 if(sym==(pow(n,2)-n)/2)
    return 1; ///sym
 if(antisym==(pow(n,2)+2)/2)
    return -1; ///antiym
// OK, now what to do if antisym is not equal to that expression?
// you need a final return statement here
// or better still use if ... else if ... else if ... else ...
 
Share this answer
 
This is just a guess, but ...
Replace this:
if(M[i][j]!=(M[j][i] || -M[j][i]))
With this:
if(M[i][j] != M[j][i] && M[i][j] != -M[j][i]))
when you OR two integers using || use treat them as boolean values: so
(M[j][i] || -M[j][i])
Will return true or false - and probably doesn't match your cell contents.
 
Share this answer
 
i did this and it works

int vrf_sym_anti(const int **M,const int n);
int vrf_sym_anti(const int **M,const int n)
{
int i,j,sym=0,antisym=0;
for(i=0; i<n; i++)
for(j=0; j<n; j++)
{
if(M[i][j]==M[j][i])
sym++;
else
if((M[i][j]==-M[j][i])&&(M[i][i]==0))
antisym++;
}
if(sym==n*n)
return 1; ///sym
else
if(antisym==(n*n)-n)
return -1; ///antiym
else
return 0; ///Ni sym ni antsym
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900