0% found this document useful (0 votes)
13 views

Lab 8

The document is a laboratory work in Ukrainian on algorithms and data structures. It presents two ways to solve an assignment to modify an ordered list of product codes. The first uses a vector container and the second uses a one-dimensional dynamic array. Both involve inserting 0 values at indices divisible by 3, swapping the first and last elements, and removing every other element. Screenshots of the console output demonstrating the solutions are provided.

Uploaded by

andrii.sikora.22
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Lab 8

The document is a laboratory work in Ukrainian on algorithms and data structures. It presents two ways to solve an assignment to modify an ordered list of product codes. The first uses a vector container and the second uses a one-dimensional dynamic array. Both involve inserting 0 values at indices divisible by 3, swapping the first and last elements, and removing every other element. Screenshots of the console output demonstrating the solutions are provided.

Uploaded by

andrii.sikora.22
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Міністерство освіти і науки України

Прикарпатський національний університет


імені В.Стефаника

Факультет математики та інформатики


Кафедра інформаційних технологій

Алгоритми і структури даних

Лабораторна робота №8
Тема: Стандартна бібліотека шаблонів

Варіант: 5

Виконав: Сікора А.Л.


Групa: ІПЗ-14
Дата: 4 квітня 2023р.

Викладач: Пікуляк М.
В.
Івано-Франківськ – 2023
Задача
У відсортованому списку товарів, який містить код продуктів, на
всіх позиціях, які кратні трійці, вставити значення 0. Вставлені
виводити іншим кольором для зручності користувачів. Після цього
поміняти місцями перший та останній елементи в списку товарів.
реалізувати задачу двома способами:
- за допомогою контейнера вектор;
- використавши одномірний динамічний масив.
За допомогою контейнера вектор.

Код виконання завдання


#include <iostream>
#include <vector>
#include <algorithm> #include
<time.h> #include <windows.h>
using namespace std;
HANDLE colors = GetStdHandle(STD_OUTPUT_HANDLE);
Void GenerateVector(vector<int>&vector);
void ShowVector(vector<int>&vector);
void InsertElement(vector<int>&vector, std::vector <int>::iterator it, int num);
void ShowNewVector(vector<int>& vector);
void RemoveElements(vector<int>& vector);

int main()
{
setlocale(LC_ALL, "Rus");
vector <int> vector(10);
std::vector <int>::iterator it;

int num = 0;

GenerateVector(vector);

cout << "Згенерований масив:\n";


ShowVector(vector);

sort(vector.begin(), vector.end());

cout << "Масив пiсля сортування:\n";


ShowVector(vector);

InsertElement(vector, it, num);

cout << "Масив пiсля вставки елементiв:\n";


ShowNewVector(vector);

RemoveElements(vector);
cout << "Масив пiсля видалення кожного другого елементу:\n";
ShowNewVector(vector);

swap(vector.at(0), vector.at(13));

cout << "Масив пiсля перестановки мiсцями першого та останього елемента:\n";


ShowNewVector(vector);

void GenerateVector(vector<int>&vector)
{
//srand(time(0));
for (int i = 0; i < vector.size(); i++) {
vector.at(i) = rand() % 31;
}
}

void ShowVector(vector<int>&vector)
{
for (int i = 0; i < vector.size(); i++)
{
cout << vector.at(i) << " ";
}
cout << "\n\n";

void InsertElement(vector<int>&vector, std::vector <int>::iterator it, int num)


{
for (int i = 1; i < vector.size(); i++)
{
it = vector.begin() + i - 1;

if (i % 3 == 0)
{
vector.insert(it, num);
}
}
}

void ShowNewVector(vector<int>& vector)


{
for (int i = 0; i < vector.size(); i++)
{
if (vector.at(i) == 0)
{
SetConsoleTextAttribute(colors, 12);
}
else
{
SetConsoleTextAttribute(colors, 15);
} cout << vector.at(i) << "
";
}
cout << "\n\n";
}

void RemoveElements(vector<int>& vector)


{
for (int i = 1; i < vector.size(); i += 2)
{
vector.erase(vector.begin() + i);
}
}
Скріншот консолі

Використавши одномірний динамічний масив

Код виконання завдання


#include <windows.h>
#include <iostream>
#include <time.h>
using namespace std;
HANDLE colors = GetStdHandle(STD_OUTPUT_HANDLE);
void GenerationArray(int* arr, int);
void ShowArray(int* arr, int);
void SortArray(int* arr, int);
int* Insert(int* arr, int, int);
void SwapElements(int* newArr, int, int);
int* Delete(int* arr, int size);

int main() {
setlocale(LC_ALL, "Rus");
int size = 10;
int n = size / 3 + 1;
int* arr = new int[size];

GenerationArray(arr, size);
cout << "Згенерований масив:\n";
ShowArray(arr, size);
SortArray(arr, size);
cout << "Масив пiсля сортування:\n";
ShowArray(arr, size);
cout << "Масив пiсля вставки елементiв\n";
int* newArr = Insert(arr, size, n);
ShowArray(newArr, size + n);
newArr = Delete(newArr, size + n);
cout << "Масив пiсля видалення кожного другого елементу:\n";
ShowArray(newArr, size + n / 2);
SwapElements(newArr, size, n);
cout << "Масив пiсля перестановки мiсцями першого та останього елемента:\n";
ShowArray(newArr, size + n / 2);
delete[] newArr;
}
void GenerationArray(int* arr, int size)
{
//srand(time(0));
for (int i = 0; i < size; i++)
{
arr[i] = rand() % 101;
}
}
void ShowArray(int* arr, int size)
{
for (int i = 0; i < size; i++)
{
if (arr[i] == 0)
{
SetConsoleTextAttribute(colors, 12);
}
else
{
SetConsoleTextAttribute(colors, 15);
}
cout << arr[i] << " ";
}
cout << "\n\n";
}
void SortArray(int* arr, int size)
{
int temp;
for (int a = 0; a < size; a++)
{
for (int i = size - 1; i > a; i--)
{
if (arr[i] < arr[i - 1])
{
temp = arr[i];
arr[i] = arr[i - 1];
arr[i - 1] = temp;
}
}
}
}
int* Insert(int* arr, int size, int n)
{

int value = 0; int Newindex = 0; int counter = 1;

int* newArr = new int[size + n];

for (int oldIndex = 0; oldIndex < size; oldIndex++, Newindex++)


{
if (((oldIndex + counter) % 3) == 0)
{
newArr[Newindex] = value;
Newindex++; counter++;
}

newArr[Newindex] = arr[oldIndex];
}

return newArr;
}

void SwapElements(int* newArr, int size, int n)


{
int x;
x = newArr[0]; newArr[0] =
newArr[size + n - 1];
newArr[size + n - 1] = x;
}

int* Delete(int* arr, int size)


{
int* newArr = new int[size / 2];
for (int i = 0; i < size; i += 2)
{
newArr[i / 2] = arr[i];
}
delete[] arr;
return newArr;
}
Скріншот консолі

You might also like