# Diagonal

This is a discussion on Diagonal within the C Programming forums, part of the General Programming Boards category; So far I have managed to get diagonal of a matrix only like this, but I know there is a ...

1. ## 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]);
}
}```

2. start by asking yourself what is a diagonal. (look at the math definition)

3. i know what it is, i just don't know how to print it

4. 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

5. sorry for being stupid got it!

6. 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.

7. lose the second for along with b and print mx[a][a]