Before posting the solution please read the IMPORTANT NOTE i would post after th
ID: 3684669 • Letter: B
Question
Before posting the solution please read the IMPORTANT NOTE i would post after the question. I will highlight it in bold so it is easy to see.
Here is the question:
In Example 13-10, the class template listType is designed to implement a list in a program. For illustration purposes, that example included only the sorting operation. Extend the definition of the class template to include the remove and search operations. Write the definitions of the member functions to implement the class template listType. Also, write a test program to test various operations on a list. Make sure you implement input statements to test your code.
IMPORTANT NOTE: I will explain the program in 4 parts so it is easier to understand. but the most important of all is the PROGRAM MUST RUN AND COMPILE: I would really appreciate it if it did
1) The program error: I will post the program I have below. I am compiling the program as a project. for some reason there is an error written as "[Error] listType.h: No such file or directory" and "recipe for target 'listType.o' failed". I don't know what it means but I checked the file name properly, but i cant find the error.
2) Program parts: I divided the program into 3 parts: a header file (listType.h), an implementation file (listType.cpp), and a tester file(main.cpp). since the book asked for the program to be tested as a project I had to seperate the header file and the implementation file (in the book they are wirtten as one file). Since it is from the book, when doing the program you must only copy it and not change anything related to it, although the extra function remove and search can be edited as much as possible since it is the programmers work. I will post the picture of the listType from the book at the last part of this question.
3) Extra function/parts: The book specifically asked to extend the definition of the class template to include the remove and search operations. Since this is not from the book you are given free reign to change it however you like. I don't know what errors are there because of "the no such file error" blocking it, but if its fix I would appreciate it.
4) The Main program/ Tester: this is the same as the Extra function, where you are given free reign to change it. The most important thing is that it must have user input when executing. I couldn't test it because of the error. If you find errors here and fixed it, I would appreciate it as well.
Before posting it, make sure that it COMPILES AND RUN, that would really solve all the problem and I would really be thankful. so please test it first so that i don't waste my question.
And without further ado, here is my program. I would divide the program parts with the ============ symbol:
// List type header
// listType.h
#ifndef H_listType
#define H_listType
//Include
#include<iostream>
#include<cassert>
using namespace std;
template <class elemType>
class listType
{
public:
bool isEmpty() const;
bool isFull() const;
int getLength() const;
int getMaxSize() const;
void sort();
void print();
void insertAt(const elemType& item, int position);
void removeAt(int position);
void remove(const elemType& item);
int search(const elemType& item);
listType(int listSize = 50);
~listType();
private:
int maxSize;
int length;
elemType *list;
}; // end def
#endif
=======================================================
//listType.cpp
#include<listType.h>
template <class elemType>
bool listType<elemType>::isEmpty() const
{
return (length == 0); // should this ; be here?
}// end of isEmpty
template <class elemType>
bool listType<elemType>::isFull() const
{
return (length == maxSize);
}// end of isFull
template <class elemType>
int listType<elemType>::getLength() const
{
return length;
} // end of getLength
template <class elemType>
int listType<elemType>::getMaxSize() const
{
return maxSize;
} // end of getMaxSize
// default constructor
template <class elemType>
listType<elemType>::listType(int listSize)
{
maxSize = listSize;
length = 0;
list = new elemType[maxSize];
} //end of constructor listType
template <class elemType>
listType<elemType>::~listType()
{
delete[] list;
} // end of destructor listType
template <class elemType>
void listType<elemType>::sort()
{
int i, j;
int min;
elemType temp;
for (i = 0; i < length; i++) // check if this should be backwards ++i
{
min = i;
for (j = i + 1; j < length; ++j) // ++j? check if this should be j++
if (list[j] < list[min])
min = j;
temp = list[i];
list[i] = list[min];
list[min] = temp;
} //end for
} // end function sort
template <class elemType>
void listType<elemType>::print()
{
int i;
for (i = 0; i < length; ++i) // i++? check if ++i
cout << " " << list[i];
cout << endl;
} // end function print
template <class elemType>
void listType<elemType>::insertAt(const elemType& item, int position)
{
assert(position >= 0 && position < maxSize);
list[position] = item;
length++;
} // end function insertAt
// End of same data from book, experiment with this
// SPECIAL NOTE: this is the added extra function removeAt THE CODE BELOW ARE EXTRA TOO
template <class elemType>
void listType<elemType>::removeAt(int position)
{
assert(position >= 0 && position < maxSize);
for (int i = position; i < (length - 1); i++) // is it ++i
list[i] = list[i + 1];
length--;
cout << " Removal successful";
} // end of function removeAt
// review and check this
template <class elemType>
void listType<elemType>::remove(const elemType& item)
{
int position = search(item);
if (position == -1)
{
cout << " " << item << "doesn't exists in the list. ";
return; // this is void will this be allowed?
}
for (int i = position; i < (length - 1); i++) // should it be ++i?
list[i] = list[i + 1];
length--;
} // end of remove
template <class elemType>
int listType<elemType>::search(const elemType& item)
{
int ind;
for (ind = 0; ind < length; ind++) // ++ind?
if (list[ind] == item)
return ind;
return -1;
} // end function remove
=========================================
//program main/tester
#include<listType.h>
int main() // function main
{
//opening
cout << " Program for templates. ";
listType<int> intList(100);
listType<double> doubleList;
int index;
int myInt;
double myDouble;
cout << " Processing the integer list ";
//prompt and read the numbers into the list
for (index = 0; index < 5; index++)
{
cout << " Enter a (integer) number: ";
cin >> myInt;
intList.insertAt(myInt, index);
} // end for
// print the lements of the list
cout << " Integer list is ";
intList.print();
//sort the list
intList.sort();
// print the elements of the list
cout << " List after sorting is ";
intList.print();
cout << " Enter the element to delete: ";
cin >> myInt;
int searchResult = intList.search(myInt);
if (searchResult == -1)
cout << " " << myInt << " doesn't exists in the list.";
else
intList.removeAt(searchResult);
// print the elements of the list
cout << " Integer list is ";
intList.print();
cout << " Processing the double list ";
//prompt and read the numbers into the list
for (index = 0; index < 5; index++)
{
cout << " Enter a (double) number: ";
cin >> myDouble;
doubleList.insertAt(myDouble, index);
} //end for
// print the elements of the list
cout << " Double list is ";
doubleList.print();
//sort the list
doubleList.sort();
//print the elements of the list
cout << " List after sorting is ";
doubleList.print();
cout << " Enter the element to delete: ";
cin >> myDouble;
searchResult = doubleList.search(myDouble);
if (searchResult == -1)
cout << " " << myDouble << " doesn't exists in the list.";
else
doubleList.removeAt(searchResult);
//print the elements of the list
cout << " Integer list is ";
doubleList.print();
return 0; // program end
}// end main
===============================================================
And here is Example 13-10 of the book for the template listType:
Explanation / Answer
// List type header
// listType.h
#ifndef H_listType
#define H_listType
//Include
#include <iostream>
#include <cassert>
//using namespace std;
template <class elemType>
class listType
{
public:
bool isEmpty() const;
bool isFull() const;
int getLength() const;
int getMaxSize() const;
void sort();
void print();
void insertAt(const elemType& item, int position);
void removeAt(int position);
void remove(const elemType& item);
int search(const elemType& item);
listType(int listSize = 50);
~listType();
private:
int maxSize;
int length;
elemType *list;
}; // end def
#endif
==============================================================
//listType.cpp
#include "listType.h"
#include <iostream>
using namespace std;
template <class elemType>
bool listType<elemType>::isEmpty() const
{
return (length == 0); // should this ; be here?
}// end of isEmpty
template <class elemType>
bool listType<elemType>::isFull() const
{
return (length == maxSize);
}// end of isFull
template <class elemType>
int listType<elemType>::getLength() const
{
return length;
} // end of getLength
template <class elemType>
int listType<elemType>::getMaxSize() const
{
return maxSize;
} // end of getMaxSize
// default constructor
template <class elemType>
listType<elemType>::listType(int listSize)
{
maxSize = listSize;
length = 0;
list = new elemType[maxSize];
} //end of constructor listType
template <class elemType>
listType<elemType>::~listType()
{
delete[] list;
} // end of destructor listType
template <class elemType>
void listType<elemType>::sort()
{
int i, j;
int min;
elemType temp;
for (i = 0; i < length; i++) // check if this should be backwards ++i
{
min = i;
for (j = i + 1; j < length; ++j) // ++j? check if this should be j++
if (list[j] < list[min])
min = j;
temp = list[i];
list[i] = list[min];
list[min] = temp;
} //end for
} // end function sort
template <class elemType>
void listType<elemType>::print()
{
int i;
for (i = 0; i < length; ++i) // i++? check if ++i
cout << " " << list[i];
cout << endl;
} // end function print
template <class elemType>
void listType<elemType>::insertAt(const elemType& item, int position)
{
assert(position >= 0 && position < maxSize);
list[position] = item;
length++;
} // end function insertAt
// End of same data from book, experiment with this
// SPECIAL NOTE: this is the added extra function removeAt THE CODE BELOW ARE EXTRA TOO
template <class elemType>
void listType<elemType>::removeAt(int position)
{
assert(position >= 0 && position < maxSize);
for (int i = position; i < (length - 1); i++) // is it ++i
list[i] = list[i + 1];
length--;
cout << " Removal successful";
} // end of function removeAt
// review and check this
template <class elemType>
void listType<elemType>::remove(const elemType& item)
{
int position = search(item);
if (position == -1)
{
cout << " " << item << "doesn't exists in the list. ";
return; // this is void will this be allowed?
}
for (int i = position; i < (length - 1); i++) // should it be ++i?
list[i] = list[i + 1];
length--;
} // end of remove
template <class elemType>
int listType<elemType>::search(const elemType& item)
{
int ind;
for (ind = 0; ind < length; ind++) // ++ind?
if (list[ind] == item)
return ind;
return -1;
} // end function remove
==========================================================
//program main/tester
#include "listType.h"
#include "listType.cpp"
#include <iostream>
using namespace std;
int main() // function main
{
//opening
cout << " Program for templates. ";
listType<int> intList(100);
listType<double> doubleList;
int index;
int myInt;
double myDouble;
cout << " Processing the integer list ";
//prompt and read the numbers into the list
for (index = 0; index < 5; index++)
{
cout << " Enter a (integer) number: ";
cin >> myInt;
intList.insertAt(myInt, index);
} // end for
// print the lements of the list
cout << " Integer list is ";
intList.print();
//sort the list
intList.sort();
// print the elements of the list
cout << " List after sorting is ";
intList.print();
cout << " Enter the element to delete: ";
cin >> myInt;
int searchResult = intList.search(myInt);
if (searchResult == -1)
cout << " " << myInt << " doesn't exists in the list.";
else
intList.removeAt(searchResult);
// print the elements of the list
cout << " Integer list is ";
intList.print();
cout << " Processing the double list ";
//prompt and read the numbers into the list
for (index = 0; index < 5; index++)
{
cout << " Enter a (double) number: ";
cin >> myDouble;
doubleList.insertAt(myDouble, index);
} //end for
// print the elements of the list
cout << " Double list is ";
doubleList.print();
//sort the list
doubleList.sort();
//print the elements of the list
cout << " List after sorting is ";
doubleList.print();
cout << " Enter the element to delete: ";
cin >> myDouble;
searchResult = doubleList.search(myDouble);
if (searchResult == -1)
cout << " " << myDouble << " doesn't exists in the list.";
else
doubleList.removeAt(searchResult);
//print the elements of the list
cout << " Integer list is ";
doubleList.print();
return 0; // program end
}// end main
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.