Skip to main content
Spelling fixes
Source Link
Toby Speight
  • 88.4k
  • 14
  • 104
  • 327

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(n2) 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.

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(n2) 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). You 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.

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() 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). Your 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.

Improve the answer description
Source Link
Anatolii
  • 992
  • 5
  • 17

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(n2) 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). You size cannot be negative, anyways. So, change int N = 5; to const uint32_t N = 5.

Also, rather than defining your array as int pixel_array[5][5]=..., you should use do it as int pixel_array[N][N]=....

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.

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(n2) 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). You size cannot be negative, anyways. So, change int N = 5; to const uint32_t N = 5.

Also, rather than defining your array as int pixel_array[5][5]=..., you should use do it as int pixel_array[N][N]=....

Rename your array to squareMatrix because you're actually dealing with square matrices in your app.

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(n2) 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). You 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.

Remove the last paragraph as it's wrong
Source Link
Anatolii
  • 992
  • 5
  • 17

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(n2) 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). You size cannot be negative, anyways. So, change int N = 5; to const uint32_t N = 5.

Also, rather than defining your array as int pixel_array[5][5]=..., you should use do it as int pixel_array[N][N]=....

Rename your array to squareMatrix because you're actually dealing with square matrices in your app.

As you currently define your logic in main() you could create a separate method instead like printMatrixDiagonally(int32_t** matrix, const uint32_t size). Then, you could use this method anywhere in your app.

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(n2) 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). You size cannot be negative, anyways. So, change int N = 5; to const uint32_t N = 5.

Also, rather than defining your array as int pixel_array[5][5]=..., you should use do it as int pixel_array[N][N]=....

Rename your array to squareMatrix because you're actually dealing with square matrices in your app.

As you currently define your logic in main() you could create a separate method instead like printMatrixDiagonally(int32_t** matrix, const uint32_t size). Then, you could use this method anywhere in your app.

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(n2) 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). You size cannot be negative, anyways. So, change int N = 5; to const uint32_t N = 5.

Also, rather than defining your array as int pixel_array[5][5]=..., you should use do it as int pixel_array[N][N]=....

Rename your array to squareMatrix because you're actually dealing with square matrices in your app.

Improve the answer description
Source Link
Anatolii
  • 992
  • 5
  • 17
Loading
Add an additional explanation
Source Link
Anatolii
  • 992
  • 5
  • 17
Loading
Source Link
Anatolii
  • 992
  • 5
  • 17
Loading