Dynamic Array


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
using namespace std;
int main()
{
    int l;
    cout << "enter number: ";
    cin >> l;
    string* x= new string [n];
  
    for (int i = 0; i < l; i++)
        cin >> x[i];
  
    for (int i = 0; i<l; i++)
        cout << A[i] <<  endl;
}
Last edited on
cout << B[i] << " " < C[i] << endl;

Have a real good look at this line. Count how many < there are.
Other problems with the code:

The last loop in main goes all the way up to n but the B and C arrays only have n/2 elements.

In Alternate_split you delete the A array and then you access elements from the array that you just deleted.
Thank you guys, I don't know how I overlooked these things
However, if I input,
that I want 4 places and record them as 0, 1, 2, 3
The output should be

B C
0 2
1 3


But I keep getting

B       C
0        3
1        3
1
2
3
4
5
for (int i = 0; i <n/2; i++)
{
  for (int j = n/2; j < n; j++)
    C[i] = A[j];
}

Lets walk that through. The n==4 and n/2==2.

On first iteration of the outer loop i==0 and the inner loop does:
1
2
C[0] = A[2];
C[0] = A[3];

That is just like you had done only C[0] = A[3];

On first iteration of the outer loop i==1 and the inner loop does:
1
2
C[1] = A[2];
C[1] = A[3];

That is just like you had done only C[1] = A[3];


Overall, if you can "fill" the B with one loop, then why would filling C need two loops?
got it Thank you
Topic archived. No new replies allowed.