Advertisement
Guest User

Untitled

a guest
Feb 16th, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. 1)
  2. #include <cstdio>
  3. #include <clocale>
  4. #include <stdlib.h>
  5. /*
  6. Функция pudge вычисляет среднюю длину подпоследовательностей нечетных чисел.
  7. На вход получает указатель на файл с последовательностью чисел и переменную res типа double, она содержит результат работы функции
  8. Возвращает -1 в случае успешного выполнения
  9. -2 в случае, если не удалось открыть файл
  10. -3 в случае, если в файле было обнаружено некорректное значение
  11. */
  12.  
  13. int pudge(FILE* file, double& res);
  14. {
  15. char namef[100]
  16. printf("Введите название файла, откуда произойдет считывание элементов\n");
  17. scanf_s("%99s", namef, 99);
  18. int open = fopen_s(&file, namef, "r");
  19. if (open == 1)
  20. return -2;
  21. int k1 = 0; int k2 = 0; int sum = 0; double a; int read; int flag = 0;
  22. while (read = fscanf_s(file, "%lf", &a) != EOF)
  23. {
  24. if (read != EOF)
  25. if (read == 0)
  26. return -3;
  27. if (a % 2 == 1)
  28. {
  29. k1++;
  30. flag = 1;
  31. }
  32. else
  33. {
  34. k2++;
  35. sum = sum + k1;
  36. k1 = 0;
  37. }
  38. }
  39. if (flag == 0)
  40. res = k1;
  41. else
  42. res = sum / k2;
  43. return -1;
  44. }
  45.  
  46.  
  47. 2)
  48.  
  49. #pragma once
  50. #include <cstdio>
  51.  
  52. /*
  53.  
  54.  
  55.  
  56. */
  57.  
  58. int pudge(FILE* file, double& res);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement