In: Computer Science
In this exercise you will apply you new understanding of class design to develop an advanced version of the array helper class we created in the classroom. This variation of the design will maintain the data stored in the array in an ordered fashion (0 -100 for our int storage) . The class should always maintain its stored data in order meaning at any time if the programmer iterates the items, they will come back in order. This means each time an item is added it must be inserted at the correct location (sorted). Use the unordered class we created in the classroom as your example. The logic of the member functions will be different since the new class needs to keep things ordered and some methods don't make sense in the context of an ordered array like insertAt(). This function isn't needed because the insertion in an ordered array depends on what is already in the array.
You will also add a bulk loading overload for the constructor that takes an array of ints as its parameter and loads the array in an ordered fashion when the object is instantiated. This should also be available as a public method (function) so that the user can clear the array and load it up again with just two function calls (reset() and then load()).
Below is the UML for the two objects. Use your existing code from class as a template for creating this class. You can use just a header file or both a header and implementation file. Which every you choose you must also include a main that runs through all of the methods to test their functionality.
My code .hpp file
class OrderedArrayADT
{
private:
static const int max_size = 100;
int length;
int data[max_size];
void initialize()
{
for(int i = 0; i < max_size; i ++)
{
data[i] = 0;
}
length = 0;
}
public:
OrderedArrayADT()
{
initialize();
}
void addItem(int item)
{
data[length] = item;
length++;
// logic for ordered array add item:
add (int itemToBeInserted) {
int tempStorage = 0;
for (int i = 0; i < length; i++) {
if (data[i] > itemToBeInserted) {
// positon to insert is found
tempStorage = data[i];
data[i] = itemToBeInserted;
//shift items
}
}
}
// end of logic
}
int replaceItem(int replace, int replaceWith)
{
for(int i = 0; i < length; i++)
{
int oldValue = 0;
if (data[i] == replace)
{
oldValue = data[i];
data[i] = replaceWith;
return oldValue;
}
}
return 0;
}
void removeItem(int item)
{
bool flag = false;
for(int i = 0; i < length; i++ )
{
if(data[i] == item || flag == true)
data[i] = data[i + 1];
flag = true;
}
}
int sumArray()
{
int total = 0;
for(int i = 0; i < length; i++)
{
total += data[i];
}
return total/length;
}
double averageArray()
{
return sumArray() / length;
}
void resetArray()
{
initialize();
}
int getItem(int position)
{
return data[position];
}
int getLength()
{
return length;
}
int stored (int item)
{
}
};
cpp file
#include <iostream>
#include "OrderedArray.h"
using namespace std;
int main()
{
OrderedArrayADT specialArray;
specialArray.addItem(12);
specialArray.removeItem(12);
// specialArray.getItem(4);
cout << specialArray.averageArray();
for(int i = 0; i < specialArray.getLength(); i ++)
{
cout << specialArray.getItem(i);
}
specialArray.stored(int item);
return 0;
}
//Many errors in the cpp file
oarray.cpp
//C++ program to implement ordered array
#include <iostream>
#include "OrderedArray.h"
using namespace std;
int main()
{
cout<<"\n load Ordered array from int array \n";
int arr[] = {100, 22, 3, 4, 50, 6, 7, 8};
unsigned int n = sizeof(arr)/sizeof(arr[0]);
OrderedArrayADT loadArray(arr,n);
for(int i = 0; i < loadArray.getLength(); i++)
{
cout << loadArray.getItem(i) << " ";
}
OrderedArrayADT specialArray;
cout<<"\n Adding Items to ordered array :<12 11 18 128
1> \n";
specialArray.addItem(12);
specialArray.addItem(11);
specialArray.addItem(18);
specialArray.addItem(128);
specialArray.addItem(1);
for(int i = 0; i < specialArray.getLength(); i ++)
{
cout << specialArray.getItem(i) << " ";
}
cout<<"\n remove 12 from ordered array \n ";
specialArray.removeItem(12);
for(int i = 0; i < specialArray.getLength(); i ++)
{
cout << specialArray.getItem(i) << " ";
}
cout<<"\n get item at position 4 \n ";
cout<<specialArray.getItem(3) <<endl; //array starts
from 0
cout<<"\n sum of array \n ";
cout<<specialArray.sumArray();
cout<<"\nAvg of array \n ";
cout << specialArray.averageArray();
cout<<"\nReplace 11 with 100 in array \n ";
specialArray.replaceItem(11,100);
for(int i = 0; i < specialArray.getLength(); i ++)
{
cout << specialArray.getItem(i) << " ";
}
cout<<"\nReplace 12 with 100 in array \n ";
specialArray.replaceItem(12,100);
cout<<"\nreset Array :" ;
specialArray.resetArray();
cout<<"\n Adding Items to ordered array :<100 200>
\n";
specialArray.addItem(100);
specialArray.addItem(200);
for(int i = 0; i < specialArray.getLength(); i ++)
{
cout << specialArray.getItem(i) << " ";
}
return 0;
}
OrderedArray.h
using namespace std;
class OrderedArrayADT
{
private:
static const int max_size = 100;
int length;
int data[max_size];
void initialize()
{
for(int i = 0; i < max_size; i ++)
{
data[i] = 0;
}
length = 0;
}
public:
OrderedArrayADT()
{
initialize();
}
OrderedArrayADT(int *a,unsigned int size)
{
initialize();
for(int i = 0; i < size; i ++)
{
addItem(a[i]);
}
}
void addItem(int itemToBeInserted)
{
if (length >= max_size)
cout<<"cannot insert "<<itemToBeInserted<<" max
size reached \n";
else
{
//add item to its correct position and keep array sorted
int i;
for (i = length - 1; (i >= 0 && data[i] >
itemToBeInserted); i--)
data[i + 1] = data[i];
data[i + 1] = itemToBeInserted;
length++;
}
}
void replaceItem(int replace, int replaceWith)
{
int flag=0;
for(int i = 0; i < length; i++ )
{
if(data[i] == replace)
{
removeItem(replace);
addItem(replaceWith);
flag=1;
break;
}
}
if(flag==0)
cout<<"\nreplace item "<<replace<<" not
found";
}
void removeItem(int item)
{
int flag = -1;
for(int i = 0; i < length; i++ )
{
if(data[i] == item)
{
flag = i;
break;
}
}
if(flag!=-1)
{
for(int i = flag; i < length; i++ )
data[i] =data[i+1];
length--;
}
}
int sumArray()
{
int total = 0;
for(int i = 0; i < length; i++)
{
total += data[i];
}
return total;
}
double averageArray()
{
return sumArray() / length;
}
void resetArray()
{
initialize();
}
int getItem(int position)
{
return data[position];
}
int getLength()
{
return length;
}
};
SAMPLE OUTPUT
load Ordered array from int array
3 4 6 7 8 22 50 100
Adding Items to ordered array :<12 11 18 128 1>
1 11 12 18 128
remove 12 from ordered array
1 11 18 128
get item at position 4
128
sum of array
158
Avg of array
39
Replace 11 with 100 in array
1 18 100 128
Replace 12 with 100 in array
replace item 12 not found
reset Array :
Adding Items to ordered array :<100 200>
100 200