Make a Container class for an Array program, C++

Oct 7th, 2015
Sigchi4life
Category:
Computer Science
Price: $70 USD

Question description

Provide a Container class for program below (I can't attach the file for some reason so the source code is below). The Container class has to interact with the Array class.  The array in the Array class is defined to have only five elements, which is too restrictive for the ordinary user.  You must declare a static array of 100 members, or you may introduce a dynamic array which provides a new array containing one more element for each new element entered by the user.

#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;

#define XMAXSIZE 10

class Array {
public:
   Array() {listIdx = -1;}
   bool addElement (string);
   bool deleteElement (string);
   bool findElement (string);
   string listElements ();
private:
   string X [XMAXSIZE];
   int listIdx;
};

bool Array::addElement (string data) {
   bool ret = true;
   if (listIdx < XMAXSIZE){
     X[++listIdx] = data;
   }
   else
     ret = false;
   return ret;
}

bool Array::findElement (string data) {
   bool found = false;
   for (int i = 0; i < listIdx+1 && found == false; i++){
     if ((X[i] == data)){
       found = true;
     }
   }
   return found;
}

bool Array::deleteElement(string data) {
   bool found = false;
   if (findElement (data)) {
     for (int i = 0; i < listIdx+1 && found == false; i++){
      if ((X[i] == data)){
       int idxToDelete = i;
       int j = idxToDelete + 1;
       for (; j <= listIdx; j++)
         X[j-1] = X [j];
       X[listIdx--] = "";
       found = true;
      }
     }
   }
   return found;
}

string Array::listElements (){
   string elementList = "";
   if (listIdx > -1){
     for(int i = 0; i <= listIdx; i++)
       elementList += " " + X[i];
   }
   return elementList;
}

int main () {
   Array A;
  string udata;
   bool cont = true;
  char sel;
  while (!cin.eof() && cont == true) {
    cout << "Enter your function: Add, Find, List, Delete, Exit or control-z: ";
    cin >> sel;
    if (!cin.eof()) {
    switch (sel) {     
      case 'A': case 'a': {
       cout << "Enter your string ";
       cin >> udata;
       if (!cin.eof()) {
         if (!A.addElement(udata))
           cout << "Element not added. Array already full" << endl;
         }
      break;
      }
      case 'E': case 'e': {
       cont = false;
       break;
       }
      case 'D': case 'd': {
       cout << "Please Enter User Data value to delete: ";
       cin >> udata;
       if (!cin.eof()) {
         if (A.deleteElement (udata)) {
           cout << "Entry " << udata << " deleted" << endl;
         }
         else
           cout << "Entry " << udata << " not found" << endl;
         }
       break;
       }
     case 'F': case 'f': {
       cout << "Please Enter User Data value to find: ";
       cin >> udata;
       if (!cin.eof()) {
         if (A.findElement(udata))
            cout << "Entry " << udata << " found" << endl;
         else
            cout << "Entry " << udata << " not found" << endl;
           
         }
       break;
       }
     case 'L': case 'l': {
       string W = A.listElements();
       if (W != ""){
         cout << "List of Entries" << endl;  // check this placement
         cout << W << endl;
       }
       else
         cout << "List is Empty";
       cout << endl;
       break;
       }
     default: {
       cout << "Invalid function entered." << endl;
       break;
        }
      }
    }
   }
   //system ("PAUSE");
   return 0;
}


Tutor Answer

(Top Tutor) Daniel C.
(997)
School: Cornell University
PREMIUM TUTOR

Studypool has helped 1,244,100 students

8 Reviews


Summary
Quality
Communication
On Time
Value
darnay
Dec 5th, 2016
" The best tutor out there!!!! "
dontuwannaknow
Nov 26th, 2016
" Excellent work as always thanks so much "
SjSurvivor143
Nov 12th, 2016
" Thanks for the help. "
ddg82
Nov 3rd, 2016
" Thanks, good work "
tinytim47
Oct 21st, 2016
" Wow this is really good.... didn't expect it. Sweet!!!! "
jestin7
Oct 12th, 2016
" This tutor is great! "
mixedballz
Oct 7th, 2016
" excellent work as always thanks for the help "
lilmoe415
Sep 28th, 2016
" Thank you, Thank you, for top quality work, this is your guy!! "
Ask your homework questions. Receive quality answers!

Type your question here (or upload an image)

1829 tutors are online

Brown University





1271 Tutors

California Institute of Technology




2131 Tutors

Carnegie Mellon University




982 Tutors

Columbia University





1256 Tutors

Dartmouth University





2113 Tutors

Emory University





2279 Tutors

Harvard University





599 Tutors

Massachusetts Institute of Technology



2319 Tutors

New York University





1645 Tutors

Notre Dam University





1911 Tutors

Oklahoma University





2122 Tutors

Pennsylvania State University





932 Tutors

Princeton University





1211 Tutors

Stanford University





983 Tutors

University of California





1282 Tutors

Oxford University





123 Tutors

Yale University





2325 Tutors