Advertisement
dxvmxnd

Untitled

Sep 30th, 2024 (edited)
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. #define SIZE 6
  6. #define MAX_VALUE 100
  7.  
  8. void fillMatrix(int matrix[SIZE][SIZE]) {
  9. for (int i = 0; i < SIZE; i++) {
  10. for (int j = 0; j < SIZE; j++) {
  11. matrix[i][j] = rand() % (MAX_VALUE - 1) + 2;
  12. }
  13. }
  14. }
  15.  
  16. void printMatrix(int matrix[SIZE][SIZE]) {
  17. for (int i = 0; i < SIZE; i++) {
  18. for (int j = 0; j < SIZE; j++) {
  19. printf("%3d ", matrix[i][j]);
  20. }
  21. printf("\n");
  22. }
  23. }
  24.  
  25. void replaceElements(int matrix[SIZE][SIZE]) {
  26. int count[MAX_VALUE + 1] = {0};
  27.  
  28.  
  29. for (int i = 0; i < SIZE; i++) {
  30. for (int j = 0; j < SIZE; j++) {
  31. count[matrix[i][j]]++;
  32. }
  33. }
  34.  
  35.  
  36. for (int i = 0; i < SIZE; i++) {
  37. for (int j = 0; j < SIZE; j++) {
  38. if (count[matrix[i][j]] > 1) {
  39. matrix[i][j] = 1;
  40. } else {
  41. matrix[i][j] = 0;
  42. }
  43. }
  44. }
  45. }
  46.  
  47. int main() {
  48. int matrix[SIZE][SIZE];
  49.  
  50. srand(time(NULL));
  51.  
  52. fillMatrix(matrix);
  53. printf("Исходная матрица:\n");
  54. printMatrix(matrix);
  55.  
  56. replaceElements(matrix);
  57. printf("Матрица после замены:\n");
  58. printMatrix(matrix);
  59.  
  60. return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement