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

Hey guys, this is the study guide I have for a test but the professor hasn\'t po

ID: 3788766 • Letter: H

Question

Hey guys, this is the study guide I have for a test but the professor hasn't posted the answers to it yet. I understand most of it but I'd like another persons input on completing this. Thanks!

17. Consider the following class declaration when answering this question

class DB_Arrays

{

public:

       DB_Arrays( );                       //default constructor.

       DB_Arrays(const DB_Arrays & Org); //copy constructor.

      ~DB_Arrays( );                 //destructor.

       void Print_Arrays( );         //prints the contents of both arrays (DB1 followed by DB2)

       void Insert(const string &, const int & loc);   //adds a string to the back of DB1 if loc =1; otherwise string is added

                                                                              // to the back of DB2.

       void Remove(const string & key, const int & loc);   // 1)removes key from DB1 if loc = 1 and it is there;

                   //AND 2) removes key from DB2 if loc =2 and it is there;

                                                                        

       int Find(const string & key, cons tint & loc ); //1)returns location of key in DB1 if loc=1 and if it is there; otherwise -1 is returned

                                                          //AND 2) returns location of key in DB2 if loc=2 and if it is there; otherwise -2 is returned

       int Total_Keys( );           //returns the total number of cells occupied in both arrays (DB1 and DB2).

       void Triple_Size( );            //this function triples the capacities of DB1 and DB2. Note that DB1 and DB2 may have different

                                                 //capacities and counts.

private:

       int DB1_capacity;      //total number of cells allocated for DB1.

       int DB2_capacity;     //total number of cells allocated for DB2.

       int DB1_ count;         //total number of strings stored in DB1.

       int DB2_count          //total number of strings stored in DB2.

       string *DB1, *DB2; //arrays DB1 and DB2

};

Figure 1

HINT: I INCLUDED FUNCTION PROTOTYPES AS THE APPEAR IN THE CLASS DECLARATION FOR EACH QUESTION. ALSO REMEMBER TO QUANTIFY ALL MEMBER FUNCTIONS WITH THE CLASS NAME AND “::”. FINALLY REMEMBER TO CHECK ALL NECESSARY CASES WHEN IMPLEMENTING THE CLASS FUNCTIONS.

a.Implement the destructor. // ~DB_Arrays( );

b.Implement the function “Print_Arrays” which prints the contents of both arrays (DB1 followed by DB2).

// void Print_Arrays( );

c.Implement the default constructor. The default constructor will read all the data from a file called “my_data1.txt” into the arrays, DB1 and DB2(s), respectively; at this point DB1 and DB2 will be identical. The file is composed of strings. Start off with an initial capacity of 5 for each array. You may use any functions declared in the class even if you are not asked to implement them. Keep in mind that WHILE you are reading the arrays may become FULL. Used the class declaration in Figure 1 above.//       DB_Arrays( );

d.Implement the copy constructor. Remember, that the copy constructor will perform a deep copy (DB1 and DB2 will have there own memory; keep in mind that DB1 and DB2 may have different capacities, counts and hold different stings.

//DB_Arrays(const DB_Arrays & Org);

e.Implement the function “Triple_Size”; this function triples the capacities of DB1 and DB2. Note that DB1 and DB2 may have different capacities and counts, and store different strings. // void Triple_Size( );

f.Implement the function “Find”. Note the following: 1) Find returns the location of key in DB1 if loc = 1 and if it is there; otherwise -1 is returned, and 2) Find returns the location of key in DB2 if loc=2 and if it is there; otherwise -2 is returned.

//int Find(const string & key, cons tint & loc );

g.Implement the function “Remove”. Note the following: 1)Remove removes key from DB1 if loc = 1 and it is there. if it is not there nothing is done. 2) Remove removes key from DB2 if loc =2 and it is there; if it is not there nothing is done.

//void Remove(const string & key, const int & loc);  

Explanation / Answer

PROGRAM CODE:

/*
* db.cpp
*
* Created on: 08-Feb-2017
* Author: kasturi
*/

#include <iostream>
#include <fstream>
using namespace std;

class DB_Arrays
{
public:
DB_Arrays( ); //default constructor.
DB_Arrays(const DB_Arrays & Org); //copy constructor.
~DB_Arrays( ); //destructor.
void Print_Arrays( ); //prints the contents of both arrays (DB1 followed by DB2)
void Insert(const string &, const int & loc); //adds a string to the back of DB1 if loc =1; otherwise string is added
// to the back of DB2.
void Remove(const string & key, const int & loc); // 1)removes key from DB1 if loc = 1 and it is there;
//AND 2) removes key from DB2 if loc =2 and it is there;

int Find(const string & key, const int & loc ); //1)returns location of key in DB1 if loc=1 and if it is there; otherwise -1 is returned
//AND 2) returns location of key in DB2 if loc=2 and if it is there; otherwise -2 is returned
int Total_Keys( ); //returns the total number of cells occupied in both arrays (DB1 and DB2).
void Triple_Size( ); //this function triples the capacities of DB1 and DB2. Note that DB1 and DB2 may have different
//capacities and counts.
private:
int DB1_capacity; //total number of cells allocated for DB1.
int DB2_capacity; //total number of cells allocated for DB2.
int DB1_count; //total number of strings stored in DB1.
int DB2_count; //total number of strings stored in DB2.
string *DB1, *DB2; //arrays DB1 and DB2
};

DB_Arrays::DB_Arrays()
{
   DB1_capacity = 5;
   DB2_capacity = 5;
   DB1 = new string[DB1_capacity];
   DB2 = new string[DB2_capacity];
   DB1_count = 0;
   DB2_count = 0;
   string line;
   ifstream infile("my_data1.txt");
   while(getline(infile, line))
   {
       DB1[DB1_count++] = line;
       DB2[DB2_count++] = line;

       if(DB1_count==DB1_capacity)
       {
           DB1_capacity += 10;
           DB2_capacity += 10;
           delete [] DB1;
           DB1 = new string[DB1_capacity];
           for(int i=0; i<DB2_count; i++)
           {
               DB1[i] = DB2[i];
           }
           delete [] DB2;
           DB2 = new string[DB2_capacity];
           for(int i=0; i<DB1_count; i++)
           {
               DB2[i] = DB1[i];
           }
       }
   }
   infile.close();
}
DB_Arrays::DB_Arrays(const DB_Arrays & Org)
{
   this->DB1 = Org.DB1;
   this->DB2 = Org.DB2;
   this->DB1_capacity = Org.DB1_capacity;
   this->DB2_capacity = Org.DB2_capacity;
   this->DB1_count = Org.DB1_count;
   this->DB2_count = Org.DB2_count;
}

DB_Arrays::~DB_Arrays( )
{
   delete [] DB1;
   delete [] DB2;
   DB1 = NULL;
   DB2 = NULL;
   DB1_capacity = 0;
   DB2_capacity = 0;
   DB1_count = 0;
   DB2_count = 0;

}
//For simplicity, when removing elements from the array, they are made null
//hence we have a check here to see if the current element is null or not
void DB_Arrays::Print_Arrays( )
{
   cout<<"DB1 contents: ";
   for(int i=0; i<DB1_count; i++)
   {
       if(DB1[i] != "")
           cout<<DB1[i]<<" ";
   }
   cout<<endl<<"DB2 contents: ";
   for(int i=0; i<DB2_count; i++)
   {
       if(DB1[i] != "")
           cout<<DB2[i]<<" ";
   }
}

int DB_Arrays::Find(const string & key, const int & loc )
{
   if(loc == 1)
   {
       for(int i=0; i<DB1_count; i++)
       {
           if(DB1[i] == key)
               return i;
       }
       return -1;
   }
   else if(loc==2)
   {
       for(int i=0; i<DB2_count; i++)
       {
           if(DB2[i] == key)
               return i;
       }
       return -2;
   }
   return -1;
}

void DB_Arrays::Triple_Size( )
{
   DB1_capacity *= 3;
   DB2_capacity *= 3;
   int tempCount = 0;
   string *tempArray = new string[DB1_capacity];
   for(int i=0; i<DB1_count; i++)
   {
       tempArray[i] = DB1[i];
       tempCount++;
   }
   DB1 = new string[DB1_capacity];
   for(int i=0; i<tempCount; i++)
       DB1[i] = tempArray[i];
   DB1_count = tempCount;

   delete [] tempArray;
   tempCount = 0;

   tempArray = new string[DB1_capacity];
   for(int i=0; i<DB2_count; i++)
   {
       tempArray[i] = DB2[i];
       tempCount++;
   }
   DB2 = new string[DB2_capacity];
   for(int i=0; i<tempCount; i++)
       DB2[i] = tempArray[i];
   DB2_count = tempCount;
}

int DB_Arrays::Total_Keys( )
{
   return DB1_count+DB2_count;
}

void DB_Arrays::Insert(const string &key, const int & loc)
{
   if(loc == 1)
   {
       DB1[DB1_count++] = key;
   }
   else if(loc ==2)
   {
       DB2[DB2_count++] = key;
   }
}

//For simplicity, when removing elements from the array, they are made null
void DB_Arrays::Remove(const string & key, const int & loc)
{
   if(loc == 1)
   {
       for(int i=0; i<DB1_count; i++)
       {
           if(DB1[i] == key)
           {
               DB1[i] = "";
               return;
           }
       }
   }
   else if(loc ==2)
   {
       for(int i=0; i<DB2_count; i++)
       {
           if(DB2[i] == key)
           {
               DB2[i] = "";
               return;
           }
       }
   }
}

int main()
{
   DB_Arrays arrays;
   arrays.Print_Arrays();
   arrays.Insert("Mango", 1);
   cout<<" After Insert: ";
   arrays.Print_Arrays();
   cout<<" Total Length: "<<arrays.Total_Keys()<<endl;
   cout<<"Index of Apple: "<<arrays.Find("Apple", 1);
}

OUTPUT:

DB1 contents:

Apple Banana Orange Plums Strawberry Raspberry Blueberry Sweet lime

DB2 contents:

Apple Banana Orange Plums Strawberry Raspberry Blueberry Sweet lime

After Insert:

DB1 contents:

Apple Banana Orange Plums Strawberry Raspberry Blueberry Sweet lime Mango

DB2 contents:

Apple Banana Orange Plums Strawberry Raspberry Blueberry Sweet lime

Total Length:

17

Index of Apple: 0