Question

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 operator, (8) search an array, (9) change the size of a dynamic array, (10) write function and program headers, (11) implement a destructor, and (12) implement a destructor.

1. For this assignment you will need the following files which are given: a. The file “dynamic_array_driver.cpp” contains the declaration and implementation for the dynamic array class “tlist”. b. The file “ourData.txt” contains input data for the program. c. The file “dynamic_array_output.txt” contains the output that the main program you write will produce when you add it to the file “dynamic_array_driver.cpp”.

2. Comment every item in the private and private areas of the class declaration. Be specific in your choice of words. Consider the following as appropriate comments for some of the items in the class declaration: a. Destructor b. Copy construction c. Delete an item from the dynamic array … d. Overloaded assignment operator as a member function with chaining.. e. The dynamic array

3. Write the program header for the program “dynamic_array_driver.cpp”. Your program header should contain the following fields: your name, total points, due date, course, assignment name, professor’s name and program description (see the file dynamic_array_driver.cpp”.

4. Write a function header for each function in the class declaration (see file “dynamic_array_driver.cpp”). Each function header should be placed above the function implementation; the code that is provided is enough information for you to write correct function headers. Remember, function headers should include the function name, precondition, post-condition, and function description.

5. Write a main program to produce the output, exactly, as in the file “dynamic_array_output.txt”. Use the file “ourData.txt” for input to your program. The body of your main program (which you will add to the file “dynamic_array_driver.cpp”) should have no more than 20 lines of code; this includes blank lines. The key to doing a good job on this assignment, is to read and understand the code that is provided. Hint: any output and/or messages should be done inside the functions of the class. Your main program should only use “cout” when invoking (calling) the overloaded insertion operator (<<).

ourData.txt contains:

google

cloud

azure

microsoft

sql

database

firefox

netscape

myworld

algorithm

object

class

seacrest

multiplicity

associativity

amazon


#include <iostream>

#include <fstream>

#include <string>

using namespace std;

class tlist

{

public:

tlist();//default

~tlist();

tlist(const tlist &);

bool IsEmpty();

bool IsFull();

int Search(const string &);

void Insert(const string &);

void Remove(const string &);

void Triple_size();

tlist & operator=(const tlist &);

friend ostream & operator<<(ostream &, const tlist &);

private:

string *DB;

int count;

int capacity;

};

//***************************Sample Header Format**********************

// Function Name: Default Constructor

// Pre-Condition: The count, capacity and the dynamic array DB have not been

initialized.

// Post-Condition:The count, capacity and the dynamic array DB have been

initialized.

// Description:The default constructor initializes that state of the class.

//

//********************************************************************

tlist::tlist()

{

string s;

ifstream input;

input.open("ourData.txt");

if (input.fail())

{

cout << "problem with input data file\n";

exit(1);

}

cout << "\n\nDefault Constructor Invoked\n"

<< "***************************\n\n";

count = 0;

capacity = 1;

DB = new string[capacity];

IsEmpty();

while (!input.eof())

{

input >> s;

Insert(s);

}

input.close();

}

tlist::~tlist()

{

cout << "\n\nDestructor Invoked\n"

<< "*******************\n\n";

delete[] DB;

}

tlist::tlist(const tlist & org)

{

cout << "\n\nCopy Constructor Invoked\n"

<< "*************************\n\n";

count = org.count;

capacity = org.capacity;

DB = new string[capacity];

for (int i = 0; i < count; i++)

{

DB[i] = org.DB[i];

}

}

bool tlist::IsEmpty()

{

cout << "\n\nIsEmpty Invoked\n"

<< "****************\n\n";

return count == 0;

}

bool tlist::IsFull()

{

cout << "\n\nIsFull Invoked\n"

<< "***************\n\n";

return count == capacity;

}

int tlist::Search(const string & key)

{

for (int i = 0; i < count; i++)

{

if (DB[i] == key)

{

return i;

}

}

return -1;

}

void tlist::Insert(const string & key)

{

if (IsFull())

{

Triple_size();

}

DB[count++] = key;

}

void tlist::Remove(const string & key)

{

int i = Search(key);

if (i == -1)

{

if (IsEmpty())

{

cout << "DB is empty\n\n";

}

else

{

cout << key << " not in DB\n\n";

}

}

else

{

for (int j = i; j < count - 1; j++)

{

DB[j] = DB[j + 1];

}

cout << key << " found and deleted\n\n";

count--;

}

}

void tlist::Triple_size()

{

cout << "\n\nTriple_size Invoked\n"

<< "***************************\n\n";

capacity *= 3;

string *temp = new string[capacity];

for (int i = 0; i < count; i++)

{

temp[i] = DB[i];

}

delete[] DB;

DB = temp;

}

tlist & tlist::operator=(const tlist & org)

{

cout << "\n\noverloaded assignment operator (=) Invoked\n"

<< "***************************\n\n";

if (this != &org)

{

delete[](*this).DB;

(*this).count = org.count;

(*this).capacity = org.count;

(*this).DB = new string[(*this).capacity];

for (int i = 0; i < (*this).count; i++)

{

(*this).DB[i] = org.DB[i];

}

}

return *this;

}

ostream & operator<<(ostream & cout, const tlist & org)

{

for (int i = 0; i < org.count; i++)

{

cout << org.DB[i] << endl;

}

return cout;

}

int main()

{

//YOUR ADD YOUR CODE FOR MAIN HERE

return 0;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code

Header file

tlist.h

#ifndef _TLIST_H
#define _TLIST_H
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class tlist
{
public:
   tlist();//default
   ~tlist();
   tlist(const tlist &);
   bool IsEmpty();
   bool IsFull();
   int Search(const string &);
   void Insert(const string &);
   void Remove(const string &);
   void Triple_size();
   tlist & operator=(const tlist &);
   friend ostream & operator<<(ostream &, const tlist &);
private:
   string *DB;
   int count;
   int capacity;
};

#endif

tlist.cpp implementation file

#include"tlist.h"
//***************************Sample Header Format**********************
// Function Name: Default Constructor
// Pre-Condition: The count, capacity and the dynamic array DB have not been nitialized.
// Post-Condition:The count, capacity and the dynamic array DB have been initialized.
// Description:The default constructor initializes that state of the class.
//
//********************************************************************
tlist::tlist()
{
   string s;
   ifstream input;
   input.open("ourData.txt");
   if (input.fail())
   {
       cout << "problem with input data file\n";
       exit(1);
   }
   cout<< "\nDefault Constructor Invoked\n"
   << "***************************\n";
   count = 0;
   capacity = 1;
   DB = new string[capacity];
   IsEmpty();
   while (!input.eof())
   {
       input >> s;
       Insert(s);
   }
   input.close();
}
tlist::~tlist()
{
   cout << "\nDestructor Invoked\n"
   << "*******************\n";
   delete[] DB;
}
tlist::tlist(const tlist & org)
{
   cout << "\nCopy Constructor Invoked\n"
   << "*************************\n";
   count = org.count;
   capacity = org.capacity;
   DB = new string[capacity];
   for (int i = 0; i < count; i++)
   {
       DB[i] = org.DB[i];
   }
}
bool tlist::IsEmpty()
{
   cout << "\nIsEmpty Invoked\n"
   << "****************\n";
   return count == 0;
}
bool tlist::IsFull()
{
   cout << "\nIsFull Invoked\n"
   << "***************\n";
   return count == capacity;
}
int tlist::Search(const string & key)
{
   for (int i = 0; i < count; i++)
   {
       if (DB[i] == key)
       {
           return i;
       }
   }
   return -1;
}
void tlist::Insert(const string & key)
{
   if (IsFull())
   {
       Triple_size();
   }
   DB[count++] = key;
}
void tlist::Remove(const string & key)
{
   int i = Search(key);
   if (i == -1)
   {
       if (IsEmpty())
       {
           cout << "DB is empty\n";
       }
       else
       {
           cout << key << " not in DB\n";
       }
   }
   else
   {
       for (int j = i; j < count - 1; j++)
       {
           DB[j] = DB[j + 1];
       }
       cout << key << " found and deleted\n";
       count--;
   }
}
void tlist::Triple_size()
{
   cout << "\nTriple_size Invoked\n"
   << "***************************\n";
   capacity *= 3;
   string *temp = new string[capacity];
   for (int i = 0; i < count; i++)
   {
       temp[i] = DB[i];
   }
   delete[] DB;
   DB = temp;
}
tlist & tlist::operator=(const tlist & org)
{
   cout << "\noverloaded assignment operator (=) Invoked\n"
   << "***************************\n";
   if (this != &org)
   {
       delete[](*this).DB;
       (*this).count = org.count;
       (*this).capacity = org.count;
       (*this).DB = new string[(*this).capacity];
       for (int i = 0; i < (*this).count; i++)
       {
           (*this).DB[i] = org.DB[i];
       }
   }
   return *this;
}
ostream & operator<<(ostream & cout, const tlist & org)
{
   cout<<"\noverloaded friend operator<< with chaining Invoked"<<endl;
   cout<<"******************************************************"<<endl;
   for (int i = 0; i < org.count; i++)
   {
       cout << org.DB[i] << endl;
   }
   return cout;
}

dynamic_array_driver.cpp file


#include"tlist.h"
int main()
{
   //creating the object of the tlist calss named list1
   tlist list1;
   //print the list1
   cout<<list1;
   //creating the another object list2 with copy construcotr
   tlist list2(list1);
   //print the list2
   cout<<list2;
   //try to remove amzon word from the list2
   list2.Remove("amazon");
   //print the lis2
   cout<<list2;
   //try to remove firefox word from the list2
   list2.Remove("firefox");
   //print the lis2
   cout<<list2;
   //try to remove xyz word from the list2
   list2.Remove("xyz");
   //create new list named list3
   tlist list3;
   //assign the list1 to list 3
   list3=list1;
   //print list3
   cout<<list3;
  
return 0;
}

outputs

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Add a comment
Know the answer?
Add Answer to:
Data Structures and Algorithm Analysis – Cop 3530 Module 3 – Programming Assignment This assignment will...
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
  • 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...

  • SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL CODE PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT NEEDED!!! myst...

    SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL CODE PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT NEEDED!!! mystring.h: //File: mystring1.h // ================ // Interface file for user-defined String class. #ifndef _MYSTRING_H #define _MYSTRING_H #include<iostream> #include <cstring> // for strlen(), etc. using namespace std; #define MAX_STR_LENGTH 200 class String { public: String(); String(const char s[]); // a conversion constructor void append(const String &str); // Relational operators bool operator ==(const String &str) const; bool operator !=(const String &str) const; bool operator >(const...

  • Please do it carefully Using the header file ( MyArray.h ) Type the implementation file MyArray.cpp,...

    Please do it carefully Using the header file ( MyArray.h ) Type the implementation file MyArray.cpp, and a test file to test the functionality of the class. Hint: read all the explanations in the header with attention. MyArray.h : #ifndef MYARRAY_H #define MYARRAY_H #include <iostream> using namespace std; class MyArray { friend ostream& operator<<( ostream & output, const MyArray & rhs); // to output the values of the array friend istream& operator>>( istream & input, MyArray & rhs); // to...

  • C++ Assignment - Only Implementation file( VectorDouble.cpp file) required. The header file is already given. Please help, thumbs up guaranteed. Chapter 8 discussed vectors, which are like arrays that...

    C++ Assignment - Only Implementation file( VectorDouble.cpp file) required. The header file is already given. Please help, thumbs up guaranteed. Chapter 8 discussed vectors, which are like arrays that can grow in size. Suppose that vectors were not defined in C++. Define a class called VectorDoublethat is like a class for a vector with base type double. Your class VectorDoublewill have a private member variable for a dynamic array of doubles. It will also have two member variables of type...

  • C++ Write a MyString class that stores a (null-terminated) char* and a length and implements all...

    C++ Write a MyString class that stores a (null-terminated) char* and a length and implements all of the member functions below. Submit your completed source (.cpp) file. Default constructor: empty string const char* constructor: initializes data members appropriately Copy constructor: prints "Copy constructor" and endl in addition to making a copy Move constructor: prints "Move constructor" and endl in addition to moving data Copy assignment operator: prints "Copy assignment" and endl in addition to making a copy Move assignment operator:...

  • Write a MyString class that stores a (null-terminated) char* and a length and implements all of...

    Write a MyString class that stores a (null-terminated) char* and a length and implements all of the member functions below. Default constructor: empty string const char* constructor: initializes data members appropriately Copy constructor: prints "Copy constructor" and endl in addition to making a copy Move constructor: prints "Move constructor" and endl in addition to moving data Copy assignment operator: prints "Copy assignment" and endl in addition to making a copy Move assignment operator: prints "Move assignment" and endl in addition...

  • A library maintains a collection of books. Books can be added to and deleted from and...

    A library maintains a collection of books. Books can be added to and deleted from and checked out and checked in to this collection. Title and author name identify a book. Each book object maintains a count of the number of copies available and the number of copies checked out. The number of copies must always be greater than or equal to zero. If the number of copies for a book goes to zero, it must be deleted from the...

  • The code should be written in C++. I included the Class used for this function. Also,...

    The code should be written in C++. I included the Class used for this function. Also, please fix the main function if necessary. Description: This function de-allocates all memory allocated to INV. This will be the last function to be called (automatically by the compiler) //before the program is exited. This function also erased all data in the data file. There are other functions that add, process and alter the file; the file printed after those functions will have new...

  • choices: supporting function default constructor friend function static member class getter setter attribute assignment operator Identify...

    choices: supporting function default constructor friend function static member class getter setter attribute assignment operator Identify the parts of the following class definition: class Student { string name; const int ID; float gpa; const char gender; public: Student(); Student& operator (const Student&); string getName() const {return name; } friend ostream& operator<<(ostream&, const Student); }; void outputStudent (const Student&); name [Choose Student) [Choose operator Choose) outputStudent Choose operator<< Choose) getNamel) [Choose

  • In C++, develop a class that supports array rotation. Rotating an array is an operation where...

    In C++, develop a class that supports array rotation. Rotating an array is an operation where you shift all elements of the array some number of positions left or right, and elements that are shifted off of the left or right end of the array "wrap around" to the right or left end, respectively. For example, if we rotate the array [1, 2, 3, 4, 5] to the right by 1, we get the array [5, 1, 2, 3, 4]....

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