/* This file contains the declaration of class Table.

------------------------------------------------------------------*/

#ifndef TABLE
#define TABLE

#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>

#include "Boolean.h"
#include "Strings.h"

typedef double TableElement;              // array element type

class Table
{
public:

   enum { MaxRows = 10, MaxCols = 10 };  // bounds on the 2 dimensions

protected:
/********** data members **********/

   unsigned
      Rows_,                           // the number of rows in use
      Columns_;                        // the number of columns in use

   TableElement
      Grid[MaxRows][MaxCols];          // the 2-D array member


public:
   /***** Constructor *****/
   /*-----Class-----
      Initializes a declaration of a Table object.

      Precondition:  A Table object has been declared.
      Receive:       RowVal (optional), the number of rows in the
                        Table
                     ColVal (optional), the number of columns in the
                        Table
                     InitValue  (default value zero), to initialize
                        the values of the Table's elements
      Postcondition: The declared Table object containing this
                        function has its Rows_ member initialized to
                        RowVal_,and its Columns_ member initialized
                        to ColVal.
   ----------------------------------------------------------------*/

   Table(unsigned RowVal = 0, unsigned ColVal = 0,
            TableElement InitialValue = 0);


   /***** Operations *****/
   /***---Input---***/
   /*-----File - Version 1 -----
      Fills a Table object with data from a file, assuming
         that the Table has been constructed with the number of
         rows and columns for the Table in the file.

      Receive:      FileName, a Strings object containing the name
                       of an input file
      Input (file): A table of values from the input file
      Return:       The Table object containing this function, its
                       Grid member containing the input values
                    True if operation successful, false otherwise
   ----------------------------------------------------------------*/

   Boolean Fill( const Strings& FileName);


   /*-----File - Version 2 -----
      Fills a Table object with data from a file FileName,
         assuming that the first two values in the file give
         the number of rows and columns of the Table.

      Receive:      FileName, a Strings object containing the name of
                       an input file
      Input (File): The number of rows and columns and a table
                       of values
      Return:       False, if the file could not be opened; otherwise,
                       true and the Table object containing this
                       function, its Grid member containing the
                       input values
   ----------------------------------------------------------------*/

   Boolean Load(const Strings& FileName);


   /***--- Output ---***/
   /*----- File -----
      Writes a Table object to a file FileName,
         assuming that the first two values in the file give
         the number of rows and columns of the Table.

      Receive:      FileName, a Strings object containing the name of
                       an output file
      Output(File): The number of rows and columns and a table
                       of values
      Return:       False, if the file could not be opened; otherwise,
                       true and the file filled with this Table object
   ----------------------------------------------------------------*/
   Boolean Write(const Strings& FileName);


   /***--- Overloaded I/O Operations ---***/

   /*-------------------------------------------------------------------
      This function overloads the output operator for a Table object.

      Receive: Out, an ostream (reference) object
               Tab, a (const reference) Table object
      Return:  Out, containing the data from Tab
   ------------------------------------------------------------------*/
   friend ostream& operator<<(ostream& Out, const Table& Tab);

   /*-------------------------------------------------------------------
      This function overloads the input operator for a Table object.

      Receive: In, an istream (reference) object
               Tab, a (reference to a) Table object
      Return:  Tab, containing the data from In
   ------------------------------------------------------------------*/
   friend istream& operator>>(istream& In, Table& Tab);


   /*-------------------------------------------------------------------
      This function overloads the assignment operator for a Table object.

      Receive: Tab, a (const reference) Table object
      Return:  The Table containing this function,
                  its data members containing copies of those of Tab
   -------------------------------------------------------------------*/
   Table& operator=(const Table& Tab);


   /*----- Data member extractors -----*/

   unsigned Rows(void) const
      { return Rows_;}
   unsigned Columns(void) const
      { return Columns_;}


   /*-------------------------------------------------------------------
      This function sums the elements of a given row in a Table object.

      Receive:  Integer row, the row number
      Return:   The sum of the elements in the row-th row of the Table
                   object containing this function
   -------------------------------------------------------------------*/
   TableElement RowSum(unsigned row) const;

   /*-------------------------------------------------------------------
      This function sums the elements of a given column in a Table object.

      Receive:  Integer col, the column number
      Return:   The sum of the elements in the col-th column of the Table
                   object containing this function
   -------------------------------------------------------------------*/
   TableElement ColumnSum(unsigned col) const;


   /*-------------------------------------------------------------------
      This function accesses the element at a given row & column in
        a Table.

      Receive: row, a row index
               col, a column index
      Return:  The element at Grid[row][col]
   ------------------------------------------------------------------*/
   TableElement GetElem(unsigned row, unsigned col) const;

   /*-------------------------------------------------------------------
      This function accesses the element at a given row & column in
        a Table.

      Receive: row, a row index
               col, a column index
               Elem, the element to put at Grid[row][col]
      Return:  The element at Grid[row][col]
   ------------------------------------------------------------------*/
   TableElement SetElem(unsigned row, unsigned col, TableElement Elem);

};
#endif
