Array deletetion and convert problems

Hey guys im suppose to be creating a bag adt for a code that a professor has already created. i am having troubles with deleting a certain element in a array and it is saying that it is having troubles converting the function of remove and contains. if anyone could help that would be nice Thanks in Advance!

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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <iostream>
#include <string>
using namespace std;

class Bag{
public:
string list[100];
int n;
bool add(string item);
void display(void);
bool remove(string item);
void clear(void);
bool isEmpty(void);
bool contains(string item);
int getFrequency(string item);
Bag(){
  n=0;
}};

//Test 1
bool Bag::add(string item){
        bool enter;
        list[n] = item;
        if (list [n] == item)
                enter = true;
        else
                enter = false;
        n++;
}

//Test 2
void Bag::display(void){
        int i;
        cout << "Your List:"<< endl;
        for (i=0; i<100; i++){
                cout << list[i] << endl;
        }
}

//Test 3
bool Bag::remove(string item){
        int i;
        bool none;
        for (i=0; i < 100; i++){
                if (list[i] = item){
                        delete list[i];
                        if (list[i] !== NULL)
                                none = false;
                        else
                                none = true;
                }
        }
        return none;
}

//Test 4
void Bag::clear(void){
        int i;
        for (i=0; i < 100;i++){
                delete list[i];
        }
}


//Test 5
bool Bag::isEmpty(void){
        int i;
        bool empty;
        for (i=0; i<100; i++){
        if (list[i] == 0)
            empty = true;
        else
                empty = false;
        }
        return empty;
}

//test 7
bool Bag::contains(string item){
        bool found = false;
        int i;
        if (found == false){
                for (i=0; i<100; i++){
                        if (list[i] = item){
                             found = true;
                        }
                }
        }
        return found;
}


//Test 8
int Bag::getFrequency(string item){
        int i, n;
        n = 0;
        for (i=0; i<100; i++){
                if (list[i] == item)
                        n++;
        }
        return n;
}
You cannot use delete on something that was not dynamically allocated (with new).

Look at http://www.cplusplus.com/reference/algorithm/remove/
It does not delete anything.

Your remove should essentially be a --n;, except that it is not the last item that is left out. You have to move/copy some elements first.


The clear() -- what logic operation does it perform?


The contains() -- condition on line 82 is always true and assignment on line 84 ruins your data.


isEmpty() -- when is foo == 0 true, if the type of foo is std::string?


remove() line 45 -- another assignment within condition.


Some compilers (with some options) make remarks about assignments within conditions. If yours does, then pay attention.
Line 47 should be if (list[i] != NULL) // Not !==
Line 84 should be if (list[i] == item){ // Note: == (compare) instead of = (assign)

Line 82 doesn't make sense, just remove it.
After line 85 add break;

Line 46 is wrong. Use delete only for pointer previously set with new. It does not remove an item from the array.

Further more: Do not use 100. Always use n instead.
since i need pointers is there a way of adding a pointer to be able to use /code delete /endcode. also my complier isnt liking the != and the == with NULL. it gives the error of no match for 'operator!=' or 'operator==' thanks!
Last edited on
You don't need pointer and they would not help anyway.

it gives the error of no match for 'operator!=' or 'operator=='
Because strings are not pointer.

One way to remove an element from an array:
1
2
3
4
5
6
7
8
9
10
11
12
bool Bag::remove(string item){
        bool is_found = false;
        for (int i=0; i < n; i++){ // Note: use n here
                if (list[i] == item){ // Note: ==
swap(list[i], list[n-1]); // Note: This will change the order, otherwise you need to copy from list[i + 1] ... list[n-1] -> list[i] <-> loop
--n; // Note: Important! This reduces the array size
is_found = true;
break;
                }
        }
        return is_found;
}
Topic archived. No new replies allowed.