why my code doesnt work(trying to delete element in array)

can someone let me know why does my code doesn't work.

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
  #include <iostream>
using namespace std;

class NumberList {

     int numbers[8]={100,2,31,32,20,50,49,28};
     int size =8;
     public:
    
    
    void deleteElement (int index) {
        
        
        for (int i=index; i<8; i++) {
            numbers[i] = numbers[i+1];
        }
        size--;
        
          for(int j=0;j<size;j++)
        {
            cout<<numbers[j]<<endl;
        }
        
    }
    
    int deleteEven()
    {
        int j;
        for(int i=0;i<size;i++)
        {
            if(numbers[i]%2!=0)
            {
                numbers[j]=numbers[i];
                j++;
            }
        }
        
        int* temp = new int[j];
        for(int i=0;i<j;i++)
        {
            temp[i]=numbers[i];
        }
        
        delete [] numbers;
        numbers=temp;
        
        for(int i=0;i<size;i++)
        {
            cout<<numbers[i]<<" ";
        }
    }
};

int main() {
    NumberList mylist, mylist1;  // declaring an object
    mylist.deleteElement(4);
    mylist.deleteEven();
    
}
(1) Your deleteEven function is supposed to return an int, but you don't return anything.
(2) numbers is not a pointer to a dynamically allocated array. It's simply a member variable of an ordinary C-style array. You should not call delete on this.
(3) Likewise, you can't overwrite the whole array with an assignment (line 45). You must loop through each element to copy it over.
(4) You should, however, eventually be calling delete[] on the 'temp' object that you new[]'d.
(5) You declare a variable called 'j' on line 28 but never initialize it.
(6)
1
2
3
       for (int i=index; i<8; i++) {
            numbers[i] = numbers[i+1];
        }
This will call numbers[7] = numbers[8], which goes out of bounds of your numbers array.

You should turn on warnings on your compiler; there will probably be several
51:5: warning: control reaches end of non-void function [-Wreturn-type]
39:9: warning: 'j' may be used uninitialized in this function [-Wmaybe-uninitialized]
 In function 'int main()':
15:37: warning: iteration 3u invokes undefined behavior [-Waggressive-loop-optimizations]
14:9: note: containing loop


Your size can, at most, be 8. So you don't need to dynamically create an array here. Just create an array of size 8, and use some or all of those indices.
Last edited on
You can't delete on an array like numbers.

It's not recommended that size is used as a variable as there is now a function std::size()

deleteEven() can be vastly simplified

Don't make a function have multiple jobs. deleteElement() should just delete an element. Have an additional function say display() to display the data.

Consider:

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
#include <iostream>

class NumberList {
	size_t sznums {8};
	int numbers[8] {100, 2, 31, 32, 20, 50, 49, 28};

public:
	// index starts at 0
	void deleteElement(size_t index) {
		for (size_t i = index; i < sznums - 1; ++i)
			numbers[i] = numbers[i + 1];

		--sznums;
	}

	void display() {
		for (size_t j = 0; j < sznums; ++j)
			std::cout << numbers[j] << ' ';

		std::cout << '\n';
	}

	void deleteEven() {
		for (size_t i = 0; i < sznums; )
			if (numbers[i] % 2 == 0)
				deleteElement(i);
			else
				++i;
	}
};

int main() {
	NumberList mylist, mylist1;

	mylist.deleteElement(4);
	mylist.display();

	mylist1.deleteEven();
	mylist1.display();
}



100 2 31 32 50 49 28
31 49

Topic archived. No new replies allowed.