ЗАВДАННЯ №1.

Дан клас Mas (Динамічний масив).Клас повинен містити конструктор без параметрів ( ініціалізує масив із восьми елементів), конструктор ініціалізації ( розмірність задається з клавіатури), деструктор, функцію виведення елементів масиву.

Також у класі визначити:

· функцію, яка повертає добуток від’ємних елементів;

· функцію, яка сортовує масив по-зменьшенню;

· функцію, яка кожний елемент масиву множить на число типу int;

Створити два об’єкта Ar1 , Ar2 даного класу. Перший об’єкт ініціалізується конструктором без параметрів, другий – конструктором ініціалізації. Вивести два об’єкта на екран з коментарями.

Знайти суму добуток від’ємних елементі, кожний елемент масиву помножить на число типу int; відсортовати масив по- зменьшенню. Результати вивести на екран.

ПРИМІТКА : У класі передбачити можливість добавлення нових елементів масиву при перевищенні розмірності масиву .Вивести результати

Створити об’єкт Ar3 на основі об’єкта Ar2 даного класу. Вивести об’єкт Ar3.

 

#include <iostream>

#include <stdlib.h>

#include <math.h>

#include <conio.h>

using namespace std;

class Mas

{

int *m_arr;

int m_size;

int m_step;

int m_curInd;

void AddMemory();

 

public:

Mas(int size, int step);

Mas();

~Mas();

void Add(int a);

void Show();

int Get(int index);

int subZeroMul();

void mul(int a);

void sort();

};

 

void Mas::AddMemory()

{

int *n_arr = new int[m_size + m_step];

memcpy(n_arr, m_arr, m_size * sizeof(int));

delete []m_arr;

m_arr = n_arr;

m_size += m_step;

}

 

Mas::Mas()

{

m_size = 8;

m_step = 5;

m_curInd = 0;

m_arr = new int[m_size];

}

 

Mas::Mas(int size, int step) : m_size(size), m_step(step), m_curInd(0), m_arr(new int[size]) {}

 

Mas::~Mas()

{

delete []m_arr;

}

 

void Mas::Add(int a)

{

m_arr[m_curInd++] = a;

if (m_curInd == m_size)

AddMemory();

}

 

void Mas::Show()

{

for (int i = 0; i < m_curInd; i++)

cout << m_arr[i] << " ";

cout << endl;

}

 

int Mas::Get(int index)

{

return m_arr[index];

}

 

void main()

{

Mas *Ar1 = new Mas();

int a,b;

cout<<"Input size of arrayn-->";

cin>>a;

cout<<"Input step of arrayn-->";

cin>>b;

Mas *Ar2 = new Mas(a,b);

for (int i = 0; i < 8; i++)

{

Ar1->Add(rand()%300 - 150);

}

for (int i = 0; i < a; i++)

{

Ar2->Add(rand()%300 - 150);

}

cout<<"the mutiple all negative values Ar1 "<<Ar1->subZeroMul()<<"n";

cout<<"the mutiple all negative values Ar2 "<<Ar2->subZeroMul()<<"n";

cout<<"Sorting an Mas in ascending Ar1n";

Ar1->sort();

Ar1->Show();

cout<<"n";

cout<<"Sorting an Mas in ascending Ar2n";

Ar2->sort();

Ar2->Show();

cout<<"n";

cout<<"Multiply by 4n";

Ar1->mul(4);

Ar1->Show();

cout<<"n";

Ar2->mul(4);

Ar2->Show();

cout<<"n";

Mas *Ar3(Ar2);

cout<<"Ar3 -->";

Ar3->Show();

system("pause");

}

int Mas::subZeroMul()

{

int a = 1;

for(int g = 0; g < m_size; g++)

if(m_arr[g] < 0)

a *= m_arr[g];

return a;

}

void Mas::sort()

{

for(int k = 0; k < m_size; k++)

for(int j = k + 1; j < m_size;j++)

if(m_arr[k] < m_arr[j])

{

m_arr[k] += m_arr[j];

m_arr[j] = m_arr[k] - m_arr[j];

m_arr[k] = m_arr[k] - m_arr[j];

}

}

void Mas::mul(int a)

{

for(int b = 0; b < m_size;b++)

{

m_arr[b] *= a;

}

}

 

Висновок: