Help with remove and shifting elements

My output is not printing out correctly and I think it has something to do with my deleteElement function, but I cannot figure out what the problem is. I have provided other functions that is also part of the Main.cpp file

From DArray.cpp
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
template <typename T>
DArray<T>::DArray()
{
	capacity = CAP;
	a = new T[capacity];
	numOfElem = 0;
}

template <typename T>
DArray<T>::DArray(int newCapacity)
{
	capacity = newCapacity;
	a = new T[capacity];
	numOfElem = 0;
}

template <typename T>
DArray<T>::DArray(const DArray<T>& otherArray)
{
	capacity = otherArray.capacity;
	a = new T[capacity];
	numOfElem = otherArray.numOfElem;

	for (int i = 0; i < numOfElem; ++i)
		a[i] = otherArray.a[i];
}

template <typename T>
void DArray<T>::addElement(const T& element)
{
	a[numOfElem] = element;
	++numOfElem;
}

template <typename T>
void DArray<T>::deleteElement(const T& deleteElem)
{
	bool found = false;
	int pos = 0;

	while (!found && pos != numOfElem)
	{
		if (a[pos] == deleteElem)
		{
			for (int i = pos; i < numOfElem; i++)
			{
				a[i] = a[++i];
			}

			found = true;
		}

		else
		{
			pos++;
		}
	}

	--numOfElem;
}


And this is from the Movie.cpp
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
#include "Movie.h"

ostream& operator<<(ostream& out, const Movie& movie)
{
	out << "\"" << movie.name << "\" (" << movie.year << ")" << endl;
	return out;
}

Movie::Movie() : name(), year(0) {}

Movie::Movie(const string& diffName, int diffYear) :
	name(diffName), year(diffYear) {}

string Movie::getName() const
{
	return name;
}

int Movie::getYear() const
{
	return year;
}

void Movie::setName(const string& newName)
{
	name = newName;
}

void Movie::setYear(int newYear)
{
	year = newYear;
}

bool Movie::operator==(const Movie& otherMovie) const
{
	return (name == otherMovie.name && year == otherMovie.year);
}

Movie& Movie::operator=(const Movie& otherMovie)
{
	name = otherMovie.name;
	year = otherMovie.year;

	return *this;
}

Movie::~Movie() {}



From Main.cpp
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
#include "DArray.h"
#include "DArray.cpp"
#include "Movie.h"

#include <iostream>
using namespace std;

int main()
{
        DArray<int> a2(5);

        cout << "\nTEST 3 ------------------------\n\n";
	cout << "Re-populate..." << endl;
	cout << "Array 2 is now: 21 53 64 19 43, with same capacity 5\n";
	cout << "\nYour results:\n";
	a2.addElement(21);
	a2.addElement(53);
	a2.addElement(64);
	a2.addElement(19);
	a2.addElement(43);
	cout << "\tArray is: " << a2 << endl;
	cout << "\tCapacity: " << a2.getCapacity() << endl;
	cout << "\tNumber of elements: " << a2.getNumOfElem() << endl;
	cout << "\tEmpty? " << (a2.isEmpty() ? "True." : "False") << endl;
	cout << "\tFull? " << (a2.isFull() ? "True" : "False") << endl;
	cout << "\tElement at last index: " << a2[a2.getNumOfElem() - 1] << endl;
	cout << "\tDelete first element..." << endl;
	a2.deleteElement(21);
	cout << "\tAfter deletion: " << a2 << endl;
	cout << "\tDelete last element..." << endl;
	a2.deleteElement(43);
	cout << "\tAfter deletion: " << a2 << endl;
	cout << "\tNumber of elements: " << a2.getNumOfElem() << endl;
	cout << "\tEmpty? " << (a2.isEmpty() ? "True" : "False") << endl;
	cout << "\tFull? " << (a2.isFull() ? "True" : "False") << endl;
	cout << "\tSearch for 53: ";
	cout << ((a2.search(53)) ? "Found.\n" : "Not found.\n");
	cout << "\tSearch for 19: ";
	cout << ((a2.search(19)) ? "Found.\n" : "Not found.\n");
	cout << "\tSearch for 100: ";
	cout << ((a2.search(100)) ? "Found.\n" : "Not found.\n");
	cout << "\tElement at last index: " << a2[a2.getNumOfElem() - 1] << endl;
	cout << "\tDelete remaining elements..." << endl;
	a2.deleteElement(64);
	a2.deleteElement(53);
	a2.deleteElement(19);
	cout << "\tNumber of elements: " << a2.getNumOfElem() << endl;

	cout << endl;

	DArray<Movie> movies(4);
	Movie m1("Fight Club", 1999);
	Movie m2("Snatch", 2000);
	Movie m3("Deadpool", 2016);
	Movie m4("The Day After Tomorrow", 2004);

	cout << "\nTEST 5 ------------------------\n\n";
	cout << "\nRe-populate in reverse order..." << endl;	
	cout << "Array 3 is now: same capacity 4\n"
		<< "\tThe Day After Tomorrow, 2004\n"
		<< "\tDeadpool, 2016\n"
		<< "\tSnatch, 2000\n"
		<< "\tFight Club, 1999\n";
	cout << "\nYour results:\n";
	movies.addElement(m4);
	movies.addElement(m3);
	movies.addElement(m2);
	movies.addElement(m1);
	cout << "\tArray is:\n " << movies << endl;
	cout << "\tCapacity: " << movies.getCapacity() << endl;
	cout << "\tNumber of elements: " << movies.getNumOfElem() << endl;
	cout << "\tEmpty? " << (movies.isEmpty() ? "True." : "False") << endl;
	cout << "\tFull? " << (movies.isFull() ? "True" : "False") << endl;
	cout << "\tElement at last index: " << movies[movies.getNumOfElem() - 1] << endl;

	cout << "\tDelete first element..." << endl;
	movies.deleteElement(m4);
	cout << "\tAfter deletion:\n " << movies << endl;

	cout << "\tDelete last element..." << endl;
	movies.deleteElement(m1);
	cout << "\tArray is:\n " << movies << endl;
	cout << "\tNumber of elements: " << movies.getNumOfElem() << endl;
	cout << "\tEmpty? " << (movies.isEmpty() ? "True" : "False") << endl;
	cout << "\tFull? " << (movies.isFull() ? "True" : "False") << endl;
	cout << "\tElement at last index: " << movies[movies.getNumOfElem() - 1] << endl;
	cout << "\tDelete remaining elements..." << endl;
	movies.deleteElement(m2);
	movies.deleteElement(m3);
	cout << "\tNumber of elements: " << movies.getNumOfElem() << endl;

	cout << endl;
	system("Pause");
    return 0;


}



This is the output I am getting, which is not correct
<output>
TEST 3 ------------------------

Re-populate...
Array 2 is now: 21 53 64 19 43, with same capacity 5

Your results:
Array is: 21 53 64 19 43
Capacity: 5
Number of elements: 5
Empty? False
Full? True
Element at last index: 43
Delete first element...
After deletion: 21 53 64 19
Delete last element...
After deletion: 21 53 64
Number of elements: 3
Empty? False
Full? False
Search for 53: Found.
Search for 19: Not found.
Search for 100: Not found.
Element at last index: 64
Delete remaining elements...
Number of elements: 0


TEST 5 ------------------------


Re-populate in reverse order...
Array 3 is now: same capacity 4
The Day After Tomorrow, 2004
Deadpool, 2016
Snatch, 2000
Fight Club, 1999

Your results:
Array is:
"The Day After Tomorrow" (2004)
"Deadpool" (2016)
"Snatch" (2000)
"Fight Club" (1999)

Capacity: 4
Number of elements: 4
Empty? False
Full? True
Element at last index: "Fight Club" (1999)

Delete first element...
After deletion:
"The Day After Tomorrow" (2004)
"Deadpool" (2016)
"Snatch" (2000)

Delete last element...
Array is:
"The Day After Tomorrow" (2004)
"Deadpool" (2016)

Number of elements: 2
Empty? False
Full? False
Element at last index: "Deadpool" (2016)

Delete remaining elements...
Number of elements: 0

Press any key to continue . . .
</output>
Last edited on
error: ‘DArray’ does not name a type
error: ‘ostream’ does not name a type
error: ‘Movie’ does not name a type
error: ‘string’ does not name a type
error: ‘Movie’ has not been declared

I want to simply compile your code (and that does compile correctly) execute and have your erroneous output.
as you've got several files, upload to github or similar.
also, would need an example input with the correct expected output.


1
2
3
4
			for (int i = pos; i < numOfElem; i++)
			{
				a[i] = a[++i];
			}
¿why don't simply write a[i] = a[i+1];?
you are modifying `i' twice: in the assignment and in the loop increment
also, I think that's undefined as you don't know the order of evaluation of the operands
and your last element will go out of bounds
In addition to ne555's comments, you need to move line 59 to 49: you should decrement the number of elements only if you find and delete the item.

It helps to approach software development with the idea that you need methods to help do the work. In this case I suggest two:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Find the index of the element.  Return the index or -1 if not found
template <typename T>
int DArray<T>::find(const T&element)
{
	for (int i=0; i<numElements; ++i) {
		if (a[i] == element) return i;
	}
	return -1;
}

// Delete the item at the given index
template <typename T>
void deleteIndex(int idx)
{
	for (;idx; idx < numElements-1; ++idx) {
		a[idx] = a[idx+1];
	}
	--numElements;
}



Now search, which appears to return a bool, becomes:
1
2
template <typename T>
bool DArray<T>::search(const T&element) { return find(element) >= 0; }


and deleteElement() becomes (after changing it to return a bool indicating whether it deleted anything:
1
2
3
4
5
6
7
8
9
10
template <typename T>
bool DArray<T>::deleteElement(const T& deleteElem)
{
	int pos = find(deleteElem);
	if (pos >= 0) {
		deleteIndex(pos);
		return true;
	}
	return false;
}


Finally, I see that addElement doesn't check the bounds or expand the array. Is that your intention?
Topic archived. No new replies allowed.