Question

In: Computer Science

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');
   charList.insertEnd('B');
   charList.insertEnd('f');
  
   intList.print();
   charList.print();
  
   return 0;
}

arrayListTypetempl.h :

#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;

bool isFull() const;

int listSize() const;

int maxListSize() const;

void print() const;

bool isItemAtEqual(int location, const elemType& item) const;

void insertAt(int location, const elemType& insertItem);

void insertEnd(const elemType& insertItem);

void removeAt(int location);

void retrieveAt(int location, elemType& retItem) const;

void clearList();

void replaceAt(int location, const elemType& repItem);
  
int seqSearch(const elemType& searchItem);

void remove(const elemType& removeItem);


arrayListType(int size = 100);

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

~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;
   }
   else {
return false;
   }

} // //end isEmpty

template <class elemType>
bool arrayListType<elemType>::isFull() const
{
cout << "See Programming Exercise 22." << endl;
return false;
} //end isFull

template <class elemType>
int arrayListType<elemType>::listSize() const
{
cout << "See Programming Exercise 22." << endl;
return -1;
} //end listSize

template <class elemType>
int arrayListType<elemType>::maxListSize() const
{
cout << "See Programming Exercise 22." << endl;
return -1;
} //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
{
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

template <class elemType>
void arrayListType<elemType>::clearList()
{
cout << "See Programming Exercise 22." << endl;
} //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)
{
cout << "See Programming Exercise 22." << endl;
}//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 operatror=

template <class elemType>
void arrayListType<elemType>::insertAt(int location, const elemType& 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

template <class elemType>
void arrayListType<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


#endif

Solutions

Expert Solution

The required code has been provided below followed by a sample output. Also, the remove function is implemented such that if the element is present in the list more than 1 time then it removes the element from 1st occurrence index and if not found then does nothing.

CODE:

#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');
   charList.insertEnd('B');
   charList.insertEnd('f');

   intList.print();
   charList.print();

   // checking all new functons implemented
   intList.replaceAt(1,23);
   charList.replaceAt(2,'c');

   cout<<"After intList.replaceAt(1,23) and charList.replaceAt(2,'c') the lists are:"<<endl;

   intList.print();
   charList.print();

   int res = intList.seqSearch(55);
   if (res>=0)
        cout<<"55 found at index "<<res<<endl;
   else
        cout<<"55 not found "<<endl;

    res = charList.seqSearch('d');
    if (res>=0)
        cout<<"char d found at index "<<res<<endl;
   else
        cout<<"char d not found "<<endl;

   intList.remove(4);
   charList.remove('a');

   cout<<"After intList.remove(4) and charList.remove('a') the lists are:"<<endl;

   intList.print();
   charList.print();



   return 0;
}
//arrayListTypetempl.h :
// IMPLEMENTED NEW FUNCTIONS

#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;

        bool isFull() const;

        int listSize() const;

        int maxListSize() const;

        void print() const;

        bool isItemAtEqual(int location, const elemType& item) const;

        void insertAt(int location, const elemType& insertItem);

        void insertEnd(const elemType& insertItem);

        void removeAt(int location);

        void retrieveAt(int location, elemType& retItem) const;
        void clearList();

        void replaceAt(int location, const elemType& repItem);

        int seqSearch(const elemType& searchItem);
        void remove(const elemType& removeItem);


        arrayListType(int size = 100);
        arrayListType (const arrayListType<elemType>& otherList);
        //Copy constructor

        ~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;
   }
   else {
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
{
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

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

template <class elemType>
void arrayListType<elemType>::replaceAt(int location, const elemType& repItem){

    if (location < 0 || location >= length)
        cout << "The location of the item to be retrieved is "<< "out of range" << endl;
    else
        list[location] = repItem;


}

template <class elemType>
int arrayListType<elemType>::seqSearch(const elemType& searchItem){

    for(int location=0;location<length;location++)
    {
        if (list[location]==searchItem)
            return location;
    }
    return -1;


}

template <class elemType>
void arrayListType<elemType>::remove(const elemType& removeItem){

    int location = -1;
    for(int i=0;i<length;i++)
    {
        if (list[i]==removeItem){
            location = i;
            break;
            }
    }
    if (location>=0){
        for (int i = location; i < length - 1; i++)
            list[i] = list[i + 1];

        length--;
    }

}


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)
{
    length = otherList.length;
    maxSize = otherList.maxSize;
    for(int i=0;i<length;i++)
        list[i] = otherList[i];

}//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 operatror=

template <class elemType>
void arrayListType<elemType>::insertAt(int location, const elemType& 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

template <class elemType>
void arrayListType<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


#endif

OUTPUT:

NOTE: In case of any query, you can mention it in comment section. HAPPY LEARNING!!


Related Solutions

Hello, I am working on a C-program that deals with memory mapping, Please show all code...
Hello, I am working on a C-program that deals with memory mapping, Please show all code (code must be in the C) and outputs. The instructions are as follows INSTRUCTIONS: You have two sample c codes, one for the client and one for the server. IF the client makes a change in the shared memory, those changes will show on the server side. Your task is to adapt the change strategy to develop two applications so they can send messages...
So I have written a code for it but i just have a problem with the...
So I have written a code for it but i just have a problem with the output. For the month with the highest temperature and lowest temperature, my code starts at 0 instead of 1. For example if I input that month 1 had a high of 20 and low of -10, and every other month had much warmer weather than that, it should say "The month with the lowest temperature is 1" but instead it says "The month with...
I am trying to write a code in C for a circuit board reads in a...
I am trying to write a code in C for a circuit board reads in a temperature from a sensor on the circuit board and reads it onto a 7-segment LED display. How exactly would you code a floating 2 or 3 digit reading onto the LED display? I am stuck on this part.
I am new to socket programming and I wish to know what these lines of code...
I am new to socket programming and I wish to know what these lines of code do. I know they are creating UDP sockets, but it would help if someone explained what the code means. Thank you. Python Code: import socket testingSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) testingSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, testingSocket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) testingSocket.bind(('0.0.0.0', 50000)) send_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) send_socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) receiving_thread = Thread(target=self.receivingFunction) send_thread = Thread(target=self.sendMessage) broadcast_online_status_thread = Thread(target=onlineStatus)
Can someone please provide C# code in Events,Delegates and Reflection for the below problem. ----------------- Agent-Policy...
Can someone please provide C# code in Events,Delegates and Reflection for the below problem. ----------------- Agent-Policy XYZ Assurance wants to categorize the policies based on the agent who sold the policy and the Insurance type. Given an Agent name, display the policies that were sold by that agent. Similarly, print the policies in the given Insurance type. Write a program to do the same. They have a list of few policies and want to update the list with a few...
I am trying to solve a c++ problem over c strings where I have to input...
I am trying to solve a c++ problem over c strings where I have to input an email address and check if it is valid. After completing my code I keep getting errors that my program will not run, specifically the lines with my for loops. Can you please look at my code and tell me what is wrong and how I can fix the code? I included below the assignment instructions and my current code. Assignment Instructions Write a...
Please look at the following code. When I run it from the command line, I am...
Please look at the following code. When I run it from the command line, I am supposed to get the following results: 1: I am 1: I am I need to fix it so that the functions 'print_i' and 'print_j' print all of their lines. What do I need to add? Thank you. C source code: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <pthread.h> #include <unistd.h> // These two functions will run concurrently void* print_i(void *ptr) { printf("1: I am...
Please give an explanation or what the rational of the code is about, I am trying...
Please give an explanation or what the rational of the code is about, I am trying to figure it out what it means so to better understand it. Discuss the Code Provision on Square and Rectangular HSS and Box- Shaped Members, Rounds HSS, Tees and Double Angles Loaded in the Plane of Symmetry, and Single Angles.
I am using a photoshop character that I drew. Please provide AS SIMPLE OF A PROCEDURE...
I am using a photoshop character that I drew. Please provide AS SIMPLE OF A PROCEDURE AS POSSIBLE!! 1. How do you create a sprite in Unity? 2. How do you create a sprite sliced in Unity? Thank you :)
(C++) I need to Create a Copy function of a Binary Search Tree recursively providing these...
(C++) I need to Create a Copy function of a Binary Search Tree recursively providing these structure emplate <typename T> class Tree {    struct TreeNode    {        T mData;        TreeNode* mLeft = nullptr;        TreeNode* mRight = nullptr;        TreeNode* mParent = nullptr;        bool mIsDead = false;        TreeNode()        {        }        TreeNode(T tData) : TreeNode()        {            mData = tData;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT