Advertisement
genets

массивы

Mar 18th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.86 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. void fill_array(int* a, int n);
  6. void print_array(int* a, int n);
  7. int count_more(int* a, int n);
  8.  
  9. int main()
  10. {
  11.     setlocale(LC_ALL, "rus");
  12.  
  13.     int n;
  14.     cout << "n = ";
  15.     cin >> n;
  16.  
  17.     int* a = new int[n];
  18.     fill_array(a, n);
  19.     print_array(a, n);
  20.  
  21.     int count = count_more(a, n);
  22.     cout << "Результат = " << count;
  23. }
  24.  
  25. void fill_array(int * a, int n)
  26. {
  27.     for (int i = 0; i < n; i++)
  28.     {
  29.         cout << "a[" << i << "] = ";
  30.         cin >> a[i];
  31.     }
  32. }
  33.  
  34. void print_array(int * a, int n)
  35. {
  36.     for (int i = 0; i < n; i++)
  37.     {
  38.         cout << a[i] << " ";
  39.     }
  40.     cout << endl;
  41. }
  42.  
  43. int count_more(int * a, int n)
  44. {
  45.     int count = 0;
  46.     for (int i = 1; i < n - 1; i++)
  47.     {
  48.         //если элемент больше своих соседей
  49.         if (a[i] > a[i - 1] && a[i] > a[i + 1])
  50.             count++;
  51.     }
  52.  
  53.     return count;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement