Question

Studying for an exam and would like help on the following review question: 17. Consider the...

Studying for an exam and would like help on the following review question:

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.

  1. Implement the destructor. (5 points) // ~DB_Arrays( );
  1. Implement the function “Print_Arrays” which prints the contents of both arrays (DB1 followed by DB2). (10 points)

// void Print_Arrays( );

  1. 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. (15 points) //       DB_Arrays( );
  1. 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. (10 points)

//DB_Arrays(const DB_Arrays & Org);

  1. 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. (10 points)    // void Triple_Size( );           
  1. 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.

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

  1. 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. (10 points)

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

  1. Write a driver to test the functionality of your class shown in Figure 1. If a function uses another function or if it is implied that a function is used by another function please state so in your program. I want to see that you know how different functions are called.(5 points)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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:\n";
for(int i=0; i<DB1_count; i++)
{
if(DB1[i] != "")
cout<<DB1[i]<<" ";
}
cout<<endl<<"DB2 contents:\n";
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<<"\n\nAfter Insert: \n";
arrays.Print_Arrays();
cout<<"\n\nTotal Length: \n"<<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

Alright Dude, If that worked for you... don't forget to give THUMBS UP.(that will work for me!)

Add a comment
Know the answer?
Add Answer to:
Studying for an exam and would like help on the following review question: 17. Consider the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This project is divided into 3 parts: Part 1. Create a new project and download the arrayList and...

    This project is divided into 3 parts: Part 1. Create a new project and download the arrayList and unorderedArrayList templates that are attached. Create a header file for your unorderedSet template and add it to the project. An implementation file will not be needed since the the new class will be a template. Override the definitions of insertAt, insertEnd, and replaceAt in the unorderedSet template definition. Implement the template member functions so that all they do is verify that the...

  • C++ assignment help! The instructions are below, i included the main driver, i just need help...

    C++ assignment help! The instructions are below, i included the main driver, i just need help with calling the functions in the main function This assignment will access your skills using C++ strings and dynamic arrays. After completing this assignment you will be able to do the following: (1) allocate memory dynamically, (2) implement a default constructor, (3) insert and remove an item from an unsorted dynamic array of strings, (4) use the string class member functions, (5) implement a...

  • The function retrieveAt of the class arrayListType is written as a void function. Rewrite this function...

    The function retrieveAt of the class arrayListType is written as a void function. Rewrite this function so that it is written as a value returning function, returning the required item. If the location of the item to be returned is out of range, use the assert function to terminate the program. note: please give all code in c++ below is the class and implementation(a test program would be helpful in determining how to use it): class arrayListType { public:    ...

  • Java Write the function void insertAtTail (int v). Don’t add any class variables to the List...

    Java Write the function void insertAtTail (int v). Don’t add any class variables to the List class. Here are the class definitions of Node and List that implement a linked list. class Node {private Node next; private int key; Node (Node nxt, int keyValue);//constructor Node getNext(); int getKey(); void putNext(Node nxt);} class List {//assume the class does not use a dummy Node private Node head; List ();//constructor boolean exists (int ky);//returns true if v is in the list void insertAtHead(int...

  • I need help implementing class string functions, any help would be appreciated, also any comments throughout...

    I need help implementing class string functions, any help would be appreciated, also any comments throughout would also be extremely helpful. Time.cpp file - #include "Time.h" #include <new> #include <string> #include <iostream> // The class name is Time. This defines a class for keeping time in hours, minutes, and AM/PM indicator. // You should create 3 private member variables for this class. An integer variable for the hours, // an integer variable for the minutes, and a char variable for...

  • Data Structures and Algorithm Analysis – Cop 3530 Module 3 – Programming Assignment This assignment will...

    Data Structures and Algorithm Analysis – Cop 3530 Module 3 – Programming Assignment This assignment will access your skills using C++ strings and dynamic arrays. After completing this assignment you will be able to do the following: (1) allocate memory dynamically, (2) implement a default constructor, (3) insert and remove an item from an unsorted dynamic array of strings, (4) use the string class member functions, (5) implement a copy constructor, (6) overload the assignment operator, (7) overload the insertion...

  • IntList Recursion Assignment Specifications: You will add some additional recursive functions to your IntList class as...

    IntList Recursion Assignment Specifications: You will add some additional recursive functions to your IntList class as well as make sure the Big 3 are defined. IntNode class I am providing the IntNode class you are required to use. Place this class definition within the IntList.h file exactly as is. Make sure you place it above the definition of your IntList class. Notice that you will not code an implementation file for the IntNode class. The IntNode constructor has been defined...

  • CSBP 319 Data structures - Linked Lists - USE JAVA (NetBeans) A company would like to...

    CSBP 319 Data structures - Linked Lists - USE JAVA (NetBeans) A company would like to implement its inventory of computing machines as a linked list, called ComputerList. Write a Computer node class, called ComputerNode, to hold the following information about a Computer: • code (as a String) • brand (as a String) • model (as a String) • price (as double) • quantity (as int) ComputerNode should have constructors and methods (getters, setters, and toString()) to manage the above...

  • How do I do this? -> string print() const; Function to output the data, return the...

    How do I do this? -> string print() const; Function to output the data, return the output in the form of string, with all elements in the hash table included and each seperated by a space this is my code: template <class elemType> string hashT<elemType>::print() const { for (int i = 0; i < HTSize; i++){        if (indexStatusList[i] == 1){        cout <<HTable[i]<< " " << endl;        }   }    } **********Rest of the code...

  • Please answer read the question first, don't just copy the answer from other websites, thank you...

    Please answer read the question first, don't just copy the answer from other websites, thank you very much. and this is a c++ problem. 17.Given the following class definitions class Deque; class DequeNode friend class Deque; private: int data; DequeNode *prev; DequeNode *next; class Deque private: DequeNode *head; int number; DequeNode *tail; public: Deque ) Deque() void push front (const int &); void push back (const int &) int front () int back ) int pop front ); int pop...

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