Question
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
{
}
int main()
{
ArrayList
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 <
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
~ArrayList();
const ArrayList
};
template
ArrayList
{
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
{
return length == 0;
}
template
bool ArrayList
{
return length == maxSize;
}
template
int ArrayList
{
return length;
}
template
int ArrayList
{
return maxSize;
}
template
const void ArrayList
{
if (isEmpty())
cout << "Empty list\n";
else
{
for (int i = 0; i < length; i++)
cout << list[i] << " ";
cout << "\n";
}
}
template
bool ArrayList
{
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
{
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
{
if (isFull())
cout << "Cannot insert in a full list.\n";
else
{
list[length] = x;
length++;
}
}
template
void ArrayList
{
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
{
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
{
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
{
length = 0;
}
template
int ArrayList
{
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
{
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
{
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
{
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
{
delete[] list;
}
template
const ArrayList
{
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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started