Bob103

C++_(for Nig**)

Oct 3rd, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.79 KB | None | 0 0
  1. /*   
  2. /~~~ |   |  /\  |\  |/~~  |~~   ~~|~~ |   ||~~  \        //~~\ |~~\|  |~~\
  3. |    |---| /__\ | \ || __ |--     |   |---||--   \  /\  /|    ||__/|  |   |
  4. \___ |   |/    \|  \|\__/ |__     |   |   ||__    \/  \/  \__/ |  \|__|__/
  5.  
  6.     */
  7.  
  8.  
  9. #include <iostream>
  10. using namespace std;
  11.  
  12. class Array
  13. {
  14. private :
  15.     int *ptr;
  16.     int size;
  17. public:
  18.     Array(int);//конструктор
  19.     void setdata();//заполнение массива
  20.     void showdata();//вывод массива
  21.     void sort();//сортировка массива
  22.     int getSize();//возвращает кол-во эл-ов
  23.     void sum();//сумма элементов массива
  24. };
  25.  
  26. Array::Array(int arraySize)
  27. {
  28.     size = arraySize;
  29.     ptr = new int[size];
  30.     for (int i = 0; i < size; i++)//обнулим массив
  31.     {
  32.         ptr[i] = 0;
  33.     }
  34.  
  35. }
  36. void Array::setdata()
  37. {
  38.     for (int i = 0; i < size; i++)
  39.     {
  40.         cin >> ptr[i];
  41.     }
  42. }
  43.  
  44. void Array::showdata()
  45. {
  46.     for (int i = 0; i < size; i++)
  47.     {
  48.         cout << ptr[i]<<" ";
  49.     }
  50. }
  51. int Array::getSize()
  52. {
  53.     return size;
  54. }
  55.  
  56. void Array::sort()
  57. {
  58.     int tmp;
  59.     for (int i = 0, j = 0; i<size; i++)
  60.     {
  61.         tmp = ptr[i];
  62.         j = i - 1;
  63.         while ((j >=0) && (ptr[j] > tmp))
  64.         {
  65.             ptr[j + 1] = ptr[j];
  66.             j = j - 1;
  67.         }
  68.         ptr[j + 1] = tmp;
  69.     }
  70. }
  71.  
  72. void Array::sum()
  73. {
  74.     int s = 0;
  75.     for (int i = 0; i < size; i++)
  76.     {
  77.         s += ptr[i];
  78.     }
  79.     cout << s;
  80. }
  81. int main()
  82. {
  83.     int n;
  84.     cout << "Size array=";
  85.     cin >> n;
  86.     Array myArray(n);
  87.     cout << "Enter numbers:" << endl;
  88.     myArray.setdata();
  89.     cout << "Array:" << endl;
  90.     myArray.showdata();
  91.     cout << endl;
  92.     cout << "Size array " << myArray.getSize() << endl;
  93.     cout << "Sort array=";
  94.     myArray.sort();
  95.     myArray.showdata();
  96.     cout << endl;
  97.     cout << "Add all elements=";
  98.     myArray.sum();
  99.     cout << endl;
  100.     system("pause");
  101.     return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment