My code is:
#include <iostream> #include <time.h> #include <Windows.h> #include <iomanip> #include <cmath> #include <string> #include <conio.h> using namespace std; int main() { setlocale(LC_ALL, "Rus"); srand(unsigned(time(0))); cout << "Реализовать алгоритм для вставки блока элементов, начиная с произвольного индекса массива\n\n"; cout << "Введите размер массива: "; int sizeA; cin >> sizeA; int *arrA = new int[sizeA]; cout << "\nВаш массив: "; for (int x = 0; x < sizeA; x++) { arrA[x] = rand() % 9 + 1; cout << arrA[x] << " "; } cout << "\n\nВведите количество элементов которых будете добавлять/заменять в первом массиве: "; int amount; cin >> amount; cout << "\nВведите с какого элемента первого массива будете вставлять блок элементов (Считать с 0-го индекса): "; int change; cin >> change; int tmpsizeA = sizeA; if (change + amount > sizeA) { sizeA += amount - change - 1; } cout << "\nТеперь по очередности эти элементы (Ввод-пробел-ввод): "; for (int x = change; x < change + amount; x++) { cin >> arrA[x]; } if (change + amount > sizeA) { sizeA = change + amount; } else { sizeA = tmpsizeA; } cout << "\nКонечный итог: "; for (int x = 0; x < sizeA; x++) { cout << arrA[x] << " "; } delete[] arrA; _getch(); return 0; }
Everything seems to be working, I checked it with different combinations several times, but here is the problem alone. If the number of elements that I add is larger than the initial size of the array, then when I press the enter console, instead of closing, the console makes a Windows sound and crashes. But the result is true. There are no garbage values.