Question

In: Computer Science

/* WordList source file * * *   This file will contain the function definitions you will...

/* WordList source file
*
*
*   This file will contain the function definitions you will implement.
*   The function signitures may NOT be changed. You may create your own
*   helper functions and include them in this file.
*
*   In addition to the specific instructions for each function, no function
*   should cause any memory leaks or alias m_list in any way that would result
*   in undefined behavior.
*
*   Topics: Multilevel Pointers, Dynamic Allocation, Classes
*
*/

private:
#endif

   unsigned int m_count;   // Number of words currently in list
   unsigned int m_max;       // The total size of the list.
   char** m_list;   // The list storing the words

};

/* Function: Wordlist Constructor
*/
WordList::WordList(const int max_words) {

   m_count = 0;
   if (max_words > 0) {
       m_max = max_words;
       m_list = new char* [max_words];
   }
}

/* Function: Wordlist Copy Constructor
*/
WordList::WordList(const WordList& other) {

   m_count = other.m_count;
   m_max = other.m_max;
   m_list = new char* [m_max];
   for (int i = 0; i < m_max; i++) {
       m_list[i] = other.m_list[i];
   }

}
/* Function: Wordlist Destructor
*/
WordList::~WordList() {

   delete []m_list;

}

/* Function: printList
*/
int   WordList::print() const {   // TODO:
   return -1;

}

/* Function: at
*/
char* WordList::at(const int index) const {   // TODO:
   return nullptr;

}
/* Function: count
*/
int   WordList::count() const { // TODO:
   return -1;

}
/* Function: add
*/
int   WordList::add(const char word[]) { // TODO
   return -2;

}
/* Funtion: remove
*/
int   WordList::remove(const char word[]) { // TODO:
   return -1;
}

/* Funtion: append
*/
int   WordList::append(const WordList* other) { // TODO:
   return -2;

}
/* Funtion: search
*/
int WordList::search(const char word[]) const {   // TODO:
   return -1;

}
/* Funtion: sort
*/
int   WordList::sort() {   // TODO:
return -1;

}
/* Funtion: Assignment Operator
*/
WordList& WordList::operator=(const WordList& other) { // TODO:
   return *this;
}

Solutions

Expert Solution

#include<bits/stdc++.h>
using namespace std;
const int E =  0.01;
const int MAX_ITER = 100;


class Wordlist {
private:
        unsigned int m_count, m_max;
        char ** m_list;

}


WordList::WordList(const int max_words) {
        m_count = 0;
        if (max_words > 0) {
                m_max = max_words;
                m_list = new char*[max_words];
        }
}


Wordlist::Wordlist(const Wordlist& other) {
        m_count = other.m_count;
        m_max = other.m_max;
        m_list = other.m_list;
        for (int i = 0; i < m_max; i++)
                m_list[i] = other.m_list[i];
}


WordList::~WordList() {
        delete []m_list;
}

int Wordlist::print() const {
        for (int i = 0; i < m_max; i++) {
                cout << m_list[i] << " ";
        }
        return -1;
}


char *Wordlist::at(const int index) const {
        return m_list[index];
}

int Wordlist::count()const {
        // int cnt = 0;
        return m_count;
}


int Wordlist::add(const char word[]) {
        char** tmp = new char*[m_count];

        for (int i = 0; i < m_count; i++)
        {
                tmp[i] = m_list[i];
        }

        m_count++;

        delete[] m_list;
        m_list = tmp;

        m_list[m_count - 1] = word;

}

int Wordlist::remove(const char word[]) {
        for (int i = 0; i < m_count; i++) {
                if (m_list[i] == word) {
                        for (int j = i + 1; j < m_count; j++) {
                                m_list[j - 1] = m_list[j];
                        }
                        return 1;
                }
        }
        return -1;
}

int Wordlist::search(const char word[]) {
        for (int i = 0; i < m_count; i++) {
                if (m_list[i] == word) {
                        return i;
                }
        }
        return -1;
}

int WordList::sort() {
        bool exchanges;
        do {
                exchanges = false; // assume no exchanges
                for (int i = 0; i < m_count - 1; i++) {
                        if (m_list[i] > m_list[i + 1]) {
                                double temp = m_list[i];
                                m_list[i] = m_list[i + 1];
                                m_list[i + 1] = temp;
                                exchanges = true; // after exchange, must look again
                        }
                }
        } while (exchanges);

        return 1;
}




Wordlist& Wordlist::operator=(const WordList& other) {
        for (int i = 0; i < m_count; i++) {
                m_list[i] = other[i];
        }
        return *this;
}

Related Solutions

C++ If you tried to compile a source file that doesn't contain a main() function, would...
C++ If you tried to compile a source file that doesn't contain a main() function, would this error be detected by the preprocessor, the compiler, or the linker? Briefly justify your answer. Hint: think about how the files that don't have main() get processed in a project with multiple files. Alternatively, try it out and look at the error message you get.
You are given a source file in your work area for this assignment. It consists of...
You are given a source file in your work area for this assignment. It consists of a declaration for a Val node. There are declarations for three overloaded operator functions, which you must fill in: operator+, operator*, and operator!. operator+ should implement addition. operator* should implement multiplication. operator! should reverse the digits of its operand (i.e., of "this") and return the reversed integer. So for example !123 should return 321. It should be straightforward to reverse an integer. You can...
Write this program in C++ You are given a source file in your work area for...
Write this program in C++ You are given a source file in your work area for this assignment. It consists of a declaration for a Val node. There are declarations for three overloaded operator functions, which you must fill in: operator+, operator*, and operator!. operator+ should implement addition. operator* should implement multiplication. operator! should reverse the digits of its operand (i.e., of "this") and return the reversed integer. So for example !123 should return 321. It should be straightforward to...
For this assignment, you will write a program that consists of four function definitions, including one...
For this assignment, you will write a program that consists of four function definitions, including one main() function. The main function gets inputs from the user, and the other functions show output to the user. When the program is executed, it asks the user to enter a small amount of information, and then shows output based on the user's info. Specification: Here are some specific requirements for this program: The program should consist of four function definitions, written one after...
Write a C++ program to create a text file. Your file should contain the following text:...
Write a C++ program to create a text file. Your file should contain the following text: Batch files are text files created by programmer. The file is written in notepad. Creating a text file and writing to it by using fstream: to write to a file, you need to open thew file as write mode. To do so, include a header filr to your program. Create an object of type fsrteam. Open the file as write mode. Reading from a...
how to export source data to excel file in python?
how to export source data to excel file in python?
Below is my source code for file merging. when i run the code my merged file...
Below is my source code for file merging. when i run the code my merged file is blank and it never shows merging complete prompt. i dont see any errors or why my code would be causing this. i saved both files with male names and female names in the same location my source code is in as a rtf #include #include #include using namespace std; int main() { ifstream inFile1; ifstream inFile2; ofstream outFile1; int mClientNumber, fClientNumber; string mClientName;...
Consider the following structure definitions and fill in the blanks for the enqueue function:
Consider the following structure definitions and fill in the blanks for the enqueue function:    typedef struct queue queue_t;     typedef struct node node_t;    struct node {         node_t *next;          void *val; };    struct queue {         node_t *head;         node_t *tail;};/* add val at the tail of the queue q */void enqueue (queue_t *q, void *val) {     if (q->head == NULL) {        _______________ = _______________ = malloc(sizeof(_______________));        _______________ = NULL;     } else {        _______________ = _______________ = malloc(sizeof(_______________));        _______________ = NULL;     }    q->tail->val = val; }
Write a parameterized function that takes in a file name as a parameter, reads the file,...
Write a parameterized function that takes in a file name as a parameter, reads the file, calculates the factorial of each number, and displays a formatted output as follows: Factorial of 10 = 3628800 Factorial of 5 = 120
The following code searches a text file for definitions and prints them out. I want to...
The following code searches a text file for definitions and prints them out. I want to create a function that can be called for the given vector and map. The function will either print the results in reverse order, print results without duplicate definitions, or do both. What is the best approach to make the code less repetitive. if (find(keyWords.begin(), keyWords.end(), v[0]) != keyWords.end()) {                        for (auto map_iter = mymap.cbegin(); map_iter != mymap.cend(); ++map_iter)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT