Develop a proram in c++ that does the following: Here is the IndexOfList files:
ID: 3802321 • Letter: D
Question
Develop a proram in c++ that does the following:
Here is the IndexOfList files:
/ File IndexList.h
#ifndef INDEXLIST_H
#define INDEXLIST_H
#include <iostream>
using namespace std;
template <class T, int maxSize>
class indexList
{
public:
// Constructor
indexList();
// 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
// File: IndexList.cpp
// Indexed list class implementation
#include "IndexList.h"
#include <iostream>
using namespace std;
template <class T, int maxSize>
indexList<T, maxSize>::indexList()
{
size = 0; // list is empty
}
template <class T, int maxSize>
void indexList<T, maxSize>::read(istream& ins)
{
int numItems;
T nextItem;
cout << "Enter number of list items to read: ";
ins >> numItems;
size = 0;
if (numItems >= 0 && numItems <= maxSize)
while (size < numItems)
{
cout << "Enter next item - ";
ins >> nextItem;
elements[size] = nextItem;
size++;
}
else
cerr << "Number of items " << numItems
<< " is invalid"
<< " - data entry is cancelled!" <<
endl;
}
// Display the list contents
// Pre: none
// Post: Displays each item stored in the list
template <class T, int maxSize>
void indexList<T, maxSize>::display() const
{
// Display each list element.
for (int i = 0; i < size; i++)
cout << elements[i] << endl;
}
// Find index of a target item
// Pre: none
// Post: Returns the index of target if found;
// otherwise, return -1.
template <class T, int maxSize>
int indexList<T, maxSize>::search(const T&
target) const
{
for (int i = 0; i < size; i++)
if (elements[i] == target)
return i;
// target not found
return -1;
}
// Get the current size
template <class T, int maxSize>
int indexList<T, maxSize>::getSize() const
{
return size;
}
// Sort the indexed list
template <class T, int maxSize>
void indexList<T, maxSize>::selSort()
{
// Selection sort
}
Explanation / Answer
#ifndef INDEXLIST_H
#define INDEXLIST_H
#include <iostream>
using namespace std;
template <class T, int maxSize>
class indexList
{
public:
// Constructor
indexList();
// 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
// File: IndexList.cpp
// Indexed list class implementation
#include "IndexList.h"
#include <iostream>
using namespace std;
template <class T, int maxSize>
indexList<T, maxSize>::indexList()
{
size = 0; // list is empty
}
template <class T, int maxSize>
void indexList<T, maxSize>::read(istream& ins)
{
int numItems;
T nextItem;
cout << "Enter number of list items to read: ";
ins >> numItems;
size = 0;
if (numItems >= 0 && numItems <= maxSize)
while (size < numItems)
{
cout << "Enter next item - ";
ins >> nextItem;
elements[size] = nextItem;
size++;
}
else
cerr << "Number of items " << numItems
<< " is invalid"
<< " - data entry is cancelled!" <<
endl;
}
// Display the list contents
// Pre: none
// Post: Displays each item stored in the list
template <class T, int maxSize>
void indexList<T, maxSize>::display() const
{
// Display each list element.
for (int i = 0; i < size; i++)
cout << elements[i] << endl;
}
// Find index of a target item
// Pre: none
// Post: Returns the index of target if found;
// otherwise, return -1.
template <class T, int maxSize>
int indexList<T, maxSize>::search(const T&
target) const
{
for (int i = 0; i < size; i++)
if (elements[i] == target)
return i;
// target not found
return -1;
}
// Get the current size
template <class T, int maxSize>
int indexList<T, maxSize>::getSize() const
{
return size;
}
// Sort the indexed list
template <class T, int maxSize>
void indexList<T, maxSize>::selSort()
{
// Selection sort
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.