Advertisement
vallec

Zadacha 1

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