Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This question comes from theC++ Programming book 7th edition chapter 13 problem

ID: 3681697 • Letter: T

Question

This question comes from theC++ Programming book 7th edition chapter 13 problem 19, I have to write the definitions of the member functions of the classes arrayListType and unorderedArrayListType that are not given in this chapter. Also I have to write a test program.

These are the directions my instructer gave

You need to define the following functions for these classes in order for them to work:
In arrayListType class

isEmpty()

isFull()

listSize()

maxListSize()

clearList()


In unorderedArrayType

insertAt()

replaceAt()

Please note that the classes are created in these files and that class unorderedArrayType is derived from the abstract class arrayListType.

You will upload the modified files for arrayListType.h, unorderedArrayType.h, and testProgStringList.cpp for this assignment. Do not change the file names. Please put your name in the header comment for each file. Indicate you are modifying the existing code to define the above listed functions.

These are the existing codes he provided for us to modify

Please provide comments in order for me to understand what you have done

Explanation / Answer

Here is the code for you:

arrayListType.h:

#include <iostream>
using namespace std;

#ifndef H_arrayListType
#define H_arrayListType

template <class elemType>
class arrayListType
{
public:
const arrayListType<elemType>&
operator=(const arrayListType<elemType>&);
//Overloads the assignment operator

bool isEmpty() const;
//Function to determine whether the list is empty
//Postcondition: Returns true if the list is empty;
// otherwise, returns false.

bool isFull() const;
//Function to determine whether the list is full
//Postcondition: Returns true if the list is full;
// otherwise, returns false.

int listSize() const;
//Function to determine the number of elements in
//the list.
//Postcondition: Returns the value of length.

int maxListSize() const;
//Function to determine the maximum size of the list
//Postcondition: Returns the value of maxSize.

void print() const;
//Function to output the elements of the list
//Postcondition: Elements of the list are output on the
// standard output device.

bool isItemAtEqual(int location, const elemType& item) const;
//Function to determine whether item is the same as
//the item in the list at the position specified
//by location.
//Postcondition: Returns true if the list[location]
// is the same as item; otherwise,
// returns false.
// If location is out of range, an
// appropriate message is displayed.

virtual void insertAt(int location, const elemType& insertItem) = 0;
//Function to insert insertItem in the list at the
//position specified by location.
//Note that this is an abstract function.
//Postcondition: Starting at location, the elements of
// the list are shifted down,
// list[location] = insertItem; length++;
// If the list is full or location is out of
// range, an appropriate message is displayed.

virtual void insertEnd(const elemType& insertItem) = 0;
//Function to insert insertItem an item at the end of
//the list. Note that this is an abstract function.
//Postcondition: list[length] = insertItem; and length++;
// If the list is full, an appropriate
// message is displayed.

void removeAt(int location);
//Function to remove the item from the list at the
//position specified by location
//Postcondition: The list element at list[location] is
// removed and length is decremented by 1.
// If location is out of range, an
// appropriate message is displayed.

void retrieveAt(int location, elemType& retItem) const;
//Function to retrieve the element from the list at the
//position specified by location
//Postcondition: retItem = list[location]
// If location is out of range, an
// appropriate message is displayed.

virtual void replaceAt(int location, const elemType& repItem) = 0;
//Function to replace repItem the elements in the list
//at the position specified by location.
//Note that this is an abstract function.
//Postcondition: list[location] = repItem
// If location is out of range, an
// appropriate message is displayed.

void clearList();
//Function to remove all the elements from the list
//After this operation, the size of the list is zero.
//Postcondition: length = 0;

virtual int seqSearch(const elemType& searchItem) const = 0;
//Function to search the list for searchItem.
//Note that this is an abstract function.
//Postcondition: If the item is found, returns the
// location in the array where the item is
// found; otherwise, returns -1.

virtual void remove(const elemType& removeItem) = 0;
//Function to remove removeItem from the list.
//Note that this is an abstract function.
//Postcondition: If removeItem is found in the list,
// it is removed from the list and length
// is decremented by one.

arrayListType(int size = 100);
//Constructor
//Creates an array of the size specified by the
//parameter size. The default array size is 100.
//Postcondition: The list points to the array, length = 0,
// and maxSize = size;

arrayListType (const arrayListType<elemType>& otherList);
//Copy constructor

virtual ~arrayListType();
//Destructor
//Deallocate the memory occupied by the array.

protected:
elemType *list; //array to hold the list elements
int length; //variable to store the length of the list
int maxSize; //variable to store the maximum
//size of the list
};


template <class elemType>
bool arrayListType<elemType>::isEmpty() const
{
if(length == 0)
return true;
return false;
} // //end isEmpty

template <class elemType>
bool arrayListType<elemType>::isFull() const
{
if(length == maxSize)
return true;
return false;
} //end isFull

template <class elemType>
int arrayListType<elemType>::listSize() const
{
return length;
} //end listSize

template <class elemType>
int arrayListType<elemType>::maxListSize() const
{
return maxSize;
} //end maxListSize

template <class elemType>
void arrayListType<elemType>::print() const
{
for (int i = 0; i < length; i++)
cout << list[i] << " ";
cout << endl;
} //end print

template <class elemType>
bool arrayListType<elemType>::isItemAtEqual(int location,
const elemType& item) const
{
if (location < 0 || location >= length)
{
cout << "The location of the item to be removed "
<< "is out of range." << endl;

return false;
}
else
return (list[location] == item);
} //end isItemAtEqual

template <class elemType>
void arrayListType<elemType>::removeAt(int location)
{
if (location < 0 || location >= length)
cout << "The location of the item to be removed "
<< "is out of range." << endl;
else
{
for (int i = location; i < length - 1; i++)
list[i] = list[i + 1];

length--;
}
} //end removeAt

template <class elemType>
void arrayListType<elemType>::retrieveAt(int location,
elemType& retItem) const
{
cout << "The element at location " << location << " is " << list[location] << endl;
} //end retrieveAt

template <class elemType>
void arrayListType<elemType>::clearList()
{
list = NULL;
length = 0;
} //end clearList

template <class elemType>
arrayListType<elemType>::arrayListType(int size)
{
if (size <= 0)
{
cout << "The array size must be positive. Creating "
<< "an array of the size 100. " << endl;

maxSize = 100;
}
else
maxSize = size;

length = 0;

list = new elemType[maxSize];
} //end constructor

template <class elemType>
arrayListType<elemType>::~arrayListType()
{
delete [] list;
} //end destructor

template <class elemType>
arrayListType<elemType>::arrayListType
(const arrayListType<elemType>& otherList)
{
// instructor provided code - you are very welcome
maxSize = otherList.maxSize;
length = otherList.length;

list = new int[maxSize]; //create the array

for (int j = 0; j < length; j++) //copy otherList
list [j] = otherList.list[j];
}//end copy constructor


template <class elemType>
const arrayListType<elemType>& arrayListType<elemType>::operator=
(const arrayListType<elemType>& otherList)
{
if (this != &otherList) //avoid self-assignment
{
delete [] list;
maxSize = otherList.maxSize;
length = otherList.length;

list = new elemType[maxSize];

for (int i = 0; i < length; i++)
list[i] = otherList.list[i];
}
return *this;
} //end overloading operatro=


#endif

And the code for unorderedArrayType.h:

#ifndef H_unorderedArrayListType
#define H_unorderedArrayListType

#include "arrayListType.h"
  
template <class elemType>
class unorderedArrayListType: public arrayListType<elemType>
{
public:
void insertAt(int location, const elemType& insertItem);
void insertEnd(const elemType& insertItem);
void replaceAt(int location, const elemType& repItem);
int seqSearch(const elemType& searchItem) const;
void remove(const elemType& removeItem);

unorderedArrayListType(int size = 100);
//Constructor

};

template <class elemType>
void unorderedArrayListType<elemType>::insertAt(int location,
const elemType& insertItem)
{
if(length >= maxSize)
cout<<"Cannot insert in a full list."<<endl;
else
{
for(int i = length; i >= location; i++)
list[i+1] = list[i];
list[location] = insertItem;
}
} //end insertAt

template <class elemType>
void unorderedArrayListType<elemType>::insertEnd
(const elemType& insertItem)
{
if (length >= maxSize) //the list is full
cout << "Cannot insert in a full list." << endl;
else
{
list[length] = insertItem; //insert the item at the end
length++; //increment the length
}
} //end insertEnd

template <class elemType>
int unorderedArrayListType<elemType>::seqSearch
(const elemType& searchItem) const
{
int loc;
bool found = false;

for (loc = 0; loc < length; loc++)
if (list[loc] == searchItem)
{
found = true;
break;
}

if (found)
return loc;
else
return -1;
} //end seqSearch

template <class elemType>
void unorderedArrayListType<elemType>::remove
(const elemType& removeItem)
{
int loc;

if (length == 0)
cout << "Cannot delete from an empty list." << endl;
else
{
loc = seqSearch(removeItem);

if (loc != -1)
removeAt(loc);
else
cout << "The tem to be deleted is not in the list."
<< endl;
}
} //end remove

template <class elemType>
void unorderedArrayListType<elemType>::replaceAt(int location,
const elemType& repItem)
{
if(length <= location)
cout<<"Invalid position to replace. List is much smaller."<<endl;
else
list[location] = repItem;
} //end replaceAt

template <class elemType>
unorderedArrayListType<elemType>::
unorderedArrayListType(int size)
: arrayListType<elemType>(size)
{
} //end constructor


#endif

And ofcourse the test code testProgStringList.cpp remains the same, and there will be no modification. Here is that duplicate code for you;

#include <iostream>
#include <string>
#include "unorderedArrayListType.h"

using namespace std;

int main()
{
unorderedArrayListType<string> stringList(25); //Line 1

string str; //Line 2

cout << "List 3: Enter 5 strings: "; //Line 3

for (int count = 0; count < 5; count++) //Line 4
{
cin >> str; //Line 5
stringList.insertEnd(str); //Line 6
}

cout << endl; //Line 7

cout << "Line 8: stringList: "; //Line 8
stringList.print(); //Line 9
cout << endl; //Line 10

cout << "Line 11: Enter the string to be "
<< "deleted: "; //Line 11
cin >> str; //Line 12
cout << endl; //Line 13

stringList.remove(str); //Line 14
cout << "Line 15: After removing " << str
<< " stringList: "; //Line 15
stringList.print(); //Line 16
cout << endl; //Line 17

cout << "Line 18: Enter the search item: "; //Line 18

cin >> str; //Line 19
cout << endl; //Line 20

if (stringList.seqSearch(str) != -1) //Line 21
cout << "Line 22: " << str
<< " found in stringList." << endl; //Line 22
else //Line 23
cout << "Line 24: " << str
<< " is not in stringList." << endl; //Line 24

return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote