Question

#include <iostream> using namespace std; template <typename Item> class MyArray{ private:    Item *myarray;    int...

#include <iostream>

using namespace std;

template <typename Item>
class MyArray{
private:
   Item *myarray;
   int size;
   int used;
   void doubleSize();
public:
   MyArray();
   ~MyArray();
   int length();
   void insertHead(Item i);
   void insertTail(Item i);
   void deleteHead();
   void deleteTail();
   void sortAscending();
   void sortDescending();
   Item operator [](int i){
       return myarray[i];
   }
};

template <typename Item>
MyArray<Item>::MyArray(){
   size = 5;
   used = 0;
   myarray = new Item[size];
}

template <typename Item>
MyArray<Item>::~MyArray(){
   delete[] myarray;
}

template <typename Item>
int MyArray<Item>::length(){
   return used;
}

template <typename Item>
void MyArray<Item>::doubleSize(){
   if(used == size){
       size *= 2;
       Item *newArr = new Item[size];
       for(int i = 0; i < used; ++i){
           newArr[i] = myarray[i];
       }
       myarray = newArr;
   }
}

template <typename Item>
void MyArray<Item>::insertHead(Item i){
   doubleSize();
   for(int i = used; i > 0; --i){
       myarray[i] = myarray[i - 1];
   }
   used++;
   myarray[0] = i;
}

template <typename Item>
void MyArray<Item>::insertTail(Item i){
   doubleSize();
   myarray[used++] = i;
}

template <typename Item>
void MyArray<Item>::deleteHead(){
   if(used > 0){
       for(int i = 0; i < used - 1; ++i){
           myarray[i] = myarray[i + 1];
       }
       --used;
   }
}

template <typename Item>
void MyArray<Item>::deleteTail(){
   if(used > 0){
       --used;
   }
}

template <typename Item>
void MyArray<Item>::sortAscending(){
   for(int i = 0; i < used; ++i){
       for(int j = 0; j < used - 1; ++j){
           if(myarray[j] > myarray[j + 1]){
               Item temp = myarray[j];
               myarray[j] = myarray[j + 1];
               myarray[j + 1] = temp;
           }
       }
   }
}

template <typename Item>
void MyArray<Item>::sortDescending(){
   for(int i = 1; i < used; ++i){
       for(int j = i; j > 0; --j){
           if(myarray[j] > myarray[j - 1]){
               Item temp = myarray[j];
               myarray[j] = myarray[j - 1];
               myarray[j - 1] = temp;
           }
       }
   }
}

int main(){
   MyArray<int> a1;
   a1.insertTail(35);
   a1.insertTail(45);
   a1.insertHead(55);
   a1.sortDescending();
   a1.deleteTail();
   cout << "Number of elements in a1= " << a1.length() << endl;
   for(int i = 0; i < a1.length(); i++)
       cout << a1[i] << endl;
   return 0;
}

Implement a generic (template) class called MyArray. A generic class is a general version of a class; it is a homogenous data structure that handles multiple data types. MyArray will be similar to C++ arrays but will not be constrained to a fixed size. The array size will grow dynamically when the user inserts an element to a full array. The class should not use vectors. Instead, you should use dynamic memory allocation (a dynamic array).

The code is fine but I need to make a main functions

Write a main( ) method that provides a menu driven interface that allows the user to test whether each member function of myArray is working properly. The menu should provide the following functionalities:

o Create a new array: allows the user to create an array of one of the following types: bool, char, double, float, int, string

o Get Length: the current number of elements used in the array

o Insert a new element at the head of the array

o Insert a new element at the tail of the array

o Delete the first element

o Delete the last element

o Sort the elements in ascending order

o Sort the elements in descending order

o Print all the elements in the array

o Quit the program

0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 10 more requests to produce the answer.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
#include <iostream> using namespace std; template <typename Item> class MyArray{ private:    Item *myarray;    int...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • #include <iostream> using namespace std; int main(void) {    int SIZE;    cout<<"Enter the size of the...

    #include <iostream> using namespace std; int main(void) {    int SIZE;    cout<<"Enter the size of the array"<<endl;    cin>>SIZE;     int *numlist = new int[SIZE];     // Read SIZE integers from the keyboard     for (int i = 0; i<SIZE; i++ )    {        cout << "Enter value #" << i+1 << ": ";        cin >> numlist[i];     }     // Display the numbers in a reverse order     for (int i = SIZE; i > 0; i--...

  • vector.h: #ifndef VECTOR_H #define VECTOR_H #include <algorithm> #include <iostream> #include <cassert> template <typename T> class Vector...

    vector.h: #ifndef VECTOR_H #define VECTOR_H #include <algorithm> #include <iostream> #include <cassert> template <typename T> class Vector {     public:         Vector( int initsize = 0 )         : theSize( initsize ),          theCapacity( initsize + SPARE_CAPACITY )         { objects = new T[ theCapacity ]; }         Vector( const Vector & rhs )         : theSize( rhs.theSize),          theCapacity( rhs.theCapacity ), objects( 0 )         {             objects = new T[ theCapacity ];             for( int k = 0; k < theSize; ++k)                 objects[ k ] = rhs.objects[ k...

  • #include <iostream> #include <array> using namespace std; int specific_pattern(short pattern[], int size) {    int pattern_found =...

    #include <iostream> #include <array> using namespace std; int specific_pattern(short pattern[], int size) {    int pattern_found = -1;    for (int i = 0; i < size; i++) {        for (int j = i; j < size; j++) {            if ((pattern[j] - pattern[i]) == 20) {                for (int k = j; k < size; k++) {                    if ((pattern[k] - pattern[j]) == 20) {                        return i;                    }                }            }        }    }    return pattern_found; } int main() {    short data[] = { 10,20,31,40,55,60,65525 };   ...

  • C++ program: Convert the classes to template classes #include <iostream> #include <string> using namespace std; class Node { private: int data; Node* next; public: Node(int...

    C++ program: Convert the classes to template classes #include <iostream> #include <string> using namespace std; class Node { private: int data; Node* next; public: Node(int data) { this->data=data; this->next = 0; } int getData(){return data;} Node* getNext(){return next;} void setNext(Node* next){this->next=next;} }; class LinkedList { private: Node* head = 0; public: int isEmpty() {return head == 0;} void print() { Node* currNode = head; while(currNode!=0) { cout << currNode->getData() << endl; currNode = currNode->getNext(); } } void append(int data) {...

  • #include <iostream> #include <string> using std::string; using std::cout; using std::endl; void testAnswer(string testname, int answer, int...

    #include <iostream> #include <string> using std::string; using std::cout; using std::endl; void testAnswer(string testname, int answer, int expected) { if (answer == expected) cout << "PASSED: " << testname << " expected and returned " << answer << "\n"; else cout << "FAILED: " << testname << " returned " << answer << " but expected " << expected << "\n"; } // Implement printArray here void printArray(int array[],int b) { for(int i = 0; i<b;i++) { std::cout<< array[i] << "...

  • C++ 1. A?B?C?D? which one is correct 2. 3A, 3B #include<iostream> using namespace std; void swap0(int*...

    C++ 1. A?B?C?D? which one is correct 2. 3A, 3B #include<iostream> using namespace std; void swap0(int* ptri, int* ptr2) { int *temp; temp = ptr1; ptr1 = ptr2; ptr2 = temp; void swap1(int ptri, int ptr2){ int temp; temp = ptri; ptr1 = ptr2; ptr2 = temp; portion void swap2(int *&ptri, int *&ptr2){ int* temp; temp = ptr1; ptr1 = ptr2; ptr2 = temp; void swap3(int &ptri, int &ptr2) { int temp; temp = ptr1; ptr1 = ptr2; ptr2 =...

  • In Java, Implement a class MyArray as defined below, to store an array of integers (int)....

    In Java, Implement a class MyArray as defined below, to store an array of integers (int). Many of its methods will be implemented using the principle of recursion. Users can create an object by default, in which case, the array should contain enough space to store 10 integer values. Obviously, the user can specify the size of the array s/he requires. Users may choose the third way of creating an object of type MyArray by making a copy of another...

  • // ArrayIns.java // demonstrates insertion sort 11--- class ArrayIns private long[] a; private int nElems; //...

    // ArrayIns.java // demonstrates insertion sort 11--- class ArrayIns private long[] a; private int nElems; // ref to array a // number of data items public ArrayIns(int max) // constructor a = new long[max]; nElems - © // create the array // no items yet --- public void insert(long value) // put element into array a[nElems] = value; nElems++; // insert it // increment size public void display() // displays array contents for(int j=0; j<ntlems; 1++) 1/ for each element,...

  • 1. Your project will include the following three files: A header file: dynamicArray.h that includes a...

    1. Your project will include the following three files: A header file: dynamicArray.h that includes a list of function prototypes as enumerated in the next section. An implementation file: dynamicArray.cpp that implements the functions declared in the header file. A test driver file: dynamicArray-main.cpp that includes the main() function so that you can test all the functions you've implemented above. 2. The header file dynamicArray.h will include the following list of functions: constructing a dynamic array of the specified size...

  • C++ #include <iostream> using namespace std; bool checkinventoryid(int id) {    return id > 0; }...

    C++ #include <iostream> using namespace std; bool checkinventoryid(int id) {    return id > 0; } bool checkinventoryprice(float price) {    return price > 0; } void endProgram() {    system("pause");    exit(0); } void errorID(string str) {    cout << "Invalid Id!!! " << str << " should be greater than 0" << endl; } void errorPrice(string str) {    cout << "Invalid Price!!!" << str << " should be greater than 0" << endl; } int inputId() {...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT