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.

  • Not understood. What exactly is the task? What does "insert" mean? If you insert a block of elements into an array, the size of the array should increase by the size of this block. However, in your code I don’t see memory allocation for a new - enlarged - array anywhere. How can this work? Also, in your code, I don’t see any attempts to "insert" elements at all. Instead of insertion, simply replacing one element with another is done. - AnT
  • The user chooses from which element of the array to start replacing the elements of the array. If it goes beyond the edge of the array, it expands and the elements are added to the array, if not, the replacement occurs inside it without expanding the array. All operations with the same array. - Hiko Moff
  • So why did you write insert then if you need to replace? "Replace" and "insert" are completely different things. "Expands and elements are added," say? Well, where is the array extension in your code? Or do you expect that it will magically expand for you? - AnT
  • I do not know how else, if you know, tell me. I just wonder why it makes a sound and where is the accompanying error. But it would also be interesting to look at your implementation of this task. Headline corrected. - Hiko Moff
  • Error - departure beyond the array, of course. As I said above, by itself it will not "expand" for you. Hence the "sound". - AnT

1 answer 1

You increase the sizeA variable incorrectly the first time. But it falls not because of this, but due to the fact that you do not change the size of the array, as a result of which you get out of the limits of the array during the reading of the changed values. If you want to work with a dynamic array, use std :: vector better. It has functions such as size () and resize ().

  • What you wrote is a comment (and in the case), but not the answer. While you do not have the right to write comments because of the low rating. Therefore, it makes sense to expand your comment to a full answer, which will increase your rating. Otherwise, according to the rules of the forum, it will be demolished by moderators. - Alexander Muksimov