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

c++ Using inheritance, extend this template of IndexList.h (bottem of the questi

ID: 3868647 • Letter: C

Question

c++

Using inheritance, extend this template of IndexList.h (bottem of the question)

the following functions:

Concatenate two indexed lists.

Sort an indexed list using the selection sort algorithm (declared in indexList but not implemented).

Find the position of a target using the binary search algorithm.

files should be named: IndexList.h, IndexList.cpp, ExtIndexList.h, ExtIndexList.cpp and TestExtIndexList.cpp. In the class indexList, include only the required member functions.

#ifndef INDEXLIST_H
#define INDEXLIST_H
#include
using namespace std;

template
class indexList
{
public:
// Constructor
indexList();

// Add an item to the end of an indexed list
bool append(const T&);

// Replace an element at a specified index
bool replace(int, const T&);

// Insert an element at a specified index
bool insert(int, const T&);

// Retrieve an element at a specified index
bool retrieve(int, T&) const;

//delete an element at an specified index
bool remove(int);

// Find index of smallest value in a sublist
int findMin(int, int) const;

// Find index of largest value in a sublist
int findMax(int, int) const;

// Find index of a target item
// Returns -1 if target item not found
int search(const T&) const;

// Sort an indexed list
void selSort();

// Read data into the list
void read(istream &);

// Display the list contents
void display() const;

// Get the current size
int getSize() const;

private:

//Data Members
T elements[maxSize];
int size;

};

#endif // INDEXLIST_H

Explanation / Answer

TestExtIndexList.cpp
----------------------------------------------
#include <iostream>
#include <string>
#include "ExtIndexList.h"
#include "ExtIndexList.cpp"
#include "IndexList.h"
#include "IndexList.cpp"
using namespace std;

int main() {

    indexList<int, maxsize>list1;
    indexList<int, maxsize>list2;
    ExtList<int, maxsize> list3;
    int n;
    int number;
    cout << "List 1 of type ""int""" << endl;
    cout << "Enter number of list items: ";
    cin >> n;
    for (int i = 0; i < n; i++) {
        cout << "Enter next item: ";
        cin >> number;
        list1.append(number);
    }
    cout << "List 2 of type ""int""" << endl;
    cout << "Enter number of list items: ";
    cin >> n;
    for (int i = 0; i < n; i++) {
        cout << "Enter next item: ";
        cin >> number;
        list2.append(number);
    }
    list3.append(list1, list2);
    cout << "List 1 concatenated with list2 in list3: ";
    list3.display();
    list3.selSort();
    cout << " List 3 sorted in acending order: ";
    list3.display();

    cout << " Enter the number to search in list 3: ";
    cin >> number;
    int index;
    index = list3.Binary(number);
    if (index == -1)
        cout << "Target not found! ";
    else
        cout << "number " << number << " is found in postion: " << index << endl;
}
--------------------------------------------------------------------------
ExtIndexList.cpp
--------------------------------
#include <iostream>
#include <string>
#include "ExtIndexList.h"
#include "IndexList.h"
using namespace std;

template<class T, int maxsize>       //constructor for the third list
ExtList<T, maxsize>::ExtList() {
    size = 0;
};

template<class T, int maxsize>
bool ExtList<T, maxsize>::append(const indexList& list, const indexList& list2) {   // convine the 2 lists
    bool result;
    if(size < maxsize) {               //adds the first list
        for (int i = 0; i < list.size ; i++) {
            elements[size] = list.elements[i];
            size++;
        }
        result = true;
    }
    if (size < maxsize) {               //adds the second list
        for(int i = 0; i < list2.size; i++) {
            elements[size] = list2.elements[i];
            size++;
        }
        result = true;
    }
    else {                                  //if the array is full display
        cerr << "CANNOT APPEND THE ITEM, THE ARRAY IS FULL!";
        result = false;
    }
    return result;
}

template <class T, int maxsize>
void ExtList<T, maxsize>::selSort() {
    int search_index, search_max, temp;
    temp = maxsize - 1; //store the result in the last spot of the array

    for (int current_index = 0; current_index < size; current_index++) {
        search_max = current_index;
        //find index of largest element in unsorted section of elements
        for (search_index = current_index + 1; search_index < size; search_index++)
            if (elements[search_max] > elements[search_index])   //The > operator is overloaded from the employee.cpp
                search_max = search_index;
        //exchange items at position search_max and current index
        if (search_max > current_index) {

            elements[temp] = elements[search_max];
            elements[search_max] = elements[current_index];
            elements[current_index] = elements[temp];

        }
    }
}

template<class T, int maxsize>
int ExtList<T, maxsize>::Binary(int target) {
    int index;
    int Low = 0;
    int Mid, Difference;

    while (Low <= size)
    {
        Mid = (Low + size) / 2;
        Difference = elements[Mid] - target;

        if (Difference == 0) {
            index = Mid;
            return index;
        }
        else if (Difference < 0)
            Low = Mid + 1;
        else
            size = Mid - 1;
    }
    index = -1;   // If reach here, Target was not found.
    return index;
}
--------------------------------------------------------------------
ExtIndexList.h
---------------------------
#pragma once
#include <iostream>
#include <string>
#include "IndexList.h"
using namespace std;

#ifndef EXTINDEXLIST_H
#define EXTINDEXLIST_H

template<class T, int maxsize>
class ExtList : public indexList<T, maxsize> {
public:
    ExtList();
    bool append(const indexList&, const indexList&);
    void selSort();
    int Binary(int);
};

#endif // !EXTINDEXLIST_H
------------------------------------------------------------------
IndexList.cpp
---------------------------
#include <iostream>
#include <string>
#include "IndexList.h"
using namespace std;

template<class T, int maxsize>
indexList<T, maxsize>::indexList(){
    size = 0; //List is empty
}

template<class T, int maxsize>
bool indexList<T, maxsize>::append(const int& item) {
    bool result;   //used to check if the item was added or not
    if(size < maxsize) {
        elements[size] = item;
        size++;
        result = true;
    }
    else {
        cerr << "CANNOT APPEND THE ITEM, THE ARRAY IS FULL!";
        result = false;
    }
    return result;
}

template<class T, int maxsize>
void indexList<T, maxsize>::display()const {
    for (int i = 0; i < size; i++)
        cout << elements[i] << ", ";
}

template<class T, int maxsize>     //SelSort not implemented
void indexList<T, maxsize>::selSort() {}
--------------------------------------------------------------
IndexList.h
-------------------------
#pragma once
#include <iostream>
#include <string>
using namespace std;
#ifndef INDEXLIST_H
#define INDEXLIST_H

int const maxsize = 100;
template<class T, int maxsize>
class indexList {
public:
    T elements[maxsize];    //store elements
    int size;    //keeps tracks of the size;
    indexList();
    bool append(const int&);
    void selSort();
    void display()const;
};
#endif

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