Advertisement
Guest User

Untitled

a guest
Jan 19th, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3. #include <stdio.h>
  4. using namespace std;
  5.  
  6. int max(int* a, const int N)
  7. {
  8. int maximum;
  9. for (int i = 0; i < N; i++)
  10. {
  11. if (i == 0) maximum = a[i];
  12. if (maximum < a[i]) maximum = a[i];
  13. }
  14. return maximum;
  15. }
  16.  
  17. int max_nech(int* a, const int N)
  18. {
  19. int maximum;
  20. for (int i = 0; i < N; i++)
  21. {
  22. if (a[i] % 2 == 1)
  23. {
  24. maximum = a[i];
  25. break;
  26. }
  27. }
  28. for (int i = 0; i < N; i++)
  29. {
  30. if ((maximum < a[i]) && (a[i] % 2 == 1)) maximum = a[i];
  31. }
  32. return maximum;
  33. }
  34.  
  35. int sum_otr(int* a, const int N)
  36. {
  37. int sum = 0;
  38. for (int i = 0; i < N; i++)
  39. {
  40. if (a[i] < 0) sum += a[i];
  41. }
  42. return sum;
  43. }
  44.  
  45. int main()
  46. {
  47. setlocale(0, "Rus");
  48. ifstream file("J:\\Универ\\Программирование\\Практика_txt\\z_1.txt");
  49. if (file.is_open())
  50. {
  51. cout << "Файл открыт" << endl;
  52. const int n = 30;
  53. int W[n];
  54. cout << "Массив W:" << endl;
  55. for (int i = 0; i < n; i++)
  56. {
  57. file >> W[i];
  58. cout << "W[" << i << "] = " << W[i] << endl;
  59. }
  60. cout << "Сумма отрицательных чисел: " << sum_otr(W, n) << endl;
  61. cout << "Максимальное число: " << max(W, n) << endl;
  62. cout << "Максимальное нечетное число: " << max_nech(W, n) << endl;
  63. system("pause");
  64. return 0;
  65. }
  66. else
  67. {
  68. cout << "Файл не открыт" << endl;
  69. system("pause");
  70. return 0;
  71. }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement