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

Add (and test!) the following methods (functions) to the EncapsulatedArray class

ID: 3724830 • Letter: A

Question

Add (and test!) the following methods (functions) to the EncapsulatedArray class (C++):

- void swap (int index1, int index2) { swaps two elements of the same array provided index1 and index2}

- void bubbleSort ( ) { uses the bubble sort algorithm implementation to sort the encapsulated array}

- void selectSort ( ) { uses the select sort algorithm implementation to sort the encapsulated array}

- void insertSort ( ) { uses the insert sort algorithm implementation to sort the encapsulated array}

- void deleteIndex (int index_to_delete) { deletes an element at index_to_delete, shifts-left elements on top of deleted element }

- void noDups ( ) { removes all duplicates from a sorted array, shifts-left elements on top of deleted elements }

- void insert_in_order ( int number_to_insert ) { insert a number in the correct order, in an already sorted array }

Explanation / Answer

#include <iostream>
using namespace std;

class Item
{
private:
string name;
double cost;

public:
Item()
{
name = "";
cost = 0.0;
}

Item(string newName, double newCost)
{
name = newName;
cost = newCost;
}

void show()
{
cout << "- " << name << " $" << cost << endl;
}

string get_name()
{
return name;
}

double get_cost()
{
return cost;
}

void set_item(string newName, double newCost)
{
name = newName;
cost = newCost;
}

void setItem(Item item)
{
name = item.get_name();
cost = item.get_cost();
}
};

class Bag
{
private:
Item *items;
int number_items;
int max;

public:
Bag(int Max)
{
items = new Item[Max]; // an array of items
number_items = 0; // this is the index of current item
max = Max; // size of the bag/array
}

void insert_item(string name, double cost)
{
items[number_items].set_item(name, cost);
number_items++;
}

void insertItem(Item item)
{
// items[number_items].set_item(item.get_name(), item.get_cost());
items[number_items].setItem(item);
number_items++;
}

int size()
{
return max;
}

int get_index_of(Item item)
{
// returns the index of an item (based on the name)
int index = -1;
for(int i = 0; i < number_items; i++)
{
if(items[i].get_name() == item.get_name())
{
index = 1;
break;
}
}
return index;
}

void most_expensive()
{
// shows the most expensive item
if(number_items > 0) {
double maxExp = items[0].get_cost();
int index = 0;
for (int i = 0; i < number_items; i++) {
if (items[i].get_cost() > maxExp) {
maxExp = items[i].get_cost();
index = i;
}
}

cout << " The most expensive item: ";
items[index].show();
}
}

void show_reverse()
{
// shows everything in the bag in reverse order of showItems()
for(int i = number_items - 1; i >= 0; i--)
items[i].show();
}

void get_frequency(Item item)
{
// shows how many times an Item (the name) appears in the bag
int count = 0;
for(int i = 0; i < number_items; i++)
{
if(items[i].get_name() == item.get_name())
count++;
}
cout << " The item " << item.get_name() << " appears in the bag " << count << " time/times." << endl;
}

void showItems()
{
for(int i = 0; i < number_items; i++)
items[i].show();
}

void delete_first()
{
// shift all the elements one to the left
for(int i = 0; i < max - 1; i++)
items[i] = items[i+1];
number_items --; // resize bag
}

bool deleteItem(Item item)
{
// delete item(based on the name), shift array elements to the left, return true if successful
int index = -1;
for(int i = 0; i < number_items; i++)
{
if(items[i].get_name() == item.get_name())
{
index = i;
break;
}
}

if(index != -1)
{
number_items--;
for(int i = index; i < number_items; i++)
{
items[i] = items[i+1];
}

return true;
}

else
return false;
}

bool contains(string name_to_find)
{
for(int i = 0; i < number_items; i++)
if(name_to_find == items[i].get_name())
return true;
return false;
}

void sort_by_cost_acending() // bubble sort
{
for(int i = 0; i < number_items - 1; i++)
for(int j = 0; j < number_items - 1; j++)
if(items[j].get_cost() > items[j+1].get_cost())
swap(j, j+1);
}

void swap(int index1, int index2)
{
Item temp = items[index1];
items[index1] = items[index2];
items[index2] = temp;
}

double sum_of_all()
{
// returns the sum of all Items
double sum = 0;
for(int i = 0; i < number_items; i++)
{
sum += items[i].get_cost();
}

return sum;
}
void selectionSort ()
{
int min_idx;
// One by one move boundary of unsorted subarray
for (int i = 0; i < number_items-1; i++)
{
// Find the minimum element in unsorted array
min_idx = i;
for (int j = i+1; j < number_items; j++)
if (items[j].get_cost() < items[min_idx].get_cost())
min_idx = j;

// Swap the found minimum element with the first element
swap(min_idx, i);
}
}
void insertionSort ()
{
Item key;
for (int i = 1; i < number_items; i++)
{
key = items[i];
int j = i-1;

// Move elements of arr[0..i-1], that are greater than key, to one position ahead of their current position
while (j >= 0 && items[j].get_cost() > key.get_cost())
{
items[j+1] = items[j];
j = j-1;
}
items[j+1] = key;
}
}
};

int main()
{
/// Test to see if Item works
cout << "Testing Item: ";
Item anItem("Fuji Apple", 4.99);
Item anItem2("Green Apple", 5.99);
Item anItem3("Apple", 8.99);
Item anItem4("Mango", 6.99);
Item anItem5("Pear", 3.99);
anItem.show();
Bag b(10);
b.insertItem(anItem);
b.insertItem(anItem2);
b.insertItem(anItem3);
b.insertItem(anItem4);
b.insertItem(anItem5);
b.most_expensive();

cout << " Items in reverse: " << endl;
b.show_reverse();
b.get_frequency(anItem);
b.get_index_of(anItem3);
b.deleteItem(anItem4);

cout << " After deleting " << anItem.get_name() << ", items are: " << endl;

b.showItems();
cout << " Sum of all items: " << b.sum_of_all();

cout<<" After Selection sort: ";
b.selectionSort();
b.showItems();

cout<<" After Insertion sort: ";
b.insertionSort();
b.showItems();

return 0;
}

Sample Output:

Testing Item: - Fuji Apple $4.99

The most expensive item: - Apple $8.99

Items in reverse:
- Pear $3.99
- Mango $6.99
- Apple $8.99
- Green Apple $5.99
- Fuji Apple $4.99

The item Fuji Apple appears in the bag 1 time/times.

After deleting Fuji Apple, items are:
- Fuji Apple $4.99
- Green Apple $5.99
- Apple $8.99
- Pear $3.99

Sum of all items: 23.96

After Selection sort:
- Pear $3.99
- Fuji Apple $4.99
- Green Apple $5.99
- Apple $8.99


After Insertion sort:
- Pear $3.99
- Fuji Apple $4.99
- Green Apple $5.99
- Apple $8.99

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote