Вы находитесь на странице: 1из 5

Д З В Ы П О Л Н И Л

Б А Б У Р О В И В А Н

-----------------------------------------------------------------------------------
-----------

-----------------------------------------------------------------------------------
-----------

З А Д А Н И Е 1

void sort(int* begin, int* end)


{
for (int* p = begin; p < end - 1; p++)
{
int* min = p;

for (int* q = p + 1; q < end; q++)


if (*q < *min)
min = q;
if (p == min)
continue;
int buf = *p;
*p = *min;
*min = buf;
}
}

int main()
{
setlocale(LC_ALL, "Russian");
int min = -2, max = 2;
cout << "Введите минимальное значение элемента массива: ";
cin >> min;
cout << "Минимальное принято: " << min << endl;
cout << "Введите максимальное значение элемента массива: ";
cin >> max;
cout << "Максимальное принято: " << max << endl;
int size1 = 6, size2 = 6;
cout << "Введите размер 1-го массива: ";
cin >> size1;
cout << "Введите размер 2-го массива: ";
cin >> size2;

srand(time(0));
int* arr1, * arr2;
arr1 = new int[size1];
arr2 = new int[size2];
if (arr1 == nullptr || arr2 == nullptr)
{
cout << "Не удалось создать массивы";
return 1;
}
for (int* p1 = arr1; p1 - arr1 < size1; p1++)
*p1 = rand() % (max - min + 1) + min;
for (int* p2 = arr2; p2 - arr2 < size2; p2++)
*p2 = rand() % (max - min + 1) + min;

cout << "Массив 1: ";


for (int* p1 = arr1; p1 - arr1 < size1; p1++)
cout << *p1 << " ";
cout << endl << "Массив 2: ";
for (int* p2 = arr2; p2 - arr2 < size2; p2++)
cout << *p2 << " ";
cout << endl;

sort(arr1, arr1 + size1);


sort(arr2, arr2 + size2);

cout << "Отсотрированный массив 1: ";


for (int* p1 = arr1; p1 - arr1 < size1; p1++)
cout << *p1 << " ";
cout << endl << "Отсортированный массив 2: ";
for (int* p2 = arr2; p2 - arr2 < size2; p2++)
cout << *p2 << " ";
cout << endl;

int upsize3 = (size1 < size2) ? size1 : size2;


int* arr3 = new int[upsize3];
if (arr3 == nullptr)
{
cout << "Не удалось создать массив 3";
return 2;
}

int* p3 = arr3;
for (int* p1 = arr1, *p2 = arr2; p1 < arr1 + size1 && p2 < arr2 + size2; )
{
if (*p1 < *p2)
{
p1++;
continue;
}
else if (*p1 > *p2)
{
p2++;
continue;
}
else
{
*p3 = *p1;
do
p1++;
while (*p1 == *p3);
p3++;
}
}

int size3 = p3 - arr3;

cout << "Количество общих элементов: " << size3 << endl << "Сам массив 3:";
for (int* p3 = arr3; p3 - arr3 < size3; p3++)
cout << " " << *p3;

system("pause");
return 0;
}

-----------------------------------------------------------------------------------
-----------

-----------------------------------------------------------------------------------
-----------

З А Д А Н И Е 2

int main()
{
setlocale(LC_CTYPE, "rus");
int* a = 0, * b = 0, * c = 0, * res, * buf;
int m = 0, n = 0, k = 0, index = 0;
cout << "Введите числа m и n (разделённые пробелом): ";
cin >> m >> n;
a = new int[m]; b = new int[n];
c = new int[m + n];
cout << "\n a[j]";
for (int j = 0; j < m; j++)
{
a[j] = (int)((((float)rand() / (float)RAND_MAX) * 10) + 0);
cout << "\n" << a[j] << endl;
}
cout << "\n b[j]";
for (int j = 0; j < n; j++)
{
b[j] = (int)((((float)rand() / (float)RAND_MAX) * 10) + 0);
cout << "\n " << b[j] << endl;
}
for (int i = 0; i < m; i++)
{
k = 0;
for (int j = 0; j < n; j++)
{

if (a[i] != b[j]) { k++; };


}
if (k == n) {
c[index] = a[i]; index++;
}
}
for (int i = 0; i < n; i++)
{
k = 0;
for (int j = 0; j < m; j++)
{

if (b[i] != a[j]) { k++; };


}
if (k == n) {
c[index] = b[i]; index++;
}
}
delete[]a; delete[]b;
k = 0; int buf1 = 0;
buf = new int[index];
buf[buf1] = c[0];
for (int j = 0; j < index; j++)
{
k = 0;
for (int i = 0; i < index; i++)
if (c[j] != buf[i]) { k++; }
if (k == index) { buf1++; buf[buf1] = c[j]; }
}

delete[]c;
res = new int[buf1 + 1]; cout << "\nres[i]= ";
for (int i = 0; i <= buf1; i++)
{
res[i] = buf[i];
cout << "\n" << res[i];
}
delete[]buf; delete[]res;

return 0;
}
-----------------------------------------------------------------------------------
-----------

Вам также может понравиться