Home c++ Project.exe triggered the breakpoint

# Project.exe triggered the breakpoint

Author

Date

Category

1) Form a one-dimensional array of integers using a random number generator.
2) Print the resulting array.
3) Remove all elements equal to 0.
4) Add an element with the value M [I-1] +2 after the first even element of the array.
5) Print the resulting array.

Methodical instructions:
1) When performing work, dynamic arrays are used. The arrays themselves are defined, then the user enters the real length of the array and works with the array of the length that he himself specified.
2) When decreasing or increasing the length of an array, it is necessary to create a new array of the required length and fill it with the elements of the old array.

The program gives the correct answer, but for some reason a breakpoint is triggered when clearing the array memory. Please tell me what needs to be done?

Code Program.cpp

``````# include & lt; iostream & gt;
#include & lt; conio.h & gt;
#include & lt; ctime & gt;
using namespace std;
int main ()
{
srand (time (NULL)); // We use a random number generator, taking the current time as a base
cout & lt; & lt; "Enter the length of the array:";
int t;
cin & gt; & gt; t;
int * arr1 = Massiv1 (t);
for (int i = 0; i & lt; t; i ++) // Print the created array
{
cout & lt; & lt; arr1 [i] & lt; & lt; "";
}
cout & lt; & lt; endl;
int p = Massiv2 (arr1, t);
int * arr2 = Massiv3 (arr1, p, t);
delete [] arr1; // Clearing the array memory
int * arr4 = Massiv4 (p, arr2);
bool elfinded = element (p, arr4);
if (elfinded == true) // If there is an even element, then increase the array memory by 1
{
p ++;
}
int m = p;
delete [] arr2; // Clearing the array memory
int * arr3 = Massiv5 (m, arr4);
delete [] arr4; // Clearing the array memory
for (int i = 0; i & lt; m; i ++) // Print the resulting array
{
cout & lt; & lt; arr3 [i] & lt; & lt; "";
}
cout & lt; & lt; endl;
delete [] arr3; // Clearing the array memory
system ("pause");
return 0;
}
``````

``````int * Massiv1 (int);
int Massiv2 (int *, int);
int * Massiv3 (int *, int, int);
int * Massiv4 (int, int *);
bool element (int, int *);
int * Massiv5 (int, int *);
``````

Code: AnotherOne.cpp

``````# include & lt; iostream & gt;
#include & lt; ctime & gt;
#include & lt; conio.h & gt;
using namespace std;
/ * Function to create an array * /
int * Massiv1 (int t)
{
int * arr1 = new int [t]; // Allocate memory for an array
for (int i = 0; i & lt; t; i ++) // Create an array using a random number generator
{
arr1 [i] = rand ()% 10;
}
return arr1;
}
/ * Function for detecting the size of the array memory * /
int Massiv2 (int * arr1, int t)
{
int n = 0; int k = 0;
int p = t - n;
for (int i = 0; i & lt; t; i ++) // Loop to check the element for zero
{
if (arr1 [i] == 0) // Count the number of elements equal to zero
{
n ++;
}
}
p = t - n;
return p;
}
/ * Function to create an array * /
int * Massiv3 (int * arr1, int p, int t)
{
int * arr2 = new int [p]; // Allocate memory for an array
int k = 0;
for (int i = 0; i & lt; t; i ++) // Loop to create an array
{
if (arr1 [i]! = 0) // Checking the element for being equal to zero
{
arr2 [k] = arr1 [i];
k ++;
}
}
return arr2;
}
/ * Function to create an array * /
int * Massiv4 (int p, int * arr2)
{
int * arr4 = new int [p]; // Allocate memory for an array
for (int i = 0; i & lt; p; i ++) // Loop to create an array
{
arr4 [i] = arr2 [i];
}
return arr4;
}
/ * The function finds out if an element needs to be added to the array * /
bool element (int p, int * arr4)
{
bool elfinded = false;
for (int i = 0; i & lt; p; i ++) // Loop to find out if an element needs to be added
{
if (arr4 [i]% 2 == 0 & amp; & amp; i! = 0) // If the condition is met, then the boolean variable becomes true
{
elfinded = true;
i = p;
}
}
return elfinded;
}
/ * Function to create a new array * /
int * Massiv5 (int m, int * arr4)
{
int * arr3 = new int [m]; // Allocate memory for an array
for (int i = 0; i & lt; m-1; i ++) // Loop to create an array
{
arr3 [i] = arr4 [i];
}
for (int i = 0; i & lt; m - 1; i ++) // Loop to add an element with the value arr4 [i-1] +2 after the first even element of the array
{
if (arr4 [i]% 2 == 0 & amp; & amp; i! = 0) // Find an element
{
for (int j = m; j! = i; j--) // Loop for assigning elements to a new array
{
arr3 [j + 1] = arr4 [j];
}
arr3 [i + 1] = arr4 [i - 1] + 2;
i = m - 1; // Exit the loop
}
}
return arr3;
}
``````

Watching your `Massiv5 `:

``````int * arr3 = new int [m]; // Allocate memory for an array
``````

Now –

``````for (int j = m; j! = i; j--) // Loop for assigning elements to a new array
{
arr3 [j + 1] = arr4 [j];
``````

Well, and where are you writing to? What is the item in the account? And where is it located? …

P.S. I was not looking for an error in completing your task in the sense of its meaning 🙂 – this is already on its own …

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.