Advertisement
vallec

Zadacha 1

Dec 17th, 2023
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int main() {
  4. int size, i, j;
  5. printf("Enter the size of the matrix: ");
  6. scanf("%d", &size);
  7. int matrix[size][size];
  8.  
  9.  
  10. // Input the elements of the matrix
  11. printf("Enter the elements of the matrix:\n");
  12. for (i = 0; i < size; i++) {
  13. for (j = 0; j < size; j++) {
  14. printf("Enter element [%d][%d]: ", i, j);
  15. scanf("%d", &matrix[i][j]);
  16. }
  17. }
  18.  
  19. // Displaying the matrix
  20. printf("Matrix info:\n");
  21. for (i = 0; i < size; i++) {
  22. for (j = 0; j < size; j++) {
  23. printf("%d ", matrix[i][j]);
  24. }
  25. printf("\n");
  26. }
  27.  
  28. int diagonalsWithZero = 0;
  29.  
  30. // Getting diagonals above the secondary diagonal
  31. for (i = 0; i < size - 1; i++) {
  32. int diagonal_length = size - i - 1;
  33. if (diagonal_length > 1) {
  34. for (j = 0; j < diagonal_length; j++) {
  35. if(matrix[j][size - i - 2 - j] == 0) {
  36. diagonalsWithZero++;
  37. break;
  38. }
  39. }
  40. }
  41. }
  42.  
  43. // Getting diagonals below the secondary diagonal
  44. for (i = 1; i < size; i++) {
  45. int diagonal_length = size - i;
  46. if (diagonal_length > 1) {
  47. for (j = 0; j < diagonal_length; j++) {
  48. if(matrix[i + j][size - 1 - j] == 0) {
  49. diagonalsWithZero++;
  50. break;
  51. }
  52. }
  53. }
  54. }
  55.  
  56. printf("Diagonals with zeros as value: %d", diagonalsWithZero);
  57.  
  58. return 0;
  59. }
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement