Question

In: Computer Science

In this exercise you will apply you new understanding of class design to develop an advanced...

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

Solutions

Expert Solution

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


Related Solutions

INTRODUCTION TO WEB APPLICATION DEVELOPMENT 1. Explain and apply the key features of advanced Web design...
INTRODUCTION TO WEB APPLICATION DEVELOPMENT 1. Explain and apply the key features of advanced Web design and programming features (MVC and Razor). 2. Create a web application by using web server controls, event handlers, application state, and session state. 3. Develop client-server applications with form validations using client-side scripting and server-side database connectivity. 4. Analyze security requirements and build websites with authentication and authorization features. 5. Develop service-oriented applications (SOAs) using web services and standard communication frameworks. 6. Enforce quality...
IN C++!!! Exercise #1: Design and implement class Rectangle to represent a rectangle object. The class...
IN C++!!! Exercise #1: Design and implement class Rectangle to represent a rectangle object. The class defines the following attributes (variables) and methods: Two Class variables of type double named height and width to represent the height and width of the rectangle. Set their default values to 0 in the default constructor. A non-argument constructor method to create a default rectangle. Another constructor method to create a rectangle with user-specified height and width. Method getArea() that returns the area. Method...
Programming Exercise Implement the following class design: class Tune { private:    string title; public:   ...
Programming Exercise Implement the following class design: class Tune { private:    string title; public:    Tune();    Tune( const string &n );      const string & get_title() const; }; class Music_collection { private: int number; // the number of tunes actually in the collection int max; // the number of tunes the collection will ever be able to hold Tune *collection; // a dynamic array of Tunes: "Music_collection has-many Tunes" public: // default value of max is a conservative...
Instructions: In this exercise, you’ll design a role playing character class. The Character class attributes should...
Instructions: In this exercise, you’ll design a role playing character class. The Character class attributes should include the characters’s name, gender, class* and race (can be human, elf or dwarf), and additional integer attributes of Strength, Dexterity, Constitution, Intelligence, Wisdom, Charisma. Your class should have a constructor that receives this data. For each attribute, provide setters and getters. The class should include methods that calculate and return the user’s total attributes (sums 6 attributes). Write a Java application that prompts...
Week 3 In-Class Exercise C++ Payroll Design a PayRoll class that is an abstract data type...
Week 3 In-Class Exercise C++ Payroll Design a PayRoll class that is an abstract data type for payroll. It has data members for an employee’s hourly pay rate, number of hours worked, and total pay for the week. Your class must include the following member functions: a constructor to set the hours and pay rate as arguments, a default constructor to set data members to 0, member functions to set each of the member variables to values given as an...
House Class The first class you should develop is a House class, as defined in the...
House Class The first class you should develop is a House class, as defined in the assignment specification. As we have not yet covered inheritance or association, we will not be integrating Banner this week — you will still be writing it for later usage though. Hence, the complete class should be implemented as defined in the UML below (please see the note on interpreting UML). You should maintain the order of the class fields in field declarations, and parameter...
In this assignment you will apply your knowledge and understanding of the AD/AS model to a...
In this assignment you will apply your knowledge and understanding of the AD/AS model to a hypothetical scenario. Write a response to the following scenario on a word file or equivalent and submit it as an 'upload file'. Please note you can only submit the following file types, doc, docx, pdf, txt, png, and jpeg. Graphs can be hand-drawn or computer graphics. Remember to correctly label all your graphs! For example, what the x and y axis represent. The COVID-19...
1.As an advanced management student with an emphasis on corporate finance, you want to apply your...
1.As an advanced management student with an emphasis on corporate finance, you want to apply your acquired knowledge to improve your personal credit history. Present at least three examples from corporate governance that would assist in this goal. Do not forget to be an academic critic, that is, avoid personal opinions without a theoretical basis.
Objective: In this assignment you will apply your knowledge and understanding of the AD/AS model to...
Objective: In this assignment you will apply your knowledge and understanding of the AD/AS model to a hypothetical scenario. Instruction: Write a response to the following scenario on a word file or equivalent and submit it as an 'upload file'. Please note you can only submit the following file types, doc, docx, pdf, txt, png, and jpeg. Graphs can be hand-drawn or computer graphics. Remember to correctly label all your graphs! For example, what the x and y axis represent....
Advanced Digital Design is analyzing a capital investment project for using new computing technology to reduce...
Advanced Digital Design is analyzing a capital investment project for using new computing technology to reduce current operating costs. The new computing technology will have a five-year life with no salvage value at the end of five years. Advanced Digital Design’s cost of capital is 12%. Relevant cash flows and present value factors for 5 years @ 12% are as follows: Investment in computer technology = $500,000. Annual net cash savings from new computer technology = $135,000. Salvage value of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT