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

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()...
needs to be done in C++ Q3. Just just have to code the function and the...
needs to be done in C++ Q3. Just just have to code the function and the function call statement. This program is for a Carpet Store. Create a function, named CarpetCost, that will have 3 parameters that are float values of length and width and price. The function will return the Total Cost of a piece of Carpet. The values for the Length and the Width is in Feet.   The price of the carpet is in Dollars per Square Yard....
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...
I WANT THIS CODE TO BE SIMPLE BECAUSE I AM NEW TO CODING AND I WANT...
I WANT THIS CODE TO BE SIMPLE BECAUSE I AM NEW TO CODING AND I WANT TO KNOW THE DETAILS! straight C Program. write a quiz game where a number of questions are asked and the user will win each time he answers right. and he loses each time he answers wrong. the type of questions asked can be about sports or anything else. You can put 5 quiz questions. It should only include the following: #include <stdio.h> and #include...
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 having trouble with a C++ code that I'm working on. It is a spell...
I am having trouble with a C++ code that I'm working on. It is a spell checker program. It needs to compare two arrays, a dictionary, and an array with misspelled strings that are compared to the strings in the dictionary. the strings that are in the second array that is not in the Dictionary are assumed to be misspelled. All of the strings in the dictionary are lowercase without any extra characters so the strings that are passed into...
I am using NetBeans IDE Java to code and I am seeking comment to the code...
I am using NetBeans IDE Java to code and I am seeking comment to the code as well (for better understanding). Magic squares. An n x n matrix that is filled with the numbers 1, 2, 3, …, n2 is a magic square if the sum of the elements in each row, in each column, and in the two diagonals is the same value. Write a program that reads in 16 values from the keyboard, displays them in a 4...
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)
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT