Please write a program to satisfy the problem and make sure it compiles. You are
ID: 3807020 • Letter: P
Question
Please write a program to satisfy the problem and make sure it compiles. You are provided with table.h and main-table.cpp. Create table.cpp.
Here is the header file:
Here is the main.cpp file:
The Problem We are going to work on making our own classes with private data members and accessors. We are going to build a Table class, a 2D vector class. Table class Way back in Week 7 we discussed the idea of a 2D vector (example 10.5). We want to see if we can incorporate those ideas in a Table class. The header for the Table class has the following private elements private 2D vector of long vector vector long>> table how wide is t how many columns) long width how high is t how many rows) long height width for setw for even columns long field width The methods are as follows Table (long width. long height long field w 3, ong val-0) constructor. Makes a Table that is square shaped, width by height. Each element is set to val, which defaults to 0. The field w is to set the width of field when printing each element (default 3). Remember that is a vector vector long and that what you can push back onto table table is a vector long> which constitutes a row of table void fill random (long lo, long hi, unsigned. int seed 0) method to set every t element to a random number of long between lo and hi inclusive. Seed sets the random number seed, defaults to 0 bool set value (unsigned int row num, unsigned. int col num, long val) a method to set a particular element, indicated by row num and co num, to the provided val o If row num and col num are indicies that exist in t sets that element to val and returns true o otherwise does not set the element and returns false long get value (unsigned int row num unsigned. int col num) const Method to provide the value at row num col num of t if those two indicies exist. o if the two indicies are valid, return the table element o if not, since we are practicing exceptions let's throw a doma in error ostream& operatorExplanation / Answer
Here is the C++ code for you:
#include <iostream>
#include <iomanip>
#include "Table.h"
using namespace std;
// table will be width x height, default val is 0
class IllegalTableBounds : public exception{
public:
const char *what() const throw()
{
return "illegal table bounds ";
}
};
Table::Table(long width, long height, long field_w, long val)
{
width_ = width;
height_ = height;
field_width_ = field_w;
for(int i = 0; i < height; i++)
{
vector<long> temp;
for(int j = 0; j < width; j++)
temp.push_back(val);
table_.push_back(temp);
}
}
// range from lo to hi, seed has default
void Table::fill_random(long lo, long hi, unsigned int seed)
{
srand(seed);
for(int i = 0; i < height_; i++)
{
for(int j = 0; j < width_; j++)
table_[i][j] = rand() % (hi - lo + 1) + lo;
}
}
bool Table::set_value(unsigned int row_num, unsigned int col_num, long val)
{
if(row_num >= height_)
return false;
if(col_num >= width_)
return false;
table_[row_num][col_num] = val;
return true;
}
long Table::get_value (unsigned int row_num, unsigned int col_num) const
{
try
{
if(row_num < height_ && col_num < width_)
return table_[row_num][col_num];
IllegalTableBounds il;
throw il;
}
catch(exception &e)
{
cout << "illegal table bounds: " << row_num << " " << col_num;
return NULL;
}
}
void print_table(ostream&)
{
}
ostream& operator<<(ostream& out, Table t)
{
for(int i = 0; i < t.height_; i++)
{
out << setw(t.field_width_) << t.table_[i][0];
for(int j = 1; j < t.width_; j++)
out << ", " << setw(t.field_width_) << t.table_[i][j];
out << endl;
}
return out;
}