Advertisement
Courbe_Impliquee

Функция 4

Mar 15th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <cstdlib>
  3. #include <iostream>
  4. #include <iomanip>
  5. #include <clocale>
  6. #include <cmath>
  7. using namespace std;
  8. void input(float *a, int n){
  9. for (int i = 0; i < n; i++){
  10. cout << i << ": ";
  11. cin >> a[i];
  12. }
  13. }
  14. void output(float *a, int n){
  15. for (int i = 0; i < n; i++){
  16. cout << setw(4) << a[i];
  17. }
  18. cout << endl;
  19. }
  20.  
  21.  
  22. int min(float *a, int n){
  23. float min = abs(a[0]);
  24. int num=0;
  25. for (int i = 1; i < n; i++){
  26. if (min > abs(a[i])){
  27. min = abs(a[i]);
  28. num = i;
  29. }
  30. }
  31. return num;
  32. }
  33. int max(float *a, int n){
  34. float max = abs(a[0]);
  35. int num = 0;
  36. for (int i = 1; i < n; i++){
  37. if (max < abs(a[i])){
  38. max = abs(a[i]);
  39. num=i;
  40. }
  41. }
  42. return num;
  43. }
  44. float pr(float *a, int n){
  45. float ppp = 1;
  46. bool flag = 1;
  47. int max_n = max(a, n), min_n = min(a, n);
  48. if (max_n < min_n)
  49. swap(max_n, min_n);
  50. for (int i = min_n + 1; i < max_n; i++){
  51. ppp *= a[i];
  52. flag = 0;
  53. }
  54. if (flag)
  55. ppp = 0;
  56. return ppp;
  57. }
  58.  
  59. void sort(float *a, int n){
  60. for (int i = 0; i < n; i++){
  61. for (int j = n-1; j>i; j--){
  62. if (a[j-1] < a[j]){
  63. float tmp = a[j];
  64. a[j] = a[j-1];
  65. a[j-1] = tmp;
  66. }
  67. }
  68. }
  69. }
  70. float sum(float *a, int n){
  71. float s = 0;
  72. for (int i = 0; i < n; i++){
  73. if (a[i]>0)
  74. s += a[i];
  75. }
  76. return s;
  77. }
  78. int _tmain(int argc, _TCHAR* argv[])
  79. {
  80. setlocale(LC_ALL, "rus");
  81. int n;
  82. cin >> n;
  83. float *a = new float[n];
  84. input(a, n);
  85. cout << endl;
  86. cout << "Исходный массив:" << endl;
  87. output(a, n);
  88. cout << "Сумма положительных элементов: " << sum(a, n)<<endl;
  89. cout << "Произведение: " << pr(a, n)<<endl;
  90. sort(a, n);
  91. cout << "Отсортированный массив:"<< endl;
  92. output(a, n);
  93. cout << endl;
  94. system("pause");
  95. return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement