Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- /~~~ | | /\ |\ |/~~ |~~ ~~|~~ | ||~~ \ //~~\ |~~\| |~~\
- | |---| /__\ | \ || __ |-- | |---||-- \ /\ /| ||__/| | |
- \___ | |/ \| \|\__/ |__ | | ||__ \/ \/ \__/ | \|__|__/
- */
- #include <iostream>
- using namespace std;
- class Array
- {
- private :
- int *ptr;
- int size;
- public:
- Array(int);//конструктор
- void setdata();//заполнение массива
- void showdata();//вывод массива
- void sort();//сортировка массива
- int getSize();//возвращает кол-во эл-ов
- void sum();//сумма элементов массива
- };
- Array::Array(int arraySize)
- {
- size = arraySize;
- ptr = new int[size];
- for (int i = 0; i < size; i++)//обнулим массив
- {
- ptr[i] = 0;
- }
- }
- void Array::setdata()
- {
- for (int i = 0; i < size; i++)
- {
- cin >> ptr[i];
- }
- }
- void Array::showdata()
- {
- for (int i = 0; i < size; i++)
- {
- cout << ptr[i]<<" ";
- }
- }
- int Array::getSize()
- {
- return size;
- }
- void Array::sort()
- {
- int tmp;
- for (int i = 0, j = 0; i<size; i++)
- {
- tmp = ptr[i];
- j = i - 1;
- while ((j >=0) && (ptr[j] > tmp))
- {
- ptr[j + 1] = ptr[j];
- j = j - 1;
- }
- ptr[j + 1] = tmp;
- }
- }
- void Array::sum()
- {
- int s = 0;
- for (int i = 0; i < size; i++)
- {
- s += ptr[i];
- }
- cout << s;
- }
- int main()
- {
- int n;
- cout << "Size array=";
- cin >> n;
- Array myArray(n);
- cout << "Enter numbers:" << endl;
- myArray.setdata();
- cout << "Array:" << endl;
- myArray.showdata();
- cout << endl;
- cout << "Size array " << myArray.getSize() << endl;
- cout << "Sort array=";
- myArray.sort();
- myArray.showdata();
- cout << endl;
- cout << "Add all elements=";
- myArray.sum();
- cout << endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment