Advertisement
SteelK

Untitled

Apr 2nd, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.24 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <cstdlib> // Для srand(time(NULL)); и rand...;
  4. #include <time.h> // Для time(NULL);
  5. #include <cmath>
  6.  
  7. using namespace std;
  8.  
  9. int unsigned z = 0;
  10.  
  11. int *copy_mas_out (int *in_mas, unsigned int in_length)
  12. {
  13.     int *copy_mas = new int[in_length];
  14.  
  15.     int min = in_mas[0];
  16.     for (unsigned int i = 1; i < in_length; i++)
  17.     {
  18.         if (min > in_mas[i])
  19.             min = in_mas[i];
  20.     }
  21.  
  22.     for (unsigned int i = 0; i < in_length; i++)
  23.     {
  24.         if (abs(min - in_mas[i]) <= 2)
  25.         {
  26.             copy_mas[z] = in_mas[i];
  27.             z++;
  28.         }
  29.     }
  30.  
  31.     return copy_mas;
  32.     delete [] copy_mas;
  33. }
  34.  
  35.  
  36. int main()
  37. {
  38.     srand(time(NULL));
  39.     setlocale(0, "");
  40.  
  41.     unsigned int lengthmas = 0;
  42.     cout << "Укажите длину массива..." << endl;
  43.     cin >> lengthmas;
  44.  
  45.     if (lengthmas < 0)
  46.     {
  47.         cout << "ERROR" << endl;
  48.         exit(1);
  49.     }
  50.  
  51.     int *mas = new int[lengthmas];
  52.  
  53.     for (unsigned int i = 0; i < lengthmas; i++)
  54.         mas[i] = rand() % 40 - 20;
  55.  
  56.     unsigned int i;
  57.     for (i = 0; i < lengthmas; i++)
  58.         cout << "mas[" << i << "] = " << mas[i] << endl;
  59.  
  60.     int * copy_mas = copy_mas_out (mas, lengthmas);
  61.     for (unsigned int i = 0; i < z; i++)
  62.         cout  << "copy_mas[" << i << "] = " << copy_mas[i] << endl;
  63.  
  64.     delete[] mas;
  65.     return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement