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

CS355 programming question Please make sure to do the first and second part Part

ID: 3747513 • Letter: C

Question

CS355 programming question

Please make sure to do the first and second part

Part 1

Build a class listType based on statically allocated array.   Its declaration is provided below (and in shell code provided with this HW):

const int MAX = 100; // max capacity for all listType objects

class listType

{

public:

listType(int max = 5); // constructor

// Post: maxSize <-- max. if max is not specified or <=0, default value 5 will be used. if max > MAX, MAX will be used

//       size <-- 0.

int getSize() const { return size; }            // return # of elements actually stored

int getMaxSize() const { return maxSize; }      // return capacity

bool isEmpty() const { return size == 0; }

bool isFull() const { return size == maxSize; }

int search(int element) const;        // look for an item. return index of first occurrence

int at(int index) const;              // return element at a specific location

void print() const;                   // print content of list on screen in format of [a, b, c] (like what ArrayList in Java does)

bool insert(int element);             // append/insert an element at the end

bool insert(int index, int element);

    // insert an element into location index.

    // Shifts the element currently at that index (if any) and any subsequent elements to the right

bool remove(int index);               // remove element at the specified location

private:

int dataArr[MAX]; // static array storing data items

int size;         // actual # of elements stored. size <= maxSize

int maxSize;      // The capacity of this listType obj. 0 <= maxSize <= MAX.

};

This class behaves like ArrayList in Java, except that each object has a capacity limit maxSize. The elements should always be stored at index [0] ~ [size-1] without any holes between.

Note that the definition of four of the member functions are already provided as inline functions.

search() will return the index of the first occurrence of element if found, otherwise return -1.

Use assert() (<cassert>, chapter 4, p236) to handle invalid index parameter of at(): add this line as the first statement into at(): assert(index >= 0 && index < size);

The two insert()s and the remove() should return true when the operation is done successfully, and return false when the operation fails, such as attempt to insert into a full list, remove from an empty list, or if an invalid index is provided.

Provide appropriate driver code to test this class thoroughly (don’t need to test the four given functions) before proceeding to Part 2.

Part 2

Modify your listType program from Part 1.

Overload output operator (<<) as a standalone function (non-member non-friend). It should work with cout and any ofstream object.

remove member function print()

Overload binary + operator as a member to add two listType objects: should return a listType object which contains all elements from the two operands (two listType objects). You don’t need to handle duplicates or order the elements in any specific way.

Modify your driver accordingly to test those two operators. Here is the output of my partial driver:

Organize your program into header file (interface), implementation file, and driver file. Use #include guard with the header file.

const int MAX = 100; // max capacity for all listType objects

class listType

{

public:

listType(int max = 5); // constructor

// Post: maxSize <-- max. if max is not specified or <=0, default value 5 will be used. if max > MAX, MAX will be used

//       size <-- 0.

int getSize() const { return size; }            // return # of elements actually stored

int getMaxSize() const { return maxSize; }      // return capacity

bool isEmpty() const { return size == 0; }

bool isFull() const { return size == maxSize; }

int search(int element) const;        // look for an item. return index of first occurrence

int at(int index) const;              // return element at a specific location

void print() const;                   // print content of list on screen in format of [a, b, c] (like what ArrayList in Java does)

bool insert(int element);             // append/insert an element at the end

bool insert(int index, int element);

    // insert an element into location index.

    // Shifts the element currently at that index (if any) and any subsequent elements to the right

bool remove(int index);               // remove element at the specified location

private:

int dataArr[MAX]; // static array storing data items

int size;         // actual # of elements stored. size <= maxSize

int maxSize;      // The capacity of this listType obj. 0 <= maxSize <= MAX.

};

list one: [10, 20 Is 40 in list one? Answer -1 means no: -1 Is 10 in list one? Answer -1 means no: 0 list two: [100, 1000. 20. 101 list two after removing I01: [1000. 20. 101 list one after + list two: 10. 20. 1000. 20. 101

Explanation / Answer

//listType.h

#pragma once

#include<iostream>

using namespace std;

const int MAX = 100; // max capacity for all listType objects

class listType

{

public:

listType(int max = 5); // constructor

// Post: maxSize <-- max. if max is not specified or <=0, default value 5 will be used. if max > MAX, MAX will be used

// size <-- 0.

int getSize() const { return size; } // return # of elements actually stored

int getMaxSize() const { return maxSize; } // return capacity

bool isEmpty() const { return size == 0; }

bool isFull() const { return size == maxSize; }

int search(int element) const; // look for an item. return index of first occurrence

int at(int index) const; // return element at a specific location

void print() const; // print content of list on screen in format of [a, b, c] (like what ArrayList in Java does)

bool insert(int element); // append/insert an element at the end

bool insert(int index, int element);

// insert an element into location index.

// Shifts the element currently at that index (if any) and any subsequent elements to the right

bool remove(int index); // remove element at the specified location

//added by cheggEA

friend ostream& operator<<(ostream& out, listType& obj);

listType operator+(listType& obj);

private:

int dataArr[MAX]; // static array storing data items

int size; // actual # of elements stored. size <= maxSize

int maxSize; // The capacity of this listType obj. 0 <= maxSize <= MAX.

};

---------------------------------------------

//listType.cpp

#include"listType.h"

listType::listType(int max) // constructor

// Post: maxSize <-- max. if max is not specified or <=0, default value 5 will be used. if max > MAX, MAX will be used

// size <-- 0.

{

maxSize = max;

size = 0;

}

//int listType::getSize() const { return size; } // return # of elements actually stored

//int listType::getMaxSize() const { return maxSize; } // return capacity

//bool listType::isEmpty() const { return size == 0; }

//bool listType::isFull() const { return size == maxSize; }

int listType::search(int element) const // look for an item. return index of first occurrence

{

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

{

if (dataArr[i] == element)

return i;

}

return -1;

}

int listType::at(int index) const // return element at a specific location

{

return dataArr[index];

}

void listType::print() const // print content of list on screen in format of [a, b, c] (like what ArrayList in Java does)

{

cout << "[ ";

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

{

if (i < size - 1)

cout << dataArr[i] << ",";

else

cout << dataArr[i] ;

}

cout << "]";

cout << endl;

}

bool listType::insert(int element) // append/insert an element at the end

{

if (size < maxSize)

{

dataArr[size++] = element;

return true;

}

else

return false;

}

bool listType::insert(int index, int element)

{

int tmp;

//push elements at given inde to right

if (index < maxSize)

{

for (int i = size; i > index; i--)

{

dataArr[i] = dataArr[i - 1];

}

dataArr[index] = element;

++size;

return true;

}

else

return false;

}

// insert an element into location index.

// Shifts the element currently at that index (if any) and any subsequent elements to the right

bool listType::remove(int index) // remove element at the specified location

{

if (index < size)

{

//move elements after given index to left

for (int i = index; i < size; i++)

{

dataArr[i] = dataArr[i + 1];

}

--size;

return true;

}

else

return false;

}

//added by cheggEA

ostream& operator<<(ostream& out, listType& obj)

{

cout << "[ ";

for (int i = 0; i < obj.size; i++)

{

if(i <obj.size-1)

cout << obj.dataArr[i] << ",";

else

cout << obj.dataArr[i] ;

}

cout << " ]";

cout << endl;

return out;

}

listType listType::operator+(listType& obj)

{

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

{

dataArr[size++] = dataArr[i];

}

for (int i = size; i < obj.size; i++)

{

dataArr[size++] = obj.dataArr[i];

}

return *this;

}

---------------------------------------

//main.cpp

#include"listType.h"

int main()

{

listType list1(20), list2(10);

list1.insert(10);

list1.insert(20);

cout << "list one: ";

list1.print();

list2.insert(100);

list2.insert(1000);

list2.insert(20);

list2.insert(10);

cout << "Is 40 in list one? Answer(-1 means no): " << list1.search(40) << endl;

cout << "Is 10 in list one? Answer(-1 means no): " << list1.search(10) << endl;

cout << "list two: ";

cout << list2;

list2.insert(2,30);

cout << "after insert 30 at index 2: " << list2;

list2.remove(0);

cout << "list two after removing [0]: ";

cout<<list2;

}

/*output

list one: [ 10,20]

Is 40 in list one? Answer(-1 means no): -1

Is 10 in list one? Answer(-1 means no): 0

list two: [ 100,1000,20,10 ]

list two after removing [0]: [ 1000,20,10 ]

*/

/*output2, tested insert at index function,below is output for that too

list one: [ 10,20]

Is 40 in list one? Answer(-1 means no): -1

Is 10 in list one? Answer(-1 means no): 0

list two: [ 100,1000,20,10 ]

after insert 30 at index 2: [ 100,1000,30,20,10 ]

list two after removing [0]: [ 1000,30,20,10 ]

*/