-
Diagonal
So far I have managed to get diagonal of a matrix only like this, but I know there is a much simpler way, help! I a matrix is big this is useless.
Code:
for(a=0;a<3;a++)
{
for(b=0;b<3;b++)
{
if(a==0 && b==0)
{
printf("\n%d", mx[a][b]);
}
if(a==1 && b==1)
{
printf("\n%d", mx[a][b]);
}
if(a==2 && b==2)
{
printf("\n%d", mx[a][b]);
}
}
-
start by asking yourself what is a diagonal. (look at the math definition)
-
i know what it is, i just don't know how to print it
-
from your code... if a==b then you print... you have eliminated the 2 ifs. if you have to print only the components that have the indexes equal try to lose a cycle (a for). you don't need to go through all the comonents of the matrix
-
sorry for being stupid:) got it!
-
Code:
for(a=0;a<4;a++)
{
for(b=0;b<4;b++)
{
if(a==b)
{
b++;
printf("\n%d", mx[a][b]);
}
}
}
I want to print upper diagonal, and i managed, but it gives me some stupid number, memory trash or something, dont know why, other than that everything works.
-
lose the second for along with b and print mx[a][a]