C++ Help

eclipse831
Assignment2Q4.pdf

Q4 (70 pts). In this question, you will write your own container class named MyCon similar to

vector class in C++. Your class must be a template class which will contain its elements in an

array named myArray. Since this class will be a template class, you should include the function

definitions at the end of the header file. You can also check the Stack example in Chapter 9. You

should keep the size and capacity of your container in two separate attributes named mySize

and myCapacity. Therefore, your class will have three data members: one generic dynamic

array (you should use pointer representation as given in the Stack example) and two integers.

In this container, the elements will always be sorted in increasing order. As the function

members of your class write the following functions:

a. (10 pts) Write a constructor that accepts one integer parameter as the capacity of your

container, creates a new array with the given capacity, and stores new array in myArray.

The constructor should also set myCapacity to the parameter value and mySize to 0. If

the parameter is not a positive number, you should set the capacity as 10 and create the

array with 10 elements.

b. (5 pts) Write getters for size (getSize) and capacity (getCapacity).

c. (15 pts) Write add function that accepts an element having generic type and adds this

element to the appropriate place in myArray. Note that the elements must always be

sorted. For instance, if the container elements are [3, 7, 9, 12] and the parameter of add

function is 5, it will be added as the second element. The elements will be [3, 5, 7, 9, 12]

after addition. You should also update the size (mySize) accordingly. The function should

return nothing. If the capacity is full, you should

 double the capacity by creating a new array,

 copy existing elements into new array,

 add the element into appropriate place,

 destroy old array,

 store the new array in myArray,

 update myCapacity.

d. (15 pts) Write remove function that accepts an element having generic type, searches it

in the array and removes all occurrences of that element in the array. You should shift

the elements after removal. For instance, if the elements are [3, 5, 5, 7, 9, 12], and the

parameter of remove function is 5, the container elements will be [3, 7, 9, 12] after the

removal. You should also update the size (mySize) accordingly. The function should

return nothing.

e. (5 pts) Write a function named display to print the elements in the container separated

by a space. Note that you should not print all array content, you should print the

elements in the container. For instance, when you create an integer container with

capacity 100, myArray will contain 100 zeros, but you should not display them.

Therefore, you should iterate your loop from 0 to mySize-1. The function should return

nothing.

f. (5 pts) Write a function named sum to return the sum of the elements in the container.

g. (5 pts) Write a function named at that accepts an integer parameter index and returns

the element at the given index.

h. (5 pts) Write a function named search that accepts an element having generic type as a

parameter and searches it in the container. The function should return true or false.

i. (5 pts) Write a function named clear that does not accept any parameter and clears the

container by setting size as 0. The function should return nothing.

An example driver class (MyConDriver.cpp) and its output are given below.

Size of b: 0

Capacity of b: 10

4

2 4

2 4 9

2 4 4 9

2 4 4 8 9

1 2 4 4 8 9

1 2 4 4 8 9 9

a.sum(): 37

a.at(3): 4

1 2 8 9 9

1 2 8 9 9

1: FOUND.

2: FOUND.

3: NOT FOUND.

4: NOT FOUND.

5: NOT FOUND.

6: NOT FOUND.

7: NOT FOUND.

8: FOUND.

9: FOUND.

10: NOT FOUND.

Size of a: 0

Capacity of a: 12

#include <iostream> #include "MyCon.h" using namespace std; int main() { MyCon<int> a(3); MyCon<double> b(-10); cout << "Size of a: " << a.getSize() << endl; cout << "Capacity of a: " << a.getCapacity() << endl; cout << "Size of b: " << b.getSize() << endl; cout << "Capacity of b: " << b.getCapacity() << endl; a.add(4); a.display(); a.add(2); a.display(); a.add(9); a.display(); a.add(4); a.display(); a.add(8); a.display(); a.add(1); a.display(); a.add(9); a.display(); cout << "a.sum(): " << a.sum() << endl; cout << "a.at(3): " << a.at(3) << endl; a.remove(4); a.display(); a.remove(7); a.display(); for (int i = 1; i <= 10; i++) { if (a.search(i)) { cout << i << ": FOUND." << endl; } else { cout << i << ": NOT FOUND." << endl; } } a.clear(); a.display(); cout << "Size of a: " << a.getSize() << endl; cout << "Capacity of a: " << a.getCapacity() << endl; }