Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implement this function to replace all negative values in list by item using C++ cpp?? //The function should also return the number of replaced values

Implement this function to replace all negative values in list by item using C++ cpp??


//The function should also return the number of replaced values
int replaceNegative(ArrayList& list, int item)
{

}

int main()
{
ArrayList intList(10);
int c;
intList.insertEnd(3); intList.insertEnd(10); intList.insertEnd(4);
intList.insertEnd(-1); intList.insertEnd(3); intList.insertEnd(-12);
intList.insertEnd(8); intList.insertEnd(-40); intList.insertEnd(6);
intList.print(); //3 10 4 -1 3 -12 8 -40 6
c = replaceNegative(intList, 100);
cout <intList.print();//3 10 4 100 3 100 8 100 6
return 0;
}

#include

template
class ArrayList{
protected:
int length, maxSize;
Type* list;
public:
ArrayList(int);//constructor
bool isEmpty();
bool isFull();
int listSize();
int maxListSize();
const void print();
bool isItemAtEqual(int, const Type&);
void insertAt(int, const Type&);
void insertEnd(const Type&);
void removeAt(int);
void retrieveAt(int, Type&);
void replaceAt(int, const Type&);
void clearList();
int seqSearch(const Type&);
void insert(const Type&);//without duplication
void remove(const Type&);
ArrayList(const ArrayList&);//copy constructor
~ArrayList();
const ArrayList& operator=(const ArrayList&);
};

template
ArrayList::ArrayList(int size)
{
length = 0;
if (size < 0)
{
cout << "The array size must be positive. Creating of an array of size 100.\n";
maxSize = 100;
}
else
maxSize = size;
list = new Type[maxSize];
assert(list != NULL); // capture programming error
}

template
bool ArrayList::isEmpty()
{
return length == 0;
}

template
bool ArrayList::isFull()
{
return length == maxSize;
}

template
int ArrayList::listSize()
{
return length;
}

template
int ArrayList::maxListSize()
{
return maxSize;
}

template
const void ArrayList::print()
{
if (isEmpty())
cout << "Empty list\n";
else
{
for (int i = 0; i < length; i++)
cout << list[i] << " ";
cout << "\n";
}
}

template
bool ArrayList::isItemAtEqual(int pos, const Type& x)
{
if (pos < 0 || pos >= maxSize)
{
cout << "Valid positions must be in " << 0 << "..." << length - 1 << "\n";
return false;
}
else if (list[pos] == x)
return true;
else
return false;
}

template
void ArrayList::insertAt(int pos, const Type& x)
{
if (isFull())
cout << "Cannot insert in a full list.\n";
else if (pos < 0 || pos > length)
cout << "Valid positions for insertion are " << 0 << "..." << length << "\n";
else
{
for (int i = length; i > pos; i--)
list[i] = list[i - 1];
list[pos] = x;
length++;
}
}

template
void ArrayList::insertEnd(const Type& x)
{
if (isFull())
cout << "Cannot insert in a full list.\n";
else
{
list[length] = x;
length++;
}
}

template
void ArrayList::removeAt(int pos)
{
if (isEmpty())
cout << "Cannot remove from an empty list.\n";
else if (pos < 0 || pos >= length)
cout << "Valid positions for removal are " << 0 << "..." << length - 1 << "\n";
else
{
for (int i = pos; i < length - 1; i++)
list[i] = list[i + 1];
length--;
}
}

template
void ArrayList::retrieveAt(int pos, Type& x)
{
if (isEmpty())
cout << "Cannot retrieve from an empty list.\n";
else if (pos < 0 || pos >= length)
cout << "Valid positions for retrieving are " << 0 << "..." << length - 1 << "\n";
else
x = list[pos];
}

template
void ArrayList::replaceAt(int pos, const Type& x)
{
if (isEmpty())
cout << "Empty list.\n";
else if (pos < 0 || pos >= length)
cout << "Valid positions for replacement are " << 0 << "..." << length - 1 << "\n";
else
list[pos] = x;
}

template
void ArrayList::clearList()
{
length = 0;
}

template
int ArrayList::seqSearch(const Type& x)
{
if (isEmpty())
{
cout << "Cannot search in an empty list.\n";
return -1;
}
else
{
for (int i = 0; i < length; i++)
if (list[i] == x)
return i;
return -1;
}
}

template
void ArrayList::insert(const Type& x)//without duplication
{
if (length == maxSize) //or if(isFull())
cout << "Cannot insert in a full list.\n";
else if (length == 0) // or if(isEmpty())
list[length++] = x;
else
{
if (seqSearch(x) != -1)
cout << "Dupplication is not allowed\n";
else
list[length++] = x;
}
}

template
void ArrayList::remove(const Type& x)
{
if (length == 0) // or if(isEmpty())
cout << "Cannot remove from empty list\n";
else
{
int i = seqSearch(x);
if (i == -1)
cout << x << " does not exist in the list\n";
else
removeAt(i);
}
}

template
ArrayList::ArrayList(const ArrayList& otherList)
{
maxSize = otherList.maxSize;
length = otherList.length;
list = new Type[maxSize];
assert(list != NULL); // capture programming error
for (int i = 0; i < length; i++)
list[i] = otherList.list[i];
}

template
ArrayList::~ArrayList() // destructor
{
delete[] list;
}

template
const ArrayList& ArrayList::operator=(const ArrayList& otherList)
{
if (this != &otherList)
{
delete[] list;
maxSize = otherList.maxSize;
length = otherList.length;
list = new Type[maxSize];
assert(list != NULL);
for (int i = 0; i < length; i++)
list[i] = otherList.list[i];
}
return *this;
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Income Tax Fundamentals 2013

Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill

31st Edition

1111972516, 978-1285586618, 1285586611, 978-1285613109, 978-1111972516

More Books

Students also viewed these Programming questions

Question

What usage trends are affecting demand for smartphones?

Answered: 1 week ago

Question

Draw a Feynman diagram for the reaction n + v p + .

Answered: 1 week ago

Question

Explain in your own words the idea of subjective probability.

Answered: 1 week ago