How can I decrease code length?
You could split your big for loop into 2 smaller ones. The first one would print all diagonals up to the main one, and the other one the remaining ones.
For instance, let's say there's a matrix like:
1 2 3
4 5 6
7 8 9
Then, your first loop would print 1 4 2 7 5 3 and the second one 8 6 9. The code would look conciser and more readable too:
for (int i = 0; i < N; i++) {
for (int j = 0; j <= i; j++) {
printf("%d ", pixel_array[i - j][j]);
}
}
for (int j = 0; j < N - 1; j++) {
for (int i = N - 1; i > j; i--) {
printf("%d ", pixel_array[i][N - i + j]);
}
}
How can I improve the efficiency of this program?
I don't think it's possible to make it more efficient than O(n2n²) as you have to go through all matrix elements to print them diagonally. So, the suggested solution is efficient enough.
Any other programming practices to be implemented
Rather than using an int type for defining a size of your array, you could use uint32_t (or uint16_t or uint64_t - it depends on your requirements). YouYour size cannot be negative, anyways. So, change int N = 5; to const uint32_t N = 5.
Rename your array to squareMatrix because you're actually dealing with square matrices in your app.
Also, create a method out of your algorithm block - but I'll leave it up to you.