asked 61.6k views
1 vote
Add the function min as an abstract function to the class arrayListType to return the smallest element of the list.

Also, write the definition of the function min in the class unorderedArrayListType and write a program to test this function.

part 3

MY Main


//Data: 18 42 78 22 42 5 42 57

#include
#include "arrayListType.h"
#include "unorderedArrayListType.h"
using namespace std;
int main()
{
unorderedArrayListType intList(25);
int number;
cout << "";
for(int i = 0;i < 8; i++)
{
cin >> number;
intList.insertEnd(number);
}
cout << endl;
intList.print();
cout << endl;

//Testing for min
cout << "The smallest number in intList: "
<< intList.min() << endl;

return 0;
}

1 Answer

5 votes

Answer:

Here is the updated code for the `arrayListType` class with the abstract function `min` added:

```cpp

class arrayListType

{

public:

virtual int min() const = 0;

//other member functions

};

```

And here is the implementation of the `min` function in the `unorderedArrayListType` class:

```cpp

class unorderedArrayListType : public arrayListType

{

public:

int min() const override

{

int minVal = list[0];

for (int i = 1; i < length; i++) {

if (list[i] < minVal) {

minVal = list[i];

}

}

return minVal;

}

//other member functions

};

```

And here's an example program that uses the `min` function:

```cpp

#include <iostream>

#include "arrayListType.h"

#include "unorderedArrayListType.h"

using namespace std;

int main()

{

unorderedArrayListType intList(25);

int number;

cout << "Enter 8 integers: ";

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

{

cin >> number;

intList.insertEnd(number);

}

cout << endl;

intList.print();

cout << endl;

cout << "The smallest number in intList: " << intList.min() << endl;

return 0;

}

```

Assuming that the `arrayListType` and `unorderedArrayListType` header and implementation files are properly included and compiled, this program should output the smallest number in the list.

answered
User Serban Petrescu
by
7.9k points