so i have this code in C++ where i have an array of an undefined initial value, and the user is supposed to keep entering values for the array until exit code -1. once that is entered the array size is determined by the number of inputs. then it has to be split in half into two arrays the first having the first half of the original array and the second having the second half of the original array.
#include <iostream>
using namespace std;
int main()
{
int n;
n++;
int a[n];
for (int i = 0; i < n; i++)
{
cin >> a[i];
n++;
if (a[i] == -1)
{
n--;
break;
}
}
cout << n << endl;
int e[n / 2];
for (int j = 0; j < (n / 2); j++)
{
e[j] = a[j];
}
int q = 0;
int o[n / 2];
for (int l = (n / 2); l < n; l++)
{
o[q] = a[l];
q++;
}
for (int h = 0; h < n / 2; h++)
{
cout << e[h] << " ";
}
cout << '\n';
for (int h = 0; h < n / 2; h++)
{
cout << o[h] << " ";
}
return 0;
}
the problem i'm facing is that ,when the array if sized 4, it works fine, but when it's sized 6 , the 5th and 6th value become 3 0 . instead of the value i entered . and making a larger array has a similar problem certain values in the array are damaged.