dynamic memory pointer in arrays

hey lads, your help will be welcomed :)))

I need to write a code which will copy all the odd and even elements from the original array into 2 new ones.

As simple as that, but I met some errors and don't have a clue respectively how to finish the code.

Please check the code below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
  #include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;
int main()
{
     srand(time(0));

    int *a = new int;  
    int *b = new int; 
    int *c = new int; 
    
    int *n = new int;
    int *k = new int;
    int *m = new int;
    
     *n=rand()%10+2;
     *n=*k; 
	 *n=*m;
     
         int *v = new int[*n]; 
	 int *p = new int[*k];
	 int *im= new int[*m];
	  
    for(int i=0; i <*n; i++)
    {
     *a=rand()%10+2;
     *b=rand()%10+2;
     *c=rand()%10+2;
     *(v+i) = *a+*b-*c;
     
     cout<<i<<" a="<<*a<<" b="<<*b<<" c="<<*c;
        cout<<" a+b-c="<<*a+*b-*c<<endl;
        
        if (*(v+i)%2==0) *(v+i)=*p;
        else *(v+i)=*im;
 }
     delete a;
     delete b;
     delete c;
     
    for(int i=0; i <*n; i++)
    {
       cout<<setw(3)<<*(v+i);
 }
 
        delete[] v;
        
  for(int i=0; i <*n; i++)
    {
       cout<<setw(3)<<*(p+k);
 }
  delete k;
  delete[] p;
 
 
  for(int i=0; i <*n; i++)
    {
       cout<<setw(3)<<*(im+m);
 }
 
 delete m;
 delete n;
 delete[]im;
 
return 0;
}
You have plenty of nonsense. Use of undefined values. Completely unnecessary dynamic memory allocations. Apparent logical errors.

The main issue is probably your attempt to use multiple features simultaneously. That obscures; makes harder to focus.

First: Replace all dynamic memory with automatic variables that you can.

You don't have to use the *(v+i) syntax. You can use (IMHO simpler) v[i]. Later, when you program works, you can return to the other syntax, if absolutely required.

You have:
1
2
3
int* x;
int* y;
x+y // Is this legal? What does it mean? 

Last edited on
> but I met some errors and don't have a clue respectively how to finish the code.
Did you have a clue how to start the code?

Or did you just pick up the brush, start painting madly, and now you find yourself in a corner.

Some initial thoughts.
1. You have a mass of meaningless single letter identifiers - wtf do they all mean?
I bet you don't even know unless you read the code in detail.

Before you went all pointer mad (why are you using them in a C++ program anyway - use a std::vector if you want variable length), you should have started with this.
1
2
3
4
5
6
7
int main() {
  int original[5] = { 1, 2, 3, 4, 5 };
  int evens[5];
  int odds[5];
  // copy (as a function)
  // print (as a function)
}

Something nice and simple that gets you moving in the right direction.

When you have something basic working, then you can incrementally improve it towards your desired goal.
- adding randomness.
- change ONE array to be a pointer with allocation and deletion.
- change the rest of the arrays to be pointers with allocation and deletion.

It's absolutely vital you get to grips with the idea of breaking problems down into smaller steps, and breaking programs down into smaller functions.

2. Your indentation could be better.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;

int main()
{
  srand(time(0));

  int *a = new int;
  int *b = new int;
  int *c = new int;

  int *n = new int;
  int *k = new int;
  int *m = new int;

  *n = rand() % 10 + 2;
  *n = *k;
  *n = *m;

  int *v = new int[*n];
  int *p = new int[*k];
  int *im = new int[*m];

  for (int i = 0; i < *n; i++) {
    *a = rand() % 10 + 2;
    *b = rand() % 10 + 2;
    *c = rand() % 10 + 2;
    *(v + i) = *a + *b - *c;

    cout << i << " a=" << *a << " b=" << *b << " c=" << *c;
    cout << " a+b-c=" << *a + *b - *c << endl;

    if (*(v + i) % 2 == 0)
      *(v + i) = *p;
    else
      *(v + i) = *im;
  }
  delete a;
  delete b;
  delete c;

  for (int i = 0; i < *n; i++) {
    cout << setw(3) << *(v + i);
  }

  delete[]v;

  for (int i = 0; i < *n; i++) {
    cout << setw(3) << *(p + k);
  }
  delete k;
  delete[]p;


  for (int i = 0; i < *n; i++) {
    cout << setw(3) << *(im + m);
  }

  delete m;
  delete n;
  delete[]im;

  return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <vector>
#include <iostream>

using namespace std;
int main() {
	vector<int> a{ 0,1,2,3,4,5,6,7,8,9};
	vector<int> even{};
	vector<int> odd{};

	for (auto i : a) {
		i % 2 == 0 ? even.push_back(i) : odd.push_back(i);
	}
	cout << "even[]= ";
	for (auto i : even) {
		cout << i << " ";
	}
	cout << '\n'<<"odd[]= ";
	for (auto i : odd) {
		cout << i << " ";
	}
}

the above could be done with pointers. I will say what i'm about to post is imo some real crap. I wouldn't ever do anything this way but here it is. edit: i fixed it up a bit. Hopefully thats right.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <vector>
#include <iostream>

using namespace std;

int** dim(const int size, int* p, int** pp) {
	int** a = new int* [size];

	for (int i = 0; i < size; i++) {

		if (i == size - 1) { a[i] = p; }
		else { a[i] = pp[i]; }
	}
	delete[] pp;
	return a;
}
int main() {
	int a[10] = { 0,1,2,3,4,5,6,7,8,9 };
	int ec = 0;
	int oc = 0;
	int** even = nullptr;
	int** odd = nullptr;

	for (int i = 0; i < sizeof(a) / sizeof(int); i++) {

		if (a[i] % 2 == 0) {
			ec++;
			even = dim(ec, &a[i], even);
		}
		else {
			oc++;
			odd = dim(oc,&a[i] , odd);
		}
	}
	cout << "odd[]= ";
	for (int i = 0; i < oc; i++) {
		cout << *odd[i] << " ";
	}
	cout << '\n' << "even[]= ";
	for (int i = 0; i < ec; i++) {
		cout << *even[i] << " ";
	}
	delete[] even;
	delete[] odd;
}


Last edited on
Topic archived. No new replies allowed.