Manipulating an array (creating destructor, shifting elements forward/back and printing the array)

Need help finishing up the yet to be defined functions in this chunk of code.

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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <iostream>
using namespace std;
// You need to define the functions remove (int value), moveTowardEnd (int index), moveTowardFront (int index),  ~Array(), and print ().
// Class definition
class Array
{
private:
	int* arr;
	int capacity;
	int size;
	void moveTowardFront(int index);
	void moveTowardEnd(int index);
public:
	Array(int capacity);
	~Array();
	void insert(int value);
	void remove(int value);
	void print() const;
};

// Constructor
Array::Array(int cap)
	:capacity(cap)
{
	arr = new int[capacity];
	size = 0;
}
// Destructor
Array :: ~Array()
{
	//YOUR CODE
	

}
// Insert member function
void Array::insert(int value)
{
	if (size == 0)
	{
		arr[0] = value;
		size++;
		return;
	}
	int index = 0;
	while (value > arr[index] && index < size)
	{
		index++;
	}
	moveTowardEnd(index);
	arr[index] = value;
	size++;
}
// Delete member function
void Array::remove(int value)
{
	int index = 0;
	while (value != arr[index] && index < size)
	{
		index++;
	}
	if (index == size)
	{
		cout << value << " is not in the list. ";
		cout << "No removal." << endl;
		return;
	}
	moveTowardFront(index++);
	size--;
}
// Print member function
void Array::print() const
{
	//YOUR CODE
	
}
// Helper function to move the elements of array towards end
void Array::moveTowardEnd(int index)
{
	//YOUR CODE
	

}
// Helper function to move the elements of array towards front
void Array::moveTowardFront(int index)
{
	//YOUR CODE
	


}

int main()
{
	// Declaration of any array of capacity 20
	Array array(20);
	// Inserting some elements and printing array
	array.insert(15);
	array.insert(13);
	array.insert(10);
	array.insert(14);
	array.insert(11);
	array.insert(17);
	array.insert(14);
	cout << "Printing array after insertions: " << endl;
	array.print();
	cout << endl;
	// Removing two elements and printing array
	array.remove(13);
	array.remove(11);
	cout << "Printing array after removals: " << endl;
	array.print();
	cout << endl;
	// Inserting two more elements and printing array
	array.insert(8);
	array.insert(22);
	cout << "Printing array after more insertion" << endl;
	array.print();
	cout << endl;
	// Try to remove an element, which is not in the array
	array.remove(31);
	return 0;
}
Last edited on
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
moveTowardEnd( index )
{
    /*
    s = size
    WHILE s > index :
        arr[s+1]  = arr[s]
        s = s - 1
    */
}

moveTowardFront( index )
{
    // Similar moveTowardEnd()
}

print()
{
    /*
    FOREACH number IN arr:
       std::cout << number << '\n';
    */
}

~Array()
{
    // deallocate arr
}
Last edited on
Topic archived. No new replies allowed.