Advertisement
Guest User

Untitled

a guest
May 24th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. // Найти два максимума в получаемом массиве (возможно, совпадающих)
  2.  
  3. #include <stdio.h>
  4.  
  5. #define CORRECT_ENDING 0
  6. #define LACK_OF_ELEMENTS -1
  7.  
  8. int process(FILE *f, int *first_max, int *second_max)
  9. {
  10. int num;
  11. int temp;
  12. int rc;
  13.  
  14. if (fscanf(f, "%d", first_max))
  15. {
  16. if (fscanf(f, "%d", second_max))
  17. {
  18. if (first_max < second_max)
  19. {
  20. temp = *first_max;
  21. *first_max = *second_max;
  22. *second_max = temp;
  23. }
  24.  
  25. while (fscanf(f, "%d", &num))
  26. {
  27. if (num >= *first_max)
  28. {
  29. *second_max = *first_max;
  30. *first_max = num;
  31. }
  32. else if (num > *second_max)
  33. {
  34. *second_max = num;
  35. }
  36. }
  37. rc = CORRECT_ENDING;
  38. }
  39. else rc = LACK_OF_ELEMENTS;
  40. }
  41.  
  42. else rc = LACK_OF_ELEMENTS;
  43.  
  44. if (rc == CORRECT_ENDING)
  45. return CORRECT_ENDING;
  46. else
  47. return LACK_OF_ELEMENTS;
  48. }
  49.  
  50. int main()
  51. {
  52. int first_max, second_max;
  53. int rc;
  54.  
  55. if ((rc = process(stdin, &first_max, &second_max)) == CORRECT_ENDING)
  56. printf("%d %d", first_max, second_max);
  57. else
  58.  
  59. {
  60. printf("There are not enough data.\n");
  61. }
  62.  
  63. return rc;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement