Expanding/shrinking a dynamic array - c++

I have an assignment to create a template container class with a dynamic array that can be increased and decreased. When the array is displayed, I get either garbage numbers or no numbers at all for the elements in the array where it was expanded(so if it expanded on the fifth element, the value would turn to junk after I enter the sixth). When I try to remove elements I get a Debug Assertion Failed error.
Here is my code:
template <class T> class container {
public:
container(); // constructor
// Post-condition: count is set to -1 and dynamic array size set to 5
~container(); //destructor
void insert(T &n);
// Pre-condition: a value is passed to the function
// Post-condition: if data array is not full, increment count by 1 and insert the value in the array
// Otherwise, display "Container full! No insertion is made."
void remove();
//Post-condition: if data array is not empty, remove the data[count] element in data array and decrement count by 1;
//otherwise, display a message "Container empty! Nothing is removed from it."
void display();
// Post-condition: if the array is not empty, displays all values in data array similar to the sample output;
//Otherwise, display the message “Container is now empty!"
void fillarray(container c);
//pre-condition: a container c is passed to the function
//post-condition: dynamic array of chosen type is created and filled continuously with user entered values
private:
bool empty;
//Post-condition: returns true is the array is empty, otherwise returns false
T *data; // dynamically allocated array used to store or contain the inserted values
int count; // indicates how many values have been inserted
int max;
};
template <class T> container<T>::container()
{
count = -1;
max = 5;
data = new T[max];
assert(data != NULL);
}
template <class T> container<T>::~container()
{
delete [] data;
}
template <class T> void container<T>::insert(T &n)
{
if (count >= (max - 1))
{
max = max * 2;
cout << "\nContainer full! Array size is increased by " << max/2 << ".";
T *temp = new T[max];
assert(temp != NULL);
for (int i = 0; i < count; i++)
temp[i] = data[i];
delete [] data;
data = temp;
count++;
data[count] = n;
}
else
count++;
data[count] = n;
}
template <class T> void container<T>::remove()
{
empty = count < 0;
if (empty == 1)
{
cout << "\nContainer empty! Nothing is removed from it.";}
else
{
count--;
T *temp1 = new T[max];
assert(temp1 != NULL);
for (int i = 0; i < count; i++)
temp1[i] = data[i];
delete [] data;
data = temp1;
}
}
template <class T> void container<T>::display()
{
empty = count < 0;
if (empty == 1)
{
cout << "\nContainer is now empty!";}
else
{
for (int i = 0; i <= count; ++i)
cout << " " << data[i];
}
}
template <class T> void container<T>::fillarray(container c)
{
char ans;
do
{
T value;
cout << "\nEnter a value:";
cin >> value;
c.insert(value);
cout << "\nAfter inserting, the \"container\" contains:" << endl;
c.display();
cout << "\nEnter more? (Y/y or N/n)";
cin >> ans;
} while (ans == 'Y' || ans == 'y');
for (int i = 0; i <= count; i++)
{
c.remove();
cout << "\nAfter removing a value from it, the \"container\" contains:" << endl;
c.display();
cout << endl;
}
}
// The main driver function to be used to test implementation
int main()
{
char choice;
cout << "\nEnter S for string container, D for double";
cin >> choice;
if (choice == 'S' || choice == 's')
{
container<string> c;
c.display();
c.fillarray(c);
}
else if(choice == 'D' || choice == 'd')
{
container<double> c;
c.display();
c.fillarray(c);
}
return 0;
}

template <class T> void container<T>::fillarray(container c)
{
//...
}
This function actually involves two container<T> objects: *this and c.
Since you "pass by value" (the function parameter is not a reference), c in fillarray is created as a copy of the original c in main. In fillarray you modify c, which deletes and changes c.data, but this->data still contains the dangling pointer to the original storage. Before long, you get undefined behavior; luckily enough bad things happened you could tell something was wrong.
Per the Rule of Three (Plus Two), if a class has a destructor, you probably should not allow the compiler to generate the default copy constructor and copy assignment, and you may want to consider implementing a move constructor and move assignment.
The easiest, and sometimes best, way of meeting that rule is to disable copying:
template <class T> class container
{
public:
container(container const&) = delete;
container& operator=(container const&) = delete;
//...
};
Then the compiler will make sure you don't accidentally make a copy and get yourself into this sort of trouble.

You make life confusing by count referring to maximum index rather than the number of entries. Using your scheme though, this line in insert is odd:
for (int i = 0; i < count; i++)
As that won't copy the final entry.

Related

C++ Array Based Stack

I am trying to double a stack once it gets full. I have tried calling the copy constructor, and when I call it the stack doesn't keep pushing words.
The code run perfectly before it gets full, but once it gets full is where my problem occur. What am I doing wrong?
#include<iostream>
#include<fstream>
#include<string>
//#include "ArgumentManager.h"
#include "Stack.h"
using namespace std;
int main(int argc, char** argv){
//ArgumentManager am(argc, argv); //Instantiating parser for command line arguments
//ReAdInG tHe InPuT fIlE nAmE
//ifstream infile(am.get("A").c_str()); // file to read from, getting name from command line
//ofstream outfile(am.get("C").c_str()); // output file, getting name from command line
ifstream infile;
ofstream outfile;
arrayStack<string> firstStack(10);
arrayStack<string> secondStack(firstStack);
firstStack.stackInitialize();
infile.open("input.txt");
outfile.open("output.txt");
//iF tHe FiLe IsNt ReAd
if(!infile){
std::cout << "ErRor[Input file not Found] YoU hAd OnE jOb....ShAmE" << endl;
std::cout << "ExItInG PrOgRaM!!! GoOdByE" << endl;
return 0;
};
string tester; // to get the words
//READ FROM INFILE AND OUTPUT TO OUTFILE
while(infile >> tester){
for(int i = 0; i < tester.size(); ++i)
{ // CHECK IF A SPECIAL CHARACTER IS ON THE FILE
if(!((tester[i] >= 'a' && tester[i] <= 'z')||(tester[i] >= 'A' && tester[i]<= 'Z')))
{
tester[i] = '\0';
}
}
firstStack.push(tester);
};
while(!firstStack.stackIsEmpty())
{
string b = firstStack.top();
outfile << b << " ";
cout << b << " ";
if(firstStack.stackIsFull()){
secondStack.push(tester)
};
firstStack.pop();
}
infile.close();
outfile.close();
return 0;
}
Also I have tried to call the copy constructor in the push function such as:
template <class Type>
void arrayStack<Type>::push(const Type& word){
if(topStack != maxStackSize){
list[topStack] = word; // adding a new word to the STACK
topStack++;
}
else
cout << "YOU KNOW YOU CAN'T ADD TO A FULL STACK............SHAME" << endl;
arrayStack<Type> newArrayStack(maxStackSize*2);
for(int i = 0; i < maxStackSize; i++){
newArrayStack.push(list[i]);
}
newArrayStack.push(word);
stackCopy(newArrayStack);
}
It doesn't work either.
Below is the Stack Template
//ARRAY BASED STACK TEMPLATE
#ifndef H_ArrayStack
#define H_ArrayStack
#include <iostream>
using namespace std;
template <class Type>
class arrayStack{
private:
int maxStackSize; // the maximum height of the STACK
int topStack; // the top of the STACK
void stackCopy(const arrayStack<Type>& newArrayStack);
Type *list; // array based needs pointer to hold the stack element
public:
const arrayStack<Type>& operator=(const arrayStack<Type>&);
void stackInitialize(){ topStack = 0;}; //Ensure the array stack is empty
bool stackIsEmpty() const{return(topStack == 0);}; //check if stack is empty, is const so will not be messed with
bool stackIsFull() const{return topStack == maxStackSize;}; // just like line 8 except check if it is full
void push(const Type& word); // add a word to the array STACK
void pop(); //remove a word from the array and increment the top
Type top() const; //returns the top of the STACK
arrayStack(int size); //the default constructor
arrayStack(const arrayStack<Type>& newArrayStack); // the copy constructor which allows a new STACK
~arrayStack(){delete [] list;}; // it is an array so to ensure no memory leaks the stack must be deleted after use
};
template <class Type>
void arrayStack<Type>::push(const Type& word){
if(topStack != maxStackSize){
list[topStack] = word; // adding a new word to the STACK
topStack++;
}
else{
cout << "YOU KNOW YOU CAN'T ADD TO A FULL STACK............SHAME" << endl;
int size = maxStackSize;
maxStackSize *= 2;
Type *temp = new Type[maxStackSize]; // create temp and double the size of list
for(int i = 0; i < size; i++){ // copy over all the values
temp[i] = list[i];
}
delete [] list; // delete the list
list = temp; // point to the resized list
list[topStack] = word;
topStack++;
}
}
template <class Type>
void arrayStack<Type>::pop(){
if (!stackIsEmpty()){
topStack--;
}
}
template <class Type>
Type arrayStack<Type>::top() const{
if(topStack == 0){
return 0;
}
else
return list[topStack - 1];
}
template <class Type>
arrayStack<Type>::arrayStack(int size){
maxStackSize = size;
topStack = 0;
list = new Type[maxStackSize];
}
template <class Type>
void arrayStack<Type>::stackCopy(const arrayStack<Type>& newArrayStack){
maxStackSize = newArrayStack.maxStackSize;
topStack = newArrayStack.topStack;
list = new Type[maxStackSize];
for(int j = 0; j < topStack; j++)
list[j] = newArrayStack.list[j];
}
template <class Type>
arrayStack<Type>::arrayStack(const arrayStack<Type>& newArrayStack){
stackCopy(newArrayStack);
}
template <class Type>
const arrayStack<Type>& arrayStack<Type>::operator=(const arrayStack<Type>& newArrayStack){
if(this != &newArrayStack)
stackCopy(newArrayStack);
return *this;
}
#endif
From:
arrayStack<Type> newArrayStack(maxStackSize*2);
for(int i = 0; i < maxStackSize; i++){
newArrayStack.push(list[i]);
}
newArrayStack.push(word);
stackCopy(newArrayStack);
To:
int size = maxStackSize;
maxStackSize *= 2;
Type *temp = new Type[maxStackSize]; // create temp and double the size of list
for(int i = 0; i < size; i++){ // copy over all the values
temp[i] = list[i];
}
delete [] list; // delete the list
list = temp; // point to the resized list
list[topStack] = word;
topStackk++;
Try this in you push function this will double the size of the stack. Like someone mentioned in the comments you have errors in your copy constructor and stackcopy function

Heap memory corruption c++ templated class

I'm just a humble student looking to further my knowledge about the C++ language. My professor isn't helping! I think the title along with comments in-code explain my issues clearly.
#ifndef H_Htable
#define H_Htable
//****************************************************************
// Author: D.S. Malik
//
// This class specifies the members to implement a hash table as
// an ADT. It uses quadratic probing to resolve collisions.
//****************************************************************
#include <iostream>
#include <cassert>
using namespace std;
template <class elemType>
class hashT
{
public:
void insert(int hashIndex, const elemType& rec);
//Function to insert an item in the hash table. The first
//parameter specifies the initial hash index of the item to
//be inserted. The item to be inserted is specified by the
//parameter rec.
//Postcondition: If an empty position is found in the hash
// table, rec is inserted and the length is incremented by
// one; otherwise, an appropriate error message is
// displayed.
//sequential search
bool search(int& hashIndex, const elemType& rec, bool found = false) const;
//Function to determine whether the item specified by the
//parameter rec is in the hash table. The parameter hashIndex
//specifies the initial hash index of rec.
//Postcondition: If rec is found, found is set to true and
// hashIndex specifies the position where rec is found;
// otherwise, found is set to false.
bool isItemAtEqual(int hashIndex, const elemType& rec) const;
//Function to determine whether the item specified by the
//parameter rec is the same as the item in the hash table
//at position hashIndex.
//Postcondition: Returns true if HTable[hashIndex] == rec;
// otherwise, returns false.
void retrieve(int hashIndex, elemType& rec) const;
//Function to retrieve the item at position hashIndex.
//Postcondition: If the table has an item at position
// hashIndex, it is copied into rec.
void remove(int hashIndex, const elemType& rec);
//Function to remove an item from the hash table.
//Postcondition: Given the initial hashIndex, if rec is found
// in the table it is removed; otherwise, an appropriate
// error message is displayed.
void print() const;
//Function to output the data.
//provide for both int and string data types in the hash table
hashT(int size = 101, bool isIntTable = true);
//constructor
//Postcondition: Create the arrays HTTable and indexStatusList;
// initialize the array indexStatusList to 0; length = 0;
// HTSize = size; and the default array size is 101.
~hashT();
//destructor
//Postcondition: Array HTable and indexStatusList are deleted.
private:
elemType *HTable; //pointer to the hash table
int *indexStatusList; //pointer to the array indicating the
//status of a position in the hash table
int length; //number of items in the hash table
int HTSize; //maximum size of the hash table
};
template <class elemType>
void hashT<elemType>::insert(int hashIndex, const elemType& rec)
{
int pCount;
int inc;
pCount = 0;
inc = 1;
while (indexStatusList[hashIndex] == 1
&& HTable[hashIndex] != rec
&& pCount < HTSize / 2)
{
pCount++;
hashIndex = (hashIndex + inc) % HTSize;
inc = inc + 2;
}
if (indexStatusList[hashIndex] != 1)
{
HTable[hashIndex] = rec;
indexStatusList[hashIndex] = 1;
length++;
}
else
if (HTable[hashIndex] == rec)
cerr << "Error: No duplicates are allowed." << endl;
else
cerr << "Error: The table is full. "
<< "Unable to resolve the collision." << endl;
}
//sequential search
template <class elemType>
bool hashT<elemType>::search(int& hashIndex, const elemType& rec, bool found) const
{
for (int i = 0; i < HTSize; i++) {
if (HTable[i] == rec) { //assuming no repeat data
found = true;
hashIndex = i;
break;
}
}
return found;
}
template <class elemType>
bool hashT<elemType>::isItemAtEqual(int hashIndex, const elemType& rec) const
{
//first make sure the item has not been removed
if (indexStatusList[hashIndex] != -1) {
//make equality comparison
if (HTable[hashIndex] == rec)
return true;
else
return false; //comparison fails
}
else
{
std::cerr << "isItemEqual(): Item has been removed" << endl;
return false;
}
}
template <class elemType>
void hashT<elemType>::retrieve(int hashIndex, elemType& rec) const
{
if (indexStatusList[hashIndex] != -1)
rec = HTable[hashIndex];
else
std::cerr << "retrieve(): item has been removed" << endl;
}
template <class elemType>
void hashT<elemType>::remove(int hashIndex, const elemType& rec)
{
//make sure the item hasn't already been removed
if (indexStatusList[hashIndex] != -1) {
bool isInList = hashT<elemType>::search(hashIndex, rec);
//update the status
if (isInList)
{
indexStatusList[hashIndex] = -1;
length--; //decrement length
}
else
std::cerr << "hasT::remove() could not remove the specified item" << endl;
}
else
{
std::cerr << "remove(): Item has already been removed from the table" << endl;
}
}
template <class elemType>
void hashT<elemType>::print() const
{
std::cout << "Hash Table Data: " << endl;
for (int i = 0; i < (length - 5); i++) {
elemType item = HTable[i];
//std::cout << item << " ";
}
}
template <class elemType>
hashT<elemType>::hashT(int size, bool isIntTable)
{
HTable = new elemType[]; //is this right? HTable is an array just like indexStatusList
HTSize = size;
length = 0;
indexStatusList = new int[0]; //I think this one works?
}
template <class elemType>
hashT<elemType>::~hashT() //deleting always causes heap errors!!!
//says writing to unallocated memory -- debugging shows otherwise
{
//delete[] HTable;
//delete[] indexStatusList; //still causing errors -- error now not associated with any particular line (of my code)
}
#endif
I've kept increasing my bounds checking security when instantiating hashT in main. I'm convinced it is because my data members are being initialized incorrectly. This is one error message I get after trying a few things: "Unhandled exception at 0x773F627C (ntdll.dll) in exercise7Chap9.exe: 0xC0000374: A heap has been corrupted (parameters: 0x77426480)."
finally, here's main just in case:
#include <iostream>
#include "hashT.h"
int main() {
//add one item and test for equality
//hashT<int> ht = hashT<int>(20);
//ht.insert(0, 1);
//bool itemInsertSuccess = ht.isItemAtEqual(0, 1);
//if (itemInsertSuccess)
// std::cout << "first test has succeeded" << endl;
//else
// std::cout << "first test has failed" << endl;
////remove item and make sure isItemEqual returns false
//ht.remove(0, 1);
//bool itemRemoved = ht.isItemAtEqual(0, 1);
//if (!itemRemoved)
// std::cout << "second test passed" << endl;
//else
// std::cout << "second test failed" << endl;
//add many items then make sure search() works
hashT<int> ht1 = hashT<int>(51);
for (int i = 0; i < 10; i++)
ht1.insert(i, i);
int indx = -1;
ht1.search(indx, 0);
if (indx == 25)
std::cout << "Test 3 has passed" << endl;
else
std::cout << "Test 3 has failed" << endl;
//print data then test retrieve() and print a single item
/*ht1.print();
int item = -1;
ht1.retrieve(10, item);
if (item != -1) {
std::cout << item << endl;
std::cout << "test 4 has passed" << endl;
}
else
std::cout << "test 4 has failed" << endl;
hashT<int> HtRetrieve = hashT<int>(10);
HtRetrieve.insert(0, 0);
int it = -1;
HtRetrieve.retrieve(0, it);
std::cout << it << endl;*/
char stop;
std::cin >> stop;
//return 0;
}
In your case here, scrap the variable isIntTable. Templates are a compile-time construct and run-time values won't influence how the template is compiled into a class in any way.
Then, in your constructor, just use the template type as the one you are allocating.
template <class elemType>
hashT<elemType>::hashT(int size)
{
HTable = new elemType[size];
length = 0;
indexStatusList = new int[0];
}
However, this could be much better. Consider using initialisation instead of assignation:
hashT<elemType>::hashT(int size) :
HTable{new elemType[size]},
length{size},
indexStatusList{int[size]} { /* empty constructor */ }
And it can be even better. Consider using smart pointer instead of raw owning pointers and vectors instead of dynamic allocated array:
template<typename T>
struct hashT {
// using the right type for size
hashT(std::size_t size) : pointerToOneT{std::make_unique<T>()}, HTable(size) {}
// make_unique is the `new` for unique pointers
// no need for destructors, the vector and unique_ptr are freeing themselves
private:
std::unique_ptr<T> pointerToOneT;
std::vector<T> HTable;
std::vector<int> indexStatusList;
};
If you don't want to use std::vector, you can always use std::unique_ptr<T[]>, which is a dynamically allocated array that free itself.
template<typename T>
struct hashT {
// using the right type for size
hashT(std::size_t size) :
HTable{std::make_unique<T[]>(size)},
indexStatusList(std::make_unique<int[]>(size)) {}
private:
std::unique_ptr<T[]> HTable;
std::unique_ptr<int[]> indexStatusList;
};
EDIT
In your actual destructor, the problem is that you initialized the int* with the new[] but you are using delete. To delete an array, you must use delete[]
template <class elemType>
hashT<elemType>::~hashT()
{
delete HTable;
delete[] indexStatusList;
}
Turns out this heap corruption was caused by my lack of understanding dynamic arrays in C++.
My incorrect initialization of the arrays HTable and indexStatusList were: Htable = new elemType();, HTable = new elemType[]; and indexStatusList = new int[0];
I simply needed to add the size as an argument (never seen a size argument passed in brackets before!)
Here is the working constructor:
//constructor
template <class elemType>
hashT<elemType>::hashT(int size)
{
HTable = new elemType[size]; // pass size so the compiler knows what to allocate and deallocate
HTSize = size;
length = 0;
indexStatusList = new int[size];
}
working destructor:
template <class elemType>
hashT<elemType>::~hashT()
{
delete[] HTable;
delete[] indexStatusList;
}

Initializing a dynamic array of stl lists for a Hash Table (Separate Chaining)

I am having a problem with my program throwing an exception when inserting data at specific index's of my array. I am using a hash table, and trying to use the STL list in an array of pointers to another class that contains the data.
Since this is a Hash Table, I'm avoiding using the vector class since the size of the array should be constant. (I know the initial size that I want)
MCVE (For you) :
#ifndef HASHTABLE_H
#define HASHTABLE_H
#include <list>
#include <cstring>
#include <stdlib.h>
template <typename T1>
class HashTable
{
public:
HashTable();
void Insert(T1 var)
int FindPrime(int);
int HashFunction(string);
private:
int prime;
list<T1> *List;
int LF;
}
#endif
template <typename T1>
HashTable<T1>::HashTable()
{
List[i] = list<T1>();
}
template <typename T1>
int HashTable<T1>::FindPrime(int num)
{
bool isNotPrime = false;
for (int i=num; i < num + 25; ++i)
{
for (int j=2; j<i; ++j)
{
if (i % j == 0)
{
isNotPrime = true;
}
}
if (isNotPrime == false)
{
prime = i;
return prime;
break;
}
isNotPrime = false;
}
prime = num;
return prime;
}
template <typename T1>
long HashTable<T1>::HashFunction(string key)
{
long numkey = 0;
char word[1000];
strcpy(word,key.c_str());
word[sizeof(word) - 1] = NULL; //Ensure null is at last index of word
for(int i = 0; word[i] != NULL; ++i)
{
numkey = numkey + (word[i] * 101 + word[i]);
}
numkey = numkey % prime;
return numkey;
}
template <typename T1>
void HashTable<T1>::Insert(T1 var)
{
int index = HashFunction(var -> getKey());
List[index].push_front(var);
++LF;
cout << "Load Factor: " << LF << endl << endl;
}
from a seperate class that is determining what to do with the data:
file >> num;
hash.FindPrime(num);
file >> letter; // Get letter from file so we know what to do
if(letter == 'D' || letter == 'd') //If the letter is D, then add a new DNA Node with corresponding data to the STL List
{
file >> Label >> ID >> Seq >> Length >> Index;
cout << "Note: Adding " << Label << " ..." << endl << endl;
Sequence* ptr = new DNA(Label, ID, Seq, Length, Index);
hash.Insert(ptr);
ptr = NULL;
delete ptr;
}
Sequence Class is the base class for Several inherited classes (DNA is one of them)
Type of List[i] is list<T1>. That's why the compiler doesn't allow you to use:
List[i] = new list<T1>;
// Trying to assign a list<T1>* to a list<T1>.
You can use
List[i] = list<T1>();
or
list<T1>** List;

How to delete an item in an array while using a Class Template?

I'm trying to delete "GoodBye" from the Remove function and then print a list with it missing.
I'm getting an error saying:
Error 1 error C2440: 'delete' : cannot convert from 'std::string' to 'void*
#include <iostream>
#include <string>
using namespace std;
const int SIZE = 5;
template <class New_Type>
class Array_Class
{
public:
Array_Class();
~Array_Class();
void Add(New_Type item);
int Search(New_Type item);
void Remove(New_Type item);
void Print();
private:
New_Type *A;
New_Type word;
int count;
};
template <class New_Type>
Array_Class<New_Type>::Array_Class()
{
cout << "You are inside the default constructor.\n";
cout << "New_Type has a size of " << sizeof(New_Type) << " bytes\n\n";
count = 0;
A = new New_Type[SIZE];
}
template <class New_Type>
Array_Class<New_Type>::~Array_Class()
{
cout << "The Destructor has been called.\n\n";
delete[] A;
count = 0;
A = 0;
}
template <class New_Type>
void Array_Class<New_Type>::Add(New_Type item)
{
if (count<SIZE)
{
A[count++] = item;
}
else
{
cout << "The array is full.\n";
}
}
template <class New_Type>
int Array_Class<New_Type>::Search(New_Type item)
{
int i;
for (i = 0; i<count; i++)
{
if (item == A[i])
{
return i;
}
}
return -1;
}
item is Goodbye. word will save the copy that gets deleted.
template <class New_Type>
void Array_Class<New_Type>::Remove(New_Type item)
{
int i;
word = item;
for (i = 0; i < count; i++)
{
if (item == A[i])
{
delete A[i];
}
}
}
template <class New_Type>
void Array_Class<New_Type>::Print()
{
int i;
for (i = 0; i<count; i++)
{
cout << "A[" << i << "] = " << A[i] << endl;
}
}
The main function which will add "GoodBye" and other words to my_String.
int main()
{
Array_Class<string> my_String;
Array_Class<int> my_Ints;
Array_Class<char> my_Chars;
my_String.Add("Hello");
my_String.Add("GoodBye");
my_String.Add("ComeHere");
my_String.Add("SayNo");
my_Chars.Add('a');
my_Chars.Add('b');
my_Chars.Add('c');
my_Chars.Add('d');
my_Chars.Add('e');
my_Chars.Add('f');
my_Chars.Add('g');
my_String.Print();
my_Ints.Print();
my_Chars.Print();
cout << endl;
my_String.Search("Hello");
my_String.Search("SayNo");
my_String.Remove will remove GoodBye from my_String:
my_String.Remove("GoodBye");
my_String.Print();
return 0;
}
The problem is that your Remove function should not be issuing any calls to delete. What it should be doing is shifting the elements "up" by one and decreasing the count member variable. This in effect "removes" the item from the Array.
To shift the elements up, you write a loop where you replace element i with element i+1, where you start the loop at the item you want to remove.
However your array is dynamically allocated, you can't just call delete on particular elements of it. This is a contiguous block of memory. You can do what #PaulMcKenzie said - To find element that matches element passed as an argument to Remove function and then shift to the left remaining array elements, then decrease count member variable. I solved it, but since this is homework posting it wouldn't be wise. Here's a very weird pseudo-code of mine. I hope you understand that concept.
//array elements : Hello, GoodBye, ComeHere, SayNo
my_String.Remove("GoodBye");
// found index of element to remove = 1;
// decrement count
// loop from saved index through count-1:
// A[i] = A[i+1];
// There will be two iterations of this loop. here's how array would look like:
// 1st: array elements : Hello, ComeHere, ComeHere, SayNo
// 2nd: array elements : Hello, ComeHere, SayNo, SayNo
Then, because of decrementing count, last element won't be printed.
And in c++, for dynamic arrays std::vector is way to go.

Circular DeQueue implementation error

Basically a dynamic array, which has circular rotation when is full. You have access to every element and you can change it's value, however you can insert and remove only from both ends.(constant time). Most of the methods seem to be working fine, however at certain "push" numbers I get wrong output.
For example first input is 1,2,3 then I insert 4 at the end. Next output is: 2,3,4 However after I insert 5 at the end the output is 2, 3, 5
I have no idea what is causing this. I am posting the entire source code below (atleast the functions which have to do with the tests where the error must hide). There is some documentation in the file and an example of the error in case I haven't explained things clearly.
#include <iostream>
using namespace std;
template <typename Object>
class ArrayVector {
private:
int capacity; // capacity
int sz; // number of elements
Object* a;
int f; // start of the indexes
int b; // end of the indexes
public:
ArrayVector(int initCap);
~ArrayVector();
int size() const { return sz; }
bool isEmpty() const { return size() == 0; }
Object elemAtRank(int r);
void pushBack( const Object& e);
void pushFront(const Object& e);
void popBack();
void popFront();
};
template <typename Object> // constructor
ArrayVector<Object>::
ArrayVector(int initCap) {
capacity = initCap;
sz = 0;
a = new Object[capacity];
f = 0;
b = 0;
}
template <typename Object> // gets the element at a certain rank
Object ArrayVector<Object>:: elemAtRank(int r)
{
return a[(f + r) % sz]; // starting position in real array + r % number of elements
}
template <typename Object>
void ArrayVector<Object>:: pushBack( const Object& e)
{
if(sz == capacity && sz > 0) // if the array is full time to spin it
{
if(f == capacity){ // Handles the front.
f = 0; // if the front is equal to the capacity
// set it to zero, else increment
}else{
f++;
}
if(b == capacity){ //Handles the back
b = 0; //if the back is equal to the capacity
// cout<< "SC insert "<< e << " at "<< b <<endl;
a[b] = e;
}else{ // set it to zero, else increment
a[b] = e;
// cout<< "SC insert "<< e << " at "<< b <<endl;
b++;
}
}else{
a[b] = e;
// cout<< "insert "<< e << " at "<< b <<endl;
b++;
sz++;
}
}
template <typename Object>
void ArrayVector<Object>:: pushFront( const Object& e)
{
if(f == 0){
f = capacity-1;
}else{
f--;
}
a[f] = e;
if(sz< capacity)
sz++;
}
int main()
{
// Fill array and print it
cout << "Fill with numbers" << endl;
ArrayVector<int> asd(3);
asd.pushBack(1);
asd.pushBack(2);
asd.pushBack(3);
for(int i =0; i < asd.size(); i++)
cout << asd.elemAtRank(i) << endl;
//Test if it spins
cout << "BEGIN Spin TEST " << endl;
asd.pushBack(4);
cout << "First test is ok" << endl;
for(int i =0; i < asd.size(); i++)
cout << asd.elemAtRank(i) << endl;
// here the error comes
asd.pushBack(5);
cout << "On the second iteration things crash and burn" << endl;
for(int i =0; i < asd.size(); i++)
cout << asd.elemAtRank(i) << endl;
return 0;
}
In addition to your insertion times not matching your desired requirements, your issue is here:
template <typename Object>
void ArrayVector<Object>:: pushFront( const Object& e)
{
if(f == 0)
{
f = capacity-1;
}
else
{
f--;
}
a[f] = e; // problem lies here!
if(sz < capacity)
sz++;
}
You are figuring out where to do the insert, but you are not pushing the other elements around the vector; that is, you are just overwriting the element at the insertion point. If you wanted to push it onto the front, you would need to copy the other elements over 1 position each and then do your insert. A better solution (which would match your constant insertion time requirement) would be to implement it as a double-linked list. Pushing onto the ends would simply require the following pseudo-code:
void push_front(const Object& o)
{
if (size == capacity)
l.pop_back();
l.push_front(o);
}
If you really must use a contiguous memory block, you will not get constant-time insertion, but it would look like this:
// Assumptions: 0 is always the front, capacity-1 is always the maximum back
template <typename Object>
void ArrayVector<Object>:: pushFront( const Object& e)
{
// assume capacity > 0, move all the elements to the right one slot
for (int i = capacity - 1; i > 0; --i)
{
a[i] = a[i - 1];
}
a[0] = e;
if(sz < capacity)
sz++;
}