Question

In: Computer Science

#include "IntVariableTable.h" #include "Tokens.h" #include <assert.h> #include <iostream> #include <iomanip> using std::cout; using std::endl; using std::left;...

#include "IntVariableTable.h"
#include "Tokens.h"
#include <assert.h>
#include <iostream>
#include <iomanip>


using std::cout;
using std::endl;
using std::left;
using std::right;
using std::setw;
using std::string;


// The IntVariableTable constructor dynamically allocates the fixed size array of integer variables.

IntVariableTable::IntVariableTable()
{
int_variable_table = new IntVariableEntry[MAX_INT_VARIABLES];
}


// The IntVariableTable destructor deallocates the integer variable array.

IntVariableTable::~IntVariableTable()
{
delete[] int_variable_table;
}


// Returns the number of variables added to the integer variable table.

int IntVariableTable::numVariables() const
{
return num_int_variables;
}

// Returns the index of the string token argument in the integer variable table,
// or -1 if the variable name is not found in the integer variable table.

int IntVariableTable::lookupVariable(const string& token) const
{
int index = -1;


// TODO: If the token is in the integer variable table then set index to the
// table position where it is found.

return index;
}


// Adds integer variable name and initial value string tokens to the integer variable
// table, and returns the index of the table entry used to store the variable.
// If the variable name is already present in the integer variable table, a message is
// generated and the table entry index is returned.
// If there is no available entry in the integer variable table, a message is generated
// and -1 is returned.

int IntVariableTable::defineVariable(const string& token1, const string& token2)
{
int index = lookupVariable(token1);

// TODO: If the token is not in the integer variable table then add it at the end
// of the table provided there is room.

return index;
}


// Returns the name string present in the integer variable table entry specified
// by the index argument.
// An assertion is triggered if the index argument is out of bounds.

string IntVariableTable::getName(int index) const
{
assert(validIndex(index));

return int_variable_table[index].name;
}


// Returns the numeric value present in the integer variable table entry specified
// by the index argument.
// An assertion is triggered if the index argument is out of bounds.

int IntVariableTable::getValue(int index) const
{
assert(validIndex(index));

return int_variable_table[index].value;
}


// Sets the numeric value in the integer variable table entry specified by the index argument.
// An assertion is triggered if the index argument is out of bounds.

void IntVariableTable::setValue(int index, int value)
{
assert(validIndex(index));

int_variable_table[index].value = value;
}


// Returns whether the argument is a valid integer variable table index.

bool IntVariableTable::validIndex(int index) const
{
return ((index >= 0) && (index < num_int_variables));
}


// Displays the contents of the integer variable table on the console.

void IntVariableTable::display() const
{
if (num_int_variables == 0) {
cout << endl << "The integer variable table is empty" << endl;
}
else {
cout << endl << "Integer variable table: [index | variable name | initial value]" << endl << endl;
for (int i = 0; i < num_int_variables; i++) {
cout << right << setw(8) << i << " "
<< left << setw(24) << addQuotes(int_variable_table[i].name)
<< right << setw(8) << int_variable_table[i].value << endl;
}
}
}

i need help on the two TODO parts.

here is what i had so far but did not work:

Returns the index of the string token argument in the integer variable table,
// or -1 if the variable name is not found in the integer variable table.

int IntVariableTable::lookupVariable(const string& token) const
{
int index = -1;
for (int i = 0; i < num_int_variables; i++) {
if (int_variable_table[i].name.compare(token)==0) {
index = i;
}
}
// TODO: If the token is in the integer variable table then set index to the
// table position where it is found.

return index;
}


// Adds integer variable name and initial value string tokens to the integer variable
// table, and returns the index of the table entry used to store the variable.
// If the variable name is already present in the integer variable table, a message is
// generated and the table entry index is returned.
// If there is no available entry in the integer variable table, a message is generated
// and -1 is returned.

int IntVariableTable::defineVariable(const string& token1, const string& token2)
{
int index = lookupVariable(token1);
if (index == -1) {
if (num_int_variables == MAX_INT_VARIABLES) {
cout << "no available entry available";
return -1;
}
int_variable_table[num_int_variables++].name = token1;
}
else {
cout << "entry already exists";
}

// TODO: If the token is not in the integer variable table then add it at the end
// of the table provided there is room.

return index;
}



Solutions

Expert Solution

In the above code sample, tokens.h is not provided and intVariableEntry definition is also not provided. Therefore, to get the code working, created my own intVariableTable.h which defines whatever is necessary. You will need to change / discard it depending on what is already defined in Tokens.h.

///////// intVariableTable.h /////////////////////////////////////////

#include <iostream>
using std::string;

class IntVariableEntry {
    public:
        string name;    
        int value;
};

class IntVariableTable {
    IntVariableEntry *int_variable_table;
    int num_int_variables;

    public:    
        IntVariableTable();
        ~IntVariableTable();
        int numVariables() const;
        int lookupVariable(const string& token) const;
        int defineVariable(const string& token1, const string& token2);
        string getName(int index) const;
        int getValue(int index) const;
        void setValue(int index, int value);
        bool validIndex(int index) const;
        void display() const;
};

/// IntVariableTable.cpp /////// code is mostly fine, added a missing initialization for num_int_variables ///////////

#define MAX_INT_VARIABLES 100
#include "IntVariableTable.h"
#include <assert.h>
#include <iostream>
#include <iomanip>


using std::cout;
using std::endl;
using std::left;
using std::right;
using std::setw;
using std::string;

// The IntVariableTable constructor dynamically allocates the fixed size array of integer variables.

IntVariableTable::IntVariableTable()
{
        int_variable_table = new IntVariableEntry[MAX_INT_VARIABLES];
        num_int_variables = 0;
}


// The IntVariableTable destructor deallocates the integer variable array.

IntVariableTable::~IntVariableTable()
{
        delete[] int_variable_table;
}


// Returns the number of variables added to the integer variable table.

int IntVariableTable::numVariables() const
{
        return num_int_variables;
}

// Returns the index of the string token argument in the integer variable table,
// or -1 if the variable name is not found in the integer variable table.

int IntVariableTable::lookupVariable(const string& token) const
{
        int index = -1;

    for (int i = 0; i < num_int_variables; i++) {
                if (int_variable_table[i].name.compare(token)==0) {
                        index = i;
                        break;
                }
        }
                
        return index;
}


// Adds integer variable name and initial value string tokens to the integer variable
// table, and returns the index of the table entry used to store the variable.
// If the variable name is already present in the integer variable table, a message is
// generated and the table entry index is returned.
// If there is no available entry in the integer variable table, a message is generated
// and -1 is returned.

int IntVariableTable::defineVariable(const string& token1, const string& token2)
{
        int index = lookupVariable(token1);
    if (index == -1) {
                if (num_int_variables == MAX_INT_VARIABLES) {
                        cout << "no available entry available" << endl;
                        return -1;
                }
                int_variable_table[num_int_variables].name = token1;
                int_variable_table[num_int_variables].value = 0;
                num_int_variables++;
        }
        else {
                cout << "entry already exists" << endl;
        }

        return index;
}


// Returns the name string present in the integer variable table entry specified
// by the index argument.
// An assertion is triggered if the index argument is out of bounds.

string IntVariableTable::getName(int index) const
{
        assert(validIndex(index));

        return int_variable_table[index].name;
}


// Returns the numeric value present in the integer variable table entry specified
// by the index argument.
// An assertion is triggered if the index argument is out of bounds.

int IntVariableTable::getValue(int index) const
{
        assert(validIndex(index));

        return int_variable_table[index].value;
}


// Sets the numeric value in the integer variable table entry specified by the index argument.
// An assertion is triggered if the index argument is out of bounds.

void IntVariableTable::setValue(int index, int value)
{
        assert(validIndex(index));

        int_variable_table[index].value = value;
}


// Returns whether the argument is a valid integer variable table index.

bool IntVariableTable::validIndex(int index) const
{
        return ((index >= 0) && (index < num_int_variables));
}


// Displays the contents of the integer variable table on the console.

void IntVariableTable::display() const
{
        if (num_int_variables == 0) {
                cout << endl << "The integer variable table is empty" << endl;
        }
        else {
                cout << endl << "Integer variable table: [index | variable name | initial value]" << endl << endl;
                for (int i = 0; i < num_int_variables; i++) {
                        cout << right << setw(8) << i << " "
                                << left << setw(24) << int_variable_table[i].name
                                << right << setw(8) << int_variable_table[i].value << endl;
                }
        }
}

/////// Main function to test: Please note: Not clear what the second string parameter in defineVariable is to be used for - no clarity in TODO comments. Have ignored it for now.

#include <iostream>
#include "IntVariableTable.h"

using namespace std;

int main()
{
    IntVariableTable it;
    cout << it.lookupVariable ("zonko") << endl;
    it.defineVariable ("first", "junk");
    it.defineVariable ("second", "junk");
    it.defineVariable ("third", "junk");
    it.defineVariable ("fourth", "junk");
    it.defineVariable ("fifth", "junk");
    cout << it.getName (1) << endl;
    it.setValue (1, 100);
    it.setValue (0, 10);
    it.setValue (2, 200);
    it.display ();
    
    
    return 0;
}

Sample output:

-1                                                                                                                              

second                                                                                                                          

                                                                                                                                

Integer variable table: [index | variable name | initial value]                                                                 

                                                                                                                                

       0 first                         10                                                                                       

       1 second                       100                                                                                       

       2 third                        200                                                                                       

       3 fourth                         0                                                                                       

       4 fifth                          0                                                                                       

                                                                                                                                

                                                                                                                                

...Program finished with exit code 0  


Related Solutions

#include <iostream> #include <string> #include <iomanip> #include <cstdlib> #include "Contact.h" using namespace std; class Contact {...
#include <iostream> #include <string> #include <iomanip> #include <cstdlib> #include "Contact.h" using namespace std; class Contact { public: Contact(string init_name = "", string init_phone = "000-000-0000"); void setName(string name); void setPhone(string phone); string getName()const; string getPhone()const; friend ostream& operator << (ostream& os, const Contact& c); friend bool operator == (const Contact& c1, const Contact& c2); friend bool operator != (const Contact& c1, const Contact& c2); private: string name, phone; }; Contact::Contact(string init_name, string init_phone) { name = init_name; phone = init_phone;...
#include <iostream> #include <iomanip> using namespace std; int main() {     int studentid, numberreverse[20], count =...
#include <iostream> #include <iomanip> using namespace std; int main() {     int studentid, numberreverse[20], count = 0, maximum = 0, minimum = 0;     cout << "Enter your student ID number: ";     cin >> studentid;     cout << "Student ID Number = " << studentid << endl;     while (studentid != 0)     {          numberreverse[count] = studentid % 10;          if (count == 0)          {              minimum = numberreverse[count];              maximum = minimum;          }          else...
What is the flowchart for this code. Thank You! #include<iostream> #include<iomanip> #include<string> #include<cmath> using namespace std;...
What is the flowchart for this code. Thank You! #include<iostream> #include<iomanip> #include<string> #include<cmath> using namespace std; float series(float r[], int n) {    float sum = 0;    int i;    for (i = 0; i < n; i++)        sum += r[i];    return sum; } float parallel(float r[], int n) {    float sum = 0;    int i;    for (i = 0; i < n; i++)        sum = sum + (1 / r[i]);...
#include <iostream> using namespace std; double print_instructions() { cout << "WELCOME TO BandN book stores" <<...
#include <iostream> using namespace std; double print_instructions() { cout << "WELCOME TO BandN book stores" << endl; cout << "Today we have a deal on e-books. Customers will receive a discount off their entire order.." << endl; cout << "The discount is 15% off your total order and cost per book is 8.99." << endl; cout << "Customers who buy 20 or more e-books will receive 20% off instead." << endl; cout << endl; return 0; } int no_of_books() {...
Can someone covert the code into C language #include<iostream> #include<iomanip> #include<ios> using namespace std; /******************************************************************************** Function...
Can someone covert the code into C language #include<iostream> #include<iomanip> #include<ios> using namespace std; /******************************************************************************** Function name: main Purpose:                   main function In parameters: b,r,i Out paramters: trun,error,total,value Version:                   1.0 Author: ********************************************************************************/ void main() {    int i;//declaring this variable to get value for quitting or calaculating series    do {//do while loop to calaculate series until user quits        cout << "Enter 1 to evaluate the series." << endl;       ...
Given: #include <iostream> using std::cout; template <typename T> struct Node { T data; Node *link;   ...
Given: #include <iostream> using std::cout; template <typename T> struct Node { T data; Node *link;       Node(T data=0, Node *p = nullptr) { //Note, this constructor combines both default and parameterized constructors. You may modify the contructor to your needs this->data = data;        link = p; } }; template <typename T> class linked_list { Node<T> *head,*current; public: //default constructor linked_list() { head = nullptr;//the head pointer current = nullptr;//acts as the tail of the list } //destructor...
C++ code Why my code is not compiling? :( #include <iostream> #include <iomanip> #include <string> using...
C++ code Why my code is not compiling? :( #include <iostream> #include <iomanip> #include <string> using namespace std; const int CWIDTH = 26; int main() {    int choice;    double convertFoC, converCtoF;    double starting, endvalue, incrementvalue;    const int CWIDTH = 13;    do {       cin >> choice;    switch (choice)    {        cin >> starting;    if (starting == 28){       cout << "Invalid range. Try again.";    }    while (!(cin >> starting)){       string  garbage;       cin.clear();       getline(cin, garbage);       cout << "Invalid data Type, must be a number....
--- TURN this Code into Java Language --- #include <iostream> #include <string> using namespace std; //...
--- TURN this Code into Java Language --- #include <iostream> #include <string> using namespace std; // constants const int FINAL_POSITION = 43; const int INITIAL_POSITION = -1; const int NUM_PLAYERS = 2; const string BLUE = "BLUE"; const string GREEN = "GREEN"; const string ORANGE = "ORANGE"; const string PURPLE = "PURPLE"; const string RED = "RED"; const string YELLOW = "YELLOW"; const string COLORS [] = {BLUE, GREEN, ORANGE, PURPLE, RED, YELLOW}; const int NUM_COLORS = 6; // names...
using only loops, no functions and no arrays. with the heading: #include <iostream> using namespace std;...
using only loops, no functions and no arrays. with the heading: #include <iostream> using namespace std; "Write a program that asks the user to enter an odd positive integer. The program reads a value (n) entered by the user and prints an n x n grid displaying a large letter X. The left half should be made up of pluses (+) and the right half should be made with the character "x" and the very center should be an asteric...
Using only loops, no functions and no arrays. with the heading: #include <iostream> using namespace std;...
Using only loops, no functions and no arrays. with the heading: #include <iostream> using namespace std; "Write a program that asks the user to enter an integer between 1 and 20. If the user enters an illegal number, the program repeatedly asks the user to enter the correct one. If the user has not entered a correct number after 10 attempts, the program chooses the number 10 as the user's number. The program prints the cube of the user's number.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT