-4

UPDATE: Errors still occuring 11:30 AM PST Assignment prompt below: A6

The main focus of this assignment is chained hash and template functions and class. . The material from Ch1 ~ 12 of the textbook can help you tremendously. You can get a lot of good information about implementing this assignment from chapter 12.

You can add any number of paramours, functions and classes for this assignment and use any advance skills you learned. But there need to have at least two .h files, two template files and one .cpp file to generate the expecting result.

In sections 12.2 ~ 12.4 discuss the hashing concept and implementation. This assignment we will implement a chaining (data structure of a hash table).

Some information Files that you need to know or write:

  1. table2.h: A header file for this assignment’s Table class. This is a template class. The template parameter, called RecordType, may be instantiated as any struct or class that contains an integer member variable called key. The following are definition of a Table class, some useful functions and variables. a. Table This class is a container template class for a Table of records. The template parameter, RecordType, is the data type of the records in the Table. It may any type with a default constructor, a copy constructor,an assignment operator, and an integer member variable called key. In the Table template class assignments and the copy constructor may be used with Table objects. If there is insufficient dynamic memory, then the copy constructor, insert, the assignment operator can add new memory.

b. CONSTRUCTOR for the Table template class: Table( ) Postcondition: The Table has been initialized as an empty Table.

c. MODIFICATION MEMBER FUNCTIONS for the Table class:

  1. void insert(const RecordType& entry) Precondition: entry>= 0. Also if entry is not already a key in the table, then the Table has space for another record. (i.e., size( ) < CAPACITY). Postcondition: If the table already had a record with a key equal to entry, then that record is replaced by entry. Otherwise, the entry will be added as a new record of the Table.

  2. void remove(int key) Postcondition: If a record was in the Table with the specified key, then that record has been removed; otherwise the table is unchanged.

  3. void print(int index)const; Postcondition: If the Table with the index has data, then print the index and data in a chining format

d. CONSTANT MEMBER FUNCTIONS for the Table class:

  1. bool is_present(const RecordType & target) const Postcondition: The return value is true if there is a record in the Table with the specified key. Otherwise, the return value is false.

  2. void find(int key, bool& found, RecordType& result) const Postcondition: If a record is in the Table with the specified key, then found is true and result is set to a copy of the record with that key. Otherwise found is false and the result contains garbage.

  3. size_t size( ) const Postcondition: Return value is the total number of records in the Table. e. Private data members:

  4.      Node<RecordType> *data[TABLE_SIZE];
    
  5.      size_t total_records;
     // HELPER FUNCTION
    
  6.      size_t hash(int key) const;
    
  7.      Node<RecordType>* find_node(Node<RecordType> *&cursor,Node<RecordType> *&precursor, int key) const;
    
  8. table2.template: is the implementation file for the table2.h.

  9. link2.h is a header file to provide a toolkit of ten template functions for manipulating linked lists. Each node of the list contains a piece of data and a pointer to the next node (provided).

  10. link2.template is the implementation file for the linke.h (provided).

  11. CISP430V4A6.cpp is the driver for this assignment and there are several steps you need to do to test the program. a. Instantiate two Table objects and each with 10 fields. b. Display the two Table objects’ information. c. Use random number generator to generate 70 numbers each in between 0~200 for the Table objects. d. Display the two Table objects’ information. e. Removes all the data in first object. f. Display the two Table objects’ information. g. Using = to assign 2nd object to the first one. h. Display the two Table objects’ information.

Errors recieved:

Severity Code Description Project File Line Suppression State Error C2664 'void CISP430_A6::Table::insert(const CISP430_A6::Table::RecordType &)': cannot convert argument 1 from 'size_t' to 'const CISP430_A6::Table::RecordType &' CISP430_A6v4 C:\Users\kater\OneDrive\Desktop\CISP430_A6v4\CISP430_A6v4\table2driver.cpp 25

Driver file:

#include <iostream>
#include <cstdlib> // for rand and srand
#include <ctime> // for time
#include "table2.h"
#include "link2.h"

using namespace std;
using namespace CISP430_A6;
int main() {
    srand(time(nullptr)); // seed the random number generator with the current time
    Table<size_t> table1;
    //Table<int> table2;
  
    cout << "Initial state of table1:\n";
    for (int i = 0; i < Table<int>::TABLE_SIZE; ++i) {
        table1.print(i); // this works
    }
    /*cout << "Initial state of table2:\n";
    for (size_t i = 0; i < Table<int>::TABLE_SIZE; ++i) {
        table2.print(i);
    }*/
    //cout << "Generating random numbers...\n";
    for (int i = 0; i < 70; ++i) {
        size_t number = rand() % 201; // generate a random number between 0 and 200
        table1.insert(number); // This will not work
        //table2.insert(number);
    }
    cout << "State of table1 after inserting 70 random numbers:\n";
    for (size_t i = 0; i < Table<int>::TABLE_SIZE; ++i) {
        table1.print(i);
    }
  
    return 0;
}

Header File

#ifndef TABLE2_H
#define TABLE2_H
#include <cstdlib> // Provides size_t
#include <list>
#include "link2.h"
using namespace std;

namespace CISP430_A6
{
    
    template <class RecordType> class Table
    {
    public:
        
        //RecordType entry;
        // MEMBER CONSTANT
        static const size_t TABLE_SIZE = 10;
        struct RecordType{ int key = 0; } entry;
        // CONSTRUCTOR AND DESTRUCTOR
        Table();
        Table(const Table& source);
        ~Table();
        // MODIFICATION MEMBER FUNCTIONS
        void insert(const RecordType& entry);
        void remove(int key);
        void print(int index);
        void operator =(const Table& source);
        // CONSTANT MEMBER FUNCTIONS
        void find(int key, bool& found, RecordType& result) const;
        bool is_present(const RecordType& target) const;
        size_t size() const { return total_records; };
    private:
        Node<RecordType>* data[TABLE_SIZE];
        size_t total_records;
        // HELPER MEMBER FUNCTION
        size_t hash(int entry) const;
        Node<RecordType>* find_node(Node<RecordType>*& cursor, Node<RecordType>*& precursor, int  key) const;

    };
}
#include "table2.template"
#endif

Insert function with other things:

        template <class RecordType>
    Table<RecordType>::Table()
    {
        //Class constructor: Constructs an empty table.

        for (size_t i = 0; i < TABLE_SIZE; ++i)
        {
            data[i] = NULL;
        }

        total_records = 0;
    }

    template <class RecordType>
    Table<RecordType>::Table(const Table& source)
    {
        // Copy constructor: Creates a copy of the con
        for (int i = 0; i < TABLE_SIZE; ++i)
        {
            Node<RecordType>* tail_ptr;
            list_copy(source.data[i], data[i], tail_ptr);
        }

        total_records = source.total_records;
    }

    template <class RecordType>
    Table<RecordType>::~Table()
    {
        // Table destructor.
        for (int i = 0; i < TABLE_SIZE; ++i)
        {
            if (data[i] != NULL)
            {
                list_clear(data[i]);
            }
        }

        total_records = 0;
    }
    template<class RecordType>
    void Table<RecordType>::operator=(const Table& source)
    {
        if (this != &source)
        {
            for (int i = 0; i < TABLE_SIZE; ++i)
            {
                Node<RecordType>* tail_ptr;
                list_copy(source.data[i], data[i], tail_ptr);
            }
            total_records = source.total_records;
        }
    }
    template<class RecordType>
    void Table<RecordType>::print(int index)
    {
        Node<RecordType>* curNode = data[index];
        cout << "[( " << index << " )]----> ";
        if (data[index] != NULL)
        {
            while (curNode != NULL)
            {
                cout << "[" << data[index] << "]-->";
            }
        }
        else if ((data[index] == NULL) || (curNode == NULL))
        {
            cout << " NULL ";
        }
        cout << endl;
    }
        template<class RecordType>
    void Table<RecordType>::insert(const RecordType& entry)
    {   // Precondition: entry >= 0 and there is space for another record 
        assert(entry.key >= 0 && total_records < TABLE_SIZE); 
        // Calculate the index in the array where the entry should be inserted 
        size_t index = hash(entry.key); 
        // Search for the entry in the linked list at the index 
        Node<RecordType>* cursor = list_search(data[index], entry.key); 
        // If the entry is found, replace it 
        if (cursor != nullptr) 
        { 
            cursor->data = entry; 
        } // Otherwise, insert the entry at the beginning of the list 
        else 
        { 
            list_head_insert(data[index], entry); 
            total_records++; 
        }
    }

This is for a chained hashing assignment and the functions called in insert are prewritten and part of a linked list toolkit.

I am expecting print to print the newly inserted values from the driver file.

blindfox
  • 11
  • 2
  • Please note that [`using namespace std;` is a bad habit](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) in source files. Doing it in header files is twice as bad. – Some programmer dude Apr 16 '23 at 17:53
  • How would I declare RecordType in main to pass it in? – blindfox Apr 16 '23 at 17:56
  • The `struct RecordType` is shadowing the template parameter `RecordType`. It is not obvious what the point of that struct is. – molbdnilo Apr 16 '23 at 17:59
  • @molbdnilo OK missed that. – john Apr 16 '23 at 17:59
  • Did you write all this code yourself? I would think you would know about `insert` and what to pass if you had. – lakeweb Apr 16 '23 at 18:00
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – n. m. could be an AI Apr 16 '23 at 18:01
  • 1
    @blindfox The serious confusion here is that you have two `RecordType`, here `template class Table` and here `struct RecordType{ int key = 0; } entry;`. Undoubtedly you only want one, but I'm not sure which. – john Apr 16 '23 at 18:01
  • My teacher gave me an assignment with various function signatures that we had to write the code for and pointed to examples in the book which did not work. – blindfox Apr 16 '23 at 18:01
  • @blindfox So did he give you this header file? – john Apr 16 '23 at 18:02
  • @john the assignment wants both to have the same name. This is why I'm lost. – blindfox Apr 16 '23 at 18:02
  • @blindfox You could have both with the same name but only is you moved `struct RecordType` outside of the class. Inside the class it makes no sense at all. – john Apr 16 '23 at 18:03
  • @john he said to basically go copy the one from the book and make a few additions via the assignment prompt – blindfox Apr 16 '23 at 18:03
  • 1
    @blindfox I'm not sure what is copied code, and what you have modified. So it's hard to know what to suggest. – john Apr 16 '23 at 18:05
  • @blindfox Anyway the basic error is that you have a struct inside a template class, where the struct name and the template parameter name are the same. That is just wrong. What the resolution is, is not at all clear to me, but that's what you have to fix one way or another. – john Apr 16 '23 at 18:08
  • @blindfox The phrase "make a few additions via the assignment prompt" is totally incomprehensible. – molbdnilo Apr 16 '23 at 18:08
  • Somebody (either your professor or yourself) apparently wants to stuff 70 numbers in a table that has a capacity of 10. Perhaps you need to double-check your assignment. – n. m. could be an AI Apr 16 '23 at 18:09
  • the issue is with code added by you to some code given to you. For us to understand you should post the code that was given to you and the code you added seperately – 463035818_is_not_an_ai Apr 16 '23 at 18:12
  • `struct RecordType{ int key = 0; } entry;` The data member declared here is never used, and the type does nothing except breaking the entire thing apart. There is no way in hell it can ever work. If the assignment says or implies that you must have this exact line no matter what, complain. If not, just remove it. It does not belong. – n. m. could be an AI Apr 16 '23 at 18:15
  • Added prompt to the post. – blindfox Apr 16 '23 at 18:28

1 Answers1

1

It's clear from the problem description what is required. Something like this

struct MyRecord
{
    int key = 0;
};

int main()
{
    Table<MyRecord> table;
    ...
}

So a type is declared outside of the template class (called MyRecord in the code above) and then that type is used as a parameter when the table is declared Table<MyRecord> table;

And also don't forget to remove struct RecordType { ... }; from class Table. That was clearly your addition based on a misunderstanding of how templates work.

There's nothing in the problem description that says you need to have two different RecordType definitions. The only mention of RecordType is as the template parameter in the Table class.

This is just like the situation with function parameters. A function parameter has some name, but that does not mean that you must call that function with a variable that has the same name. So with a template parameter, it has a name, but when you instantiate the template you can supply a type with a completely different name.

Unfortunately these changes do mean that you basically have to start again with this assignment.

john
  • 85,011
  • 4
  • 57
  • 81