I've almost got it now, but I can't figure out some segmentation error that I'm thinking is being created, likely by an indexing error I can't see for some reason, inside the overloaded [] operator method, though there's a very small chance the error could be elsewhere.
Ignore the part about AdjustArray. That can be removed for testing.
BetterAdjustArray.cpp:
#include "BetterAdjustArray.h"
#include "AdjustArray.h"
#include <iostream>
using namespace std;
BetterAdjustArray::BetterAdjustArray()
{
array = new int[1];
int x;
for ( x = 0; x < 1; x++)
{
array[x] = 0;
}
setFilledSize(0);
setSize(1);
boolArray = new bool[1];
int y;
for (y = 0; y < 1; y++)
{
boolArray[x] = true;
}
}
BetterAdjustArray::~BetterAdjustArray()
{
delete[] boolArray;
delete[] array;
cout <<"BetterAdjustArray destructor called.\n";
}
void BetterAdjustArray::setSmallestIndexDeleted(unsigned int index)
{
this -> index = index;
}
unsigned int BetterAdjustArray::getSmallestIndexDeleted() const
{
return index;
}
int& BetterAdjustArray:: operator[] (unsigned int i)
{
if (i < getSmallestIndexDeleted())
{
return array[i];
}
else
{
while (getSmallestIndexDeleted() < i)
{
for (int x = getSmallestIndexDeleted(); x < (getFilledSize() -1); x++)
{
int temp = array[x+1];
array[x] = temp;
}
setFilledSize(getFilledSize() -1);
boolArray[getSmallestIndexDeleted()] = false;
for (int v = getSmallestIndexDeleted()+1; v < getFilledSize(); v++)
{
if (boolArray[v] == true)
{
setSmallestIndexDeleted(v);
cout<<"Smallest index deleted now is:" <<v <<"\n";
break;
}
}
}
if (getFilledSize() < static_cast<int>(getSize()/2))
{
int *newArray;
bool *newBoolArray;
newArray = new int[static_cast<int>(getSize()/2)];
newBoolArray = new bool[static_cast<int>(getSize()/2)];
int p;
// setFilledSize(static_cast<int>(getSize() /2));
for (p= 0; p < getFilledSize(); p++)
{
newArray[p] = array[p];
newBoolArray[p] = boolArray[p];
}
for (int w = getFilledSize(); w < static_cast<int>(getSize()/2); w++)
{
newArray[w] = 0;
newBoolArray[w] = true;
}
delete[] array;
array = newArray;
delete[] boolArray;
boolArray = newBoolArray;
setSize(static_cast<int>(getSize()/2));
}
return array[i];
}
}
void BetterAdjustArray::erase(unsigned int i)
{
if (i >= getFilledSize())
{
cout<<"No can do. \n";
return;
}
boolArray[i] = true;
if (i < getSmallestIndexDeleted())
setSmallestIndexDeleted(i);
}
void BetterAdjustArray::setSize(unsigned int size)
{
this->size = size;
}
unsigned int BetterAdjustArray::getSize() const
{
return size;
}
void BetterAdjustArray::setFilledSize(unsigned int filledSize)
{
this->filledSize = filledSize;
}
unsigned int BetterAdjustArray::getFilledSize() const
{
return filledSize;
}
void BetterAdjustArray::push_back(int m)
{
if (isFull() == true)
{
int *newArray;
bool *newBoolArray;
//had forgotten to instantiate those two
newArray = new int[getSize() * 2];
newBoolArray = new bool[getSize() * 2];
int k;
for (k = 0; k < getSize(); k++)
{
newArray[k] = array[k];
newBoolArray[k] = boolArray[k];
}
for (int v = getSize(); v < (getSize() * 2); v++)
{
newArray[v] = 0;
newBoolArray[v] = true;
}
newArray[getSize()] = m;
newBoolArray[getSize()] = false;
delete[] array;
delete[] boolArray;
array = newArray;
boolArray = newBoolArray;
setSize(getSize() * 2);
}
else
{
array[getFilledSize()] = m;
boolArray[getFilledSize()] = false;
}
setFilledSize(getFilledSize() + 1);
}
bool BetterAdjustArray::isFull() const
{
if (getFilledSize() == getSize())
return true;
return false;
}
void BetterAdjustArray::printArray() const
{
cout <<"[";
for (int m =0; m < (getFilledSize()-1) ; m++)
{
cout << array[m] << ",";
}
cout <<array[getFilledSize() -1];
cout <<"] \n";
}
int main()
{
AdjustArray arr;
arr.push_back(222);
arr.erase(0);
cout <<arr[0];
arr.push_back(11);
cout <<arr[0] << "\n";
arr.push_back(45);
cout<< arr[0] << "," << arr[1] << "\n";
arr.push_back(15);
arr.push_back(99);
arr.push_back(1111);
arr.erase(2);
arr.printArray();
BetterAdjustArray barr;
barr.push_back(91);
barr.push_back(33);
barr.push_back(45);
barr.push_back(999);
barr.push_back(15);
barr.push_back(121);
barr.push_back(555);
barr.push_back(777);
barr.push_back(888);
barr.push_back(10000);
barr.erase(2);
barr.erase(5);
barr.erase(7);
cout <<barr[8] << "\n";
barr.printArray();
barr.push_back(19);
barr.printArray();
}
It's saying
"Segmentation error after printing out all the AdjustArray stuff, so no problems with AdjustArray.
The problem could also be in push_back, though I'm thinking it's maybe my break statement in the overloaded [] method or an indexing error.
Please help. This is almost done and due in about exactly 2 hours! It's nearly done but I need help or I might not be able to finish it all the way in time!
Another thought would be because maybe I was supposed to set SmallestIndexDeleted() to 0 right away, but I don't think so.
Thanks.
-----Edit----
Found part of error, but still getting message.
Had forgotten to initialize some stuff. But it's still giving me the error, just in a different place.
Now it's printing:
[91,33,45,999,15,121,555,777,888,10000]
Smallest index deleted now is:5
Smallest index deleted now is:7
Segmentation fault
I was trying to set smallestIndexDeleted to be bigger than i.
Maybe I should have not tried that in the for loop. Let me see if changing that makes a difference....
Better but still not working quite as expected:
[91,33,45,999,15,121,555,777,888,10000]
Smallest index deleted now is:5
Smallest index deleted now is:7
Why isn't it adding 19 to the end and instead ending the program?