Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need this lab completed in C++. The three parts of code I have here start with the arraylist.cpp, than the arraylist.h, and finally the main.cpp.

Need this lab completed in C++.

The three parts of code I have here start with the arraylist.cpp, than the arraylist.h, and finally the main.cpp. In Bold is the instructions for the three steps i need done.

/******************************

* Week 1 lab - exercise 1: *

* a simple ArrayList class *

*******************************/

#include

#include "ArrayList.h"

using namespace std;

/*

* Default constructor. Sets length to 0, initializing the list as an empty

* list. Default size of array is 20.

*/

ArrayList::ArrayList()

{

SIZE = 20;

list = new int[SIZE];

length = 0;

}

/*

* Destructor. Deallocates the dynamic array list.

*/

ArrayList::~ArrayList()

{

delete [] list;

list = NULL;

}

/*

* Determines whether the list is empty.

*

* Returns true if the list is empty, false otherwise.

*/

bool ArrayList::isEmpty()

{

return length == 0;

}

/*

* Prints the list elements.

*/

void ArrayList::display()

{

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

cout << list[i] << " ";

cout << endl;

}

/*

* Adds the element x to the end of the list. List length is increased by 1.

*

* x: element to be added to the list

*/

void ArrayList::add(int x)

{

if (length == SIZE)

{

cout << "Insertion Error: list is full" << endl;

}

else

{

list[length] = x;

length++;

}

}

/*

* Removes the element at the given location from the list. List length is

* decreased by 1.

*

* pos: location of the item to be removed

*/

void ArrayList::removeAt(int pos)

{

if (pos < 0 || pos >= length)

{

cout << "Removal Error: invalid position" << endl;

}

else

{

for ( int i = pos; i < length - 1; i++ )

list[i] = list[i+1];

length--;

}

}

1. Modify the class ArrayList given in Exercise 1 by using expandable arrays. That is, if the list is full when an item is being added to this list, the elements will be moved to a larger array. The new array should have twice the size of the original array.

/******************************

* Week 1 lab - exercise 1: *

* a simple ArrayList class *

*******************************/

/*

* Class implementing an array based list.

*/

class ArrayList

{

public:

ArrayList ();

~ArrayList();

bool isEmpty();

void display();

void add(int);

void removeAt(int);

private:

int SIZE; //size of the array that stores the list items

int *list; //array to store the list items

int length; //amount of elements in the list

};

1.1 Using the new class ArrayList, write a program to store 1,000 random numbers, each in the interval [0, 500]. The initial size of the array in the class should be set to 100. Print the numbers.

/******************************

* Week 1 lab - exercise 1: *

* a simple ArrayList class *

*******************************/

#include

#include "ArrayList.h"

using namespace std;

/*

* Program to test the ArrayList class.

*/

int main()

{

ArrayList myList;

if (!myList.isEmpty()) myList.removeAt(0);

else myList.add(-1);

//the list contains -1

myList.display();

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

{

myList.add(rand()%100); //add some random numbers to the list

}

//the list contains 6 numbers; the first one is -1

myList.display();

if (!myList.isEmpty()) myList.removeAt(0);

else myList.add(-1);

//the list contains 5 numbers; -1 has been removed

myList.display();

return 0;

}

2.

Create a class Bag (multiset) that uses an expandable array to store the bag items. The item type must be int. The class should have the methods listed below. Create a main class to test your bag class. This main class should fill a bag of integers with 10 random numbers, each in the interval [0, 20], and print how many times each integer in the interval [0, 20] appears in the bag.

Bag(): default constructor

bool isEmpty(): determines whether the bag is empty

void print(): prints the bag elements

int getLength(): returns the number of items in the bag

void clear(): removes all of the items from the bag

void add(int item): adds an item to the bag

void remove(int item): removes item from the bag; only one occurrence of item should be removed.

int count(int item): counts the number of occurrences of item in the bag.

3.Write a program that fills a vector object with the keywords of the C++ language. Once filled, the vector elements should be displayed.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Driven Web Sites

Authors: Mike Morrison, Joline Morrison

1st Edition

061901556X, 978-0619015565

More Books

Students also viewed these Databases questions

Question

Derive expressions for the rates of forward and reverse reactions?

Answered: 1 week ago

Question

Write an expression for half-life and explain it with a diagram.

Answered: 1 week ago

Question

What do you mean by underwriting of shares ?

Answered: 1 week ago

Question

Define "Rights Issue".

Answered: 1 week ago

Question

3. Is there opportunity to improve current circumstances? How so?

Answered: 1 week ago