Advertisement
Wow_Rasl

Untitled

Dec 13th, 2021
659
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.53 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <stdio.h>
  4. #include <algorithm>
  5. using namespace std;
  6. double* a;
  7. int n;
  8. double find_min(double*a){
  9.     double ans = -1;
  10.     for(int i = 0; i < n; ++i){
  11.         if(a[i]>0 && ans == -1){
  12.             ans = a[i];
  13.         }else if(a[i]>0 && ans != -1){
  14.             ans = min(ans, a[i]);
  15.         }
  16.     }
  17.     if(ans == -1){
  18.         printf("нет элементов >0\n");
  19.         return 0;
  20.     }
  21.     return ans;
  22. }
  23.  
  24. double sredn(double*a){
  25.     int j = 0;
  26.     for(int i = 1; i < n; ++i) {
  27.         if(a[i]>=a[j]) j = i;
  28.     }
  29.     double ans = 0;
  30.     for(int i = 0; i < j; ++i) {
  31.         ans += a[i];
  32.     }
  33.     if(!j){
  34.         printf("%lf\n", 0);
  35.         return 0;
  36.     }
  37.     return ans/j;
  38. }
  39.  
  40. void create(double **pa, int* pn){
  41.     printf("n = ");
  42.     scanf("%d\n", &n);
  43.     a = (double*)malloc(sizeof(double)*n);
  44.     for(int i=0;i<n;++i){
  45.         scanf("%lf", &a[i]);
  46.     }
  47.     *pa = a;
  48.     *pn = n;
  49. }
  50.  
  51. double sr(double* a){
  52.     double ans =0;
  53.     int col=0;
  54.     for(int i=0;i<n;++i){
  55.         if(a[i]>0){
  56.             ans+=a[i];
  57.             ++col;
  58.         }
  59.     }
  60.     if(!col){
  61.         return 0;
  62.     }else{
  63.         return ans/col;
  64.     }
  65. }
  66.  
  67. int main()
  68. {
  69.     create(&a, &n);
  70.     printf("Ответ на первое задание = %lf\n",find_min(a));
  71.     printf("Ответ на второе задание = %lf\n",sredn(a));
  72.     printf("ответ на доп задание = %lf\n", sr(a));
  73.     free(a);
  74.     return 0;
  75. }
  76.  
  77. //6
  78. //5.5 3 -2 5.5 5 -1
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement