Question

In: Computer Science

I want a unique c++ code for the following. PLEASE HIGHLIGHT THESE FUNCTIONS WITH COMMENTS ....

I want a unique c++ code for the following. PLEASE HIGHLIGHT THESE FUNCTIONS WITH COMMENTS .

Add the following functions to the class arrayListType: Then, update the main function to test these new functions.

  • removeAll - which removes ALL of the instances of a value in the list
  • min - which returns the minimum value in the list
  • max - which returns the maximum value in the list

arrayListType.h :

#ifndef H_arrayListType
#define H_arrayListType

class arrayListType {
public:
bool isEmpty() const;

bool isFull() const;

int listSize() const;

int maxListSize() const;

void print() const;

bool isItemAtEqual(int location, int 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 list[location] is the same as item; otherwise,
// returns false. If location is out of range, 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 appropriate message is displayed.

void retrieveAt(int location, int& 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.

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


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

arrayListType (const arrayListType& otherList);
//Copy constructor

~arrayListType();
//Destructor
//Deallocate the memory occupied by the array.
  
void insertAt(int location, int insertItem);
void insertEnd(int insertItem);
void replaceAt(int location, int repItem);
int seqSearch(int searchItem) const;
void remove(int removeItem);

private:
int *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
};

#endif

arrayListTypeIm.cpp :

#include
#include "arrayListType.h"

using namespace std;

bool arrayListType::isEmpty() const
{
return (length == 0);
} //end isEmpty

bool arrayListType::isFull() const
{
return (length == maxSize);
} //end isFull

int arrayListType::listSize() const
{
   return length;
} //end listSize

int arrayListType::maxListSize() const
{
   return maxSize;
} //end maxListSize

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

bool arrayListType::isItemAtEqual(int location, int 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

void arrayListType::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

void arrayListType::retrieveAt(int location, int& retItem) const
{
if (location < 0 || location >= length)
cout << "The location of the item to be retrieved is "
<< "out of range" << endl;
else
retItem = list[location];
} //end retrieveAt

void arrayListType::clearList()
{
length = 0;
} //end clearList

arrayListType::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 int[maxSize];
} //end constructor

arrayListType::~arrayListType()
{
delete [] list;
} //end destructor

arrayListType::arrayListType(const arrayListType& otherList)
{
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

//===========
void arrayListType::insertAt(int location, int insertItem)
{
if (location < 0 || location >= maxSize)
cout << "The position of the item to be inserted "
<< "is out of range." << endl;
else if (length >= maxSize) //list is full
cout << "Cannot insert in a full list" << endl;
else
{
for (int i = length; i > location; i--)
list[i] = list[i - 1];   //move the elements down

list[location] = insertItem; //insert the item at
//the specified position

length++;   //increment the length
}
} //end insertAt

void arrayListType::insertEnd(int 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

int arrayListType::seqSearch(int searchItem) const
{
int loc;
bool found = false;

loc = 0;

while (loc < length && !found)
if (list[loc] == searchItem)
found = true;
else
loc++;

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


void arrayListType::remove(int 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 item to be deleted is not in the list."
<< endl;
}
} //end remove

void arrayListType::replaceAt(int location, int repItem)
{
if (location < 0 || location >= length)
cout << "The location of the item to be "
<< "replaced is out of range." << endl;
else
list[location] = repItem;
} //end replaceAt

main.cpp :

#include
#include "arrayListType.h"

using namespace std;   
  
int main()
{
arrayListType intList(25);   

int number;
cout << "List 8: Enter 8 integers: ";

for (int count = 0; count < 8; count++)
{
cin >> number;
intList.insertEnd(number);   
}

cout << endl;
cout << "Line 16: intList: ";
intList.print();   
cout << endl;

cout << "Line 18: Enter the number to be "
<< "deleted: ";   
cin >> number;   
cout << endl;

intList.remove(number);

cout << "Line 22: After removing " << number
<< " intList: ";
intList.print();   
cout << endl;

cout << "Line 25: Enter the search item: ";

cin >> number;
cout << endl;

if (intList.seqSearch(number) != -1)   
cout << "Line 29: " << number
<< " found in intList." << endl;
else   
cout << "Line 31: " << number
<< " is not in intList." << endl;   

return 0;
}

Solutions

Expert Solution

arrayListType.h :

#ifndef H_arrayListType
#define H_arrayListType

class arrayListType {
public:
bool isEmpty() const;

bool isFull() const;

int listSize() const;

int maxListSize() const;

void print() const;

bool isItemAtEqual(int location, int 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 list[location] is the same as item; otherwise,
// returns false. If location is out of range, an appropriate message is displayed.

void removeall(); // remove all instaces

void min();// returns mininum value

void max();//returns maximum value

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 appropriate message is displayed.

void retrieveAt(int location, int& 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.

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


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

arrayListType (const arrayListType& otherList);
//Copy constructor

~arrayListType();
//Destructor
//Deallocate the memory occupied by the array.
  
void insertAt(int location, int insertItem);
void insertEnd(int insertItem);
void replaceAt(int location, int repItem);
int seqSearch(int searchItem) const;
void remove(int removeItem);

private:
int *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
};

#endif

arrayListTypeIm.cpp :

#include
#include "arrayListType.h"

using namespace std;

bool arrayListType::isEmpty() const
{
return (length == 0);
} //end isEmpty

bool arrayListType::isFull() const
{
return (length == maxSize);
} //end isFull

int arrayListType::listSize() const
{
   return length;
} //end listSize

int arrayListType::maxListSize() const
{
   return maxSize;
} //end maxListSize

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

bool arrayListType::isItemAtEqual(int location, int 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

void arrayListType::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

void arrayListType::retrieveAt(int location, int& retItem) const
{
if (location < 0 || location >= length)
cout << "The location of the item to be retrieved is "
<< "out of range" << endl;
else
retItem = list[location];
} //end retrieveAt

void arrayListType::clearList()
{
length = 0;
} //end clearList

arrayListType::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 int[maxSize];
} //end constructor

arrayListType::~arrayListType()
{
delete [] list;
} //end destructor

arrayListType::arrayListType(const arrayListType& otherList)
{
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

//===========
void arrayListType::insertAt(int location, int insertItem)
{
if (location < 0 || location >= maxSize)
cout << "The position of the item to be inserted "
<< "is out of range." << endl;
else if (length >= maxSize) //list is full
cout << "Cannot insert in a full list" << endl;
else
{
for (int i = length; i > location; i--)
list[i] = list[i - 1];   //move the elements down

list[location] = insertItem; //insert the item at
//the specified position

length++;   //increment the length
}
} //end insertAt

void arrayListType::insertEnd(int 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

int arrayListType::seqSearch(int searchItem) const
{
int loc;
bool found = false;

loc = 0;

while (loc < length && !found)
if (list[loc] == searchItem)
found = true;
else
loc++;

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


void arrayListType::remove(int 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 item to be deleted is not in the list."
<< endl;
}
} //end remove

//applying clear() function to remove all from the list

void arrayListType::removeall()

{ List.clear(); for (auto i = List.begin(); i!= List.end(); ++i)

cout << ' ' << *i; cout<<"\nlist is cleared "; return 0;

}

// which returns the minimum value in the list

void arrayListType::min()

{

cout << "List: ";

    for (int i = 0; i < n; i++)

        cout << List[i] << " ";

int n = sizeof(List) / sizeof(List[0]);

  cout << "\nMin Element = "

         << *min_element(List, List + n);

}

/ /which returns the maximum value in the list

void arrayListType::max()

{

cout << "List: ";

    for (int i = 0; i < n; i++)

        cout << List[i] << " ";

int n = sizeof(List) / sizeof(List[0]);

  cout << "\nMax Element = "

         << *max_element(List, List + n);

}

void arrayListType::replaceAt(int location, int repItem)
{
if (location < 0 || location >= length)
cout << "The location of the item to be "
<< "replaced is out of range." << endl;
else
list[location] = repItem;
} //end replaceAt

main.cpp :

#include
#include "arrayListType.h"

using namespace std;   
  
int main()
{
arrayListType intList(25);   

int number;
cout << "List 8: Enter 8 integers: ";

for (int count = 0; count < 8; count++)
{
cin >> number;
intList.insertEnd(number);   
}

cout << endl;
cout << "Line 16: intList: ";
intList.print();   
cout << endl;

intList.removeall();

cout << endl;

intList.min();

cout << endl;

intList.max();

cout << endl;

cout << "Line 18: Enter the number to be "
<< "deleted: ";   
cin >> number;   
cout << endl;

intList.remove(number);

cout << "Line 22: After removing " << number
<< " intList: ";
intList.print();   
cout << endl;

intList

cout << "Line 25: Enter the search item: ";

cin >> number;
cout << endl;

if (intList.seqSearch(number) != -1)   
cout << "Line 29: " << number
<< " found in intList." << endl;
else   
cout << "Line 31: " << number
<< " is not in intList." << endl;   

return 0;
}


Related Solutions

I want a unique c code for the following parts mentioned below: Please try to solve...
I want a unique c code for the following parts mentioned below: Please try to solve the following. I am not able to solve it. I have already provideD the code for Part 1 and Part 2. You may only show their working output screenshots. You JUST need to *MODIFY PART 3* and upload the code for it Please provide details too by highlighting what you modified, PERFORM ALL THE PARTS SEPARATELY IN SHELL SERVER AND ATTACH CODE. DO NOT...
C++ program, include comments stating what each part of code does please. I want to be...
C++ program, include comments stating what each part of code does please. I want to be able to understand it so I'll be more knowledgeable in the future. The program is multiple files(fibonacci.h file, fibonacci.cpp file, main.cpp file and loops_simple_data_test.cpp). After the directions I also included any starter code or comments left by my professor within the files to aide us. Directions: In folder 04_loops_simple_data write prototype and definition for string value - return function get_fibonacci with an int parameter...
I want a unique c++ code for the following. (Greatest Common Divisor) Given two integers x...
I want a unique c++ code for the following. (Greatest Common Divisor) Given two integers x and y, the following recursive definition determines the greatest common divisor of x and y, written gcd(x,y):     5 5 ± x y x y y x y y gcd( , ) if 0 gcd( , % ) if 0 Note: In this definition, % is the mod operator. Write a recursive function, gcd, that takes as parameters two integers and...
C++ Problem. I am providing the code. Just Please provide the new function and highlight it....
C++ Problem. I am providing the code. Just Please provide the new function and highlight it. implement the functions replaceAt, seqSearch, and remove. Test your new function too in main. Also, Test Old functions in main. Show the output. Also, modify the functions accordingly which have "See Programming Exercise 22". main.cpp : #include <iostream> using namespace std; #include "arrayListTypetempl.h" int main(){    arrayListType<int> intList;    arrayListType<char> charList;       intList.insertEnd(5);    intList.insertEnd(3);    intList.insertEnd(4);    intList.insertEnd(55);       charList.insertEnd('a');   ...
Please write python code for the following. Implement the functions defined below. Include a triple-quoted comments...
Please write python code for the following. Implement the functions defined below. Include a triple-quoted comments string at the bottom displaying your output. Using sets (described in Deitel chapter 6) will simplify your work. Below is the starter template for the code: def overlap(user1, user2, interests): """ Return the number of interests that user1 and user2 have in common """ return 0    def most_similar(user, interests): """ Determine the name of user who is most similar to the input user...
Please complete the following code in C using the comments as instructions. Further instructions are below...
Please complete the following code in C using the comments as instructions. Further instructions are below the code. challenge.c // goal: print the environment variables to the file "env.txt", one per line // (If envp is NULL, the file should be empty, opening in write mode will do that.) // example: // inputs: // envp/environ = {"E1=2","E2=7",NULL} // outputs: // env.txt as a string would be "E1=2\nE2=7\n" // example: // inputs: // envp/environ = {NULL} or NULL // outputs: //...
C++ Please Fill in for the functions for the code below. The functions will be implemented...
C++ Please Fill in for the functions for the code below. The functions will be implemented using vectors ONLY. Additional public helper functions or private members/functions can be used. The List class will be instantiated via a pointer and called similar to the code below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public: // Default Constructor Stack() {// ... } // Push integer n onto top of...
C++ Please Fill in for the functions for the code below. The functions will implement an...
C++ Please Fill in for the functions for the code below. The functions will implement an integer list using dynamic array ONLY (an array that can grow and shrink as needed, uses a pointer an size of array). Additional public helper functions or private members/functions can be used. The List class will be instantiated via a pointer and called similar to the code below: class List { public: // Default Constructor List() {// ... } // Push integer n onto...
C++ Please Fill in for the functions for the code below. The functions will implement an...
C++ Please Fill in for the functions for the code below. The functions will implement an integer stack using deques ONLY. It is possible to use only one deque but using two deques also works. Additional public helper functions or private members/functions can be used. The Stack class will be instantiated via a pointer and called as shown below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public:...
C++ please Fill in for the functions for the code below. The functions will implement an...
C++ please Fill in for the functions for the code below. The functions will implement an integer stack using deques ONLY. It is possible to use only one deque but using two deques also works. Additional public helper functions or private members/functions can be used. The Stack class will be instantiated via a pointer and called as shown below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT