Question

In: Computer Science

Your primary task for this exercise is to complete header file by writing three functions with...

Your primary task for this exercise is to complete header file by writing three functions with its description below:

removeAt function – to remove the item from the list at the position specified by location. Because the list elements are in no particular order (unsorted list), you could simple remove the element by swapping the last element of the list with the item to be removed and reducing the length of the list.

insertAt function - to insert an item in the list at the position specified by location. The item to be inserted is passed as a parameter to the function.

print function – to output the elements of the list.

3. Demonstrate the program by asking a user to enter 5 integers. After displaying 5 integers, ask the user the position of the item to be deleted. You can use your own data to generate two outputs.

Sample output 1:

Enter 5 integers: 45 19 2 16 77

The list you entered is: 45 19 2 16 77

Enter the position of item to be deleted: 2
After removing element at 2, the list is:
45 19 77 16

Sample output 2:

Enter 5 integers: 15 12 11 3 49

The list you entered is: 15 12 11 3 49

Enter the position of item to be deleted: 6
The location of the item to be removed is out of range
After removing element at 6, the list is:
15 12 11 3 49

======================================== C++

#ifndef H_arrayListType
#define H_arrayListType

#include <iostream>
#include <cassert>

using namespace std;

template <class elemType>
class arrayListType
{
public:
    const arrayListType<elemType>& operator=
                         (const arrayListType<elemType>&);
      //Overloads the assignment operator
    
    void print() const;
      //Function to output the elements of the list
      //Postcondition: Elements of the list are output on the 
       //   standard output device.
    
   void insertAt(int location, const elemType& insertItem);
      //Function to insert an item in the list at the 
      //position specified by location. The item to be inserted 
      //is passed as a parameter to the function.
      //Postcondition: Starting at location, the elements of the
      //    list are shifted down, list[location] = insertItem;,
      //    and length++;. If the list is full or 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,an appropriate message is displayed.
 
    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

    ~arrayListType();
      //destructor

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

// print function definition
template <class elemType>
void arrayListType<elemType>::print() const
{
        // your code here
}

// insertAt function definition
template <class elemType>
void arrayListType<elemType>::insertAt
                  (int location, const elemType& insertItem)
{
        // your code here
} //end insertAt

// removeAt function definition
template <class elemType>
void arrayListType<elemType>::removeAt(int location)
{
 // your code here

} //end removeAt

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

        maxSize = 100;
    }
    else
        maxSize = size;

    length = 0;

    list = new elemType[maxSize];
    assert(list != NULL);
}


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


template <class elemType>
arrayListType<elemType>::arrayListType
                   (const arrayListType<elemType>& otherList)
{
    maxSize = otherList.maxSize;
    length = otherList.length;
    list = new elemType[maxSize]; //create the array
    assert(list != NULL);         //terminate if unable to allocate
                                  //memory space

    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];  //create the array
        assert(list != NULL);   //if unable to allocate memory 
                                //space, terminate the program 
        for (int i = 0; i < length; i++)
            list[i] = otherList.list[i]; 
    }

    return *this; 
}

#endif

==================================================================

#include <iostream>
#include "arrayListType.h"

using namespace std;

int main()
{
    arrayListType<int> intList(100);                                              
   
    int counter;                                                                                        
    int number; 
    int position;                                                                                       
                                                                                        
    cout << "Enter 5 integers: ";                                         
                                                                                
    for (counter = 0; counter < 5; counter++)                                
    {   
                cin >> number;                                                                            
                intList.insertAt(counter, number);                                      
    }

    cout << endl;                                                                                         
    cout << "The list you entered is: ";                          
    intList.print();                                                                            
    cout << endl;                                                                                         

    cout << "Enter the position of item to be deleted: ";         
    cin >> position;                                                                                      
    intList.removeAt(position);                                                                 
    cout << "After removing element at " << position
                 << ", the list is:" << endl;                                                       
    intList.print();
        
        system("pause");
    return 0;
}

/*
Enter 5 integers: 45 19 2 16 77

The list you entered is: 45 19 2 16 77

Enter the position of item to be deleted: 2
After removing element at 2, the list is:
45 19 77 16

*/

/*
Enter 5 integers: 15 12 11 3 49

The list you entered is: 15 12 11 3 49

Enter the position of item to be deleted: 6
The location of the item to be removed is out of range
After removing element at 6, the list is:
15 12 11 3 49

*/

Solutions

Expert Solution

#ifndef H_arrayListType

#define H_arrayListType

#include <iostream>

#include <cassert>

using namespace std;

template <class elemType>

class arrayListType

{

public:

const arrayListType<elemType>& operator=

(const arrayListType<elemType>&);

//Overloads the assignment operator

void print() const;

//Function to output the elements of the list

//Postcondition: Elements of the list are output on the

// standard output device.

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

//Function to insert an item in the list at the

//position specified by location. The item to be inserted

//is passed as a parameter to the function.

//Postcondition: Starting at location, the elements of the

// list are shifted down, list[location] = insertItem;,

// and length++;. If the list is full or 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,an appropriate message is displayed.

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

~arrayListType();

//destructor

protected:

elemType *list; //array to hold the list elements

int length; //to store the length of the list

int maxSize; //to store the maximum size of the list

};

// print function definition

template <class elemType>

void arrayListType<elemType>::print() const

{

// your code here

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

cout << list[i] <<" ";

}

// insertAt function definition

template <class elemType>

void arrayListType<elemType>::insertAt

(int location, const elemType& insertItem)

{

// your code here

list[length++] = insertItem;

} //end insertAt

// removeAt function definition

template <class elemType>

void arrayListType<elemType>::removeAt(int location)

{

// your code here

if (location >= length || length == 0)

cout <<"The location of the item to be removed is out of range"<<endl;

else {

    list[location] = list[length-1];

    length--;

}

} //end removeAt

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

template <class elemType>

arrayListType<elemType>::arrayListType(int size)

{

if (size < 0)

{

cerr << "The array size must be positive. Creating "

<< "an array of size 100. " << endl;

maxSize = 100;

}

else

maxSize = size;

length = 0;

list = new elemType[maxSize];

assert(list != NULL);

}


template <class elemType>

arrayListType<elemType>::~arrayListType()

{

delete [] list;

}


template <class elemType>

arrayListType<elemType>::arrayListType

(const arrayListType<elemType>& otherList)

{

maxSize = otherList.maxSize;

length = otherList.length;

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

assert(list != NULL); //terminate if unable to allocate

//memory space

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]; //create the array

assert(list != NULL); //if unable to allocate memory

//space, terminate the program

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

list[i] = otherList.list[i];

}

return *this;

}

#endif

===========Main code is still the same================================================

=============================================================
SEE OUTPUT


Thanks, PLEASE COMMENT if there is any concern.


Related Solutions

Complete the following task in C++. Separate your class into header and cpp files. You can...
Complete the following task in C++. Separate your class into header and cpp files. You can only useiostream, string and sstream. Create a main.cpp file to test your code thoroughly. Given : commodity.h and commodity.cpp here -> https://www.chegg.com/homework-help/questions-and-answers/31-commodity-request-presented-two-diagrams-depicting-commodity-request-classes-form-basic-q39578118?trackid=uF_YZqoK Create : locomotive.h, locomotive.cpp, main.cpp Locomotive Class Hierarchy Presented here will be a class diagram depicting the nature of the class hierarchy formed between a parent locomotive class and its children, the two kinds of specic trains operated. The relationship is a...
Complete the following task in C++. Separate your class into header and cpp files. You can...
Complete the following task in C++. Separate your class into header and cpp files. You can only use iostream, string and sstream. Create a main.cpp file to test your code thoroughly. Given : commodity.h and commodity.cpp here -> https://www.chegg.com/homework-help/questions-and-answers/31-commodity-request-presented-two-diagrams-depicting-commodity-request-classes-form-basic-q39578118?trackid=uF_YZqoK Given : locomotive.h, locomotive.cpp, main.cpp -> https://www.chegg.com/homework-help/questions-and-answers/complete-following-task-c--separate-class-header-cpp-files-useiostream-string-sstream-crea-q39733428 Create : DieselElectric.cpp DieselElectric.h DieselElectric dieselElectric -fuelSupply:int --------------------------- +getSupply():int +setSupply(s:int):void +dieselElectric(f:int) +~dieselElectric() +generateID():string +calculateRange():double The class variables are as follows: fuelSuppply: The fuel supply of the train in terms of a number of kilolitres...
please complete the header file that contains a class template for ADT Queue and complete all...
please complete the header file that contains a class template for ADT Queue and complete all the member functions in the class template. Submit the header file only, but please write a source file that tests all the member functions to make sure they are working correctly. queue.h #ifndef _QUEUE #define _QUEUE #include"Node.h" template<class ItemType> class Queue { private:    Node<ItemType> *backPtr;    Node<ItemType> *frontPtr; public:    Queue(); //Default constructor    Queue(const Queue<ItemType> &aQueue);    bool isEmpty() const;    bool...
In this homework assignment, you will be writing three `void` functions. These functions each take a...
In this homework assignment, you will be writing three `void` functions. These functions each take a single integer parameter. The functions accomplish the following tasks. * The first function prints out the sum of the numbers from 1 up to and including the number passed to it as an argument. * The second function prints out the sum of the even numbers from 2 up to and including the number passed to it as an argument. * The third function...
DIRECTIONS: Complete the following Programming Exercise in Python. Name the file Lastname_Firstname_E1. You just started your...
DIRECTIONS: Complete the following Programming Exercise in Python. Name the file Lastname_Firstname_E1. You just started your summer internship with ImmunityPlus based in La Crosse, Wisconsin. You are working with the forecasting team to estimate how many doses of an immunization drug will be needed. For each drug estimation, you will be provided the following information: corona.txt 39 20 31 10 42 49 54 21 70 40 47 60 - The size of the target population. - The life expectancy, in...
What are the three primary distinct entities in this system and what are the primary functions of each?
Part 1: The role and functioning of central banksC) The United States central bank is called the Federal reserve system. What are the three primary distinct entities in this system and what are the primary functions of each? How do their responsibilities and actions overlap?D) What is a technocrat?
Your task is to count the frequency of words in a text file, and return the...
Your task is to count the frequency of words in a text file, and return the most frequent word with its count. For example, given the following text: there are two ways of constructing a software design one way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies. Based on the example your program should printout the following along with the...
JAVA * This is a skeleton file. Complete the functions below. You * may also edit...
JAVA * This is a skeleton file. Complete the functions below. You * may also edit the function "main" to test your code. * * You should not use any loops or recursions. Your code needs to run in * constant time. It is OK if your testing code has loops (like in checkInvariants). * * You must not add fields or static variables. As always, you must not change * the declaration of any method nor the name of...
Modify the attached TicTacToeStart.cpp Your job is to complete three functions init, printBoard, and gameOver #include...
Modify the attached TicTacToeStart.cpp Your job is to complete three functions init, printBoard, and gameOver #include <iostream> using namespace std; char gameBoard[3][3]; void init(){     /*     using a double loop to initialize all gameBoard element as 'E'     */ } void printBoard() {     /*using a double loop to print out gameboard     */ } bool gameOver() {     /*if any row or column, or diagonal are all X or O, return true*/     return false; } int main()...
Writing Task 1. in what ways are three temperature scales the same ? 2. In what...
Writing Task 1. in what ways are three temperature scales the same ? 2. In what ways are the three temperature scales different ? 3. At approximately what temperature would Celsius and Fahrenheit thermometer register the same reading ? 4. what is absolute zero temperature ? explain
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT