Thread: Arbitrarily large vector class

  1. #1
    Registered User
    Join Date
    Dec 2022
    Posts
    1

    Arbitrarily large vector class

    In my homework they ask for any large vector in 2 dimensions. I need to implement multiplication operation.
    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
    
    class TDVect {
    private:
      unsigned int row;
      unsigned int column;
    
      double TDvect[row][column];
    
    public:
       TDVect(unsigned int A, unsigned int B) {
        row = A;
        column = B;
      } 
      unsigned int getLength() {  //to do: parameterize, return number of row or columns
        return row;
      }
      unsigned int getWidth() {
        return column;
      }
      double getElement(unsigned int i, unsigned int j) {
        return TDVect[i][j];
      }
      void setElement(unsigned int i, unsigned int j, double x) {
        TDVect[i][j] = x;
      }
    };
    
    class V:public TDVect {
      V():TDVect(1, 2) {
    }};
    
    class M:public TDVect {
      M():TDVect(2, 2) {
    }};
    
    V multiplication(V F, M G)
    {                               //to do: row times column
      V vmid();
      vmid.setElement(0, 0, F[0][0] * G[0][0] + F[0][1] * G[1][0]);
      vmid.setElement(0, 1, F[0][0] * G[0][1] + F[0][1] * G[1][1]);
      return vmid;
    }
    
    int main()
    {
      TDVect vectobjA(-1, +9);      //expected error; not given
      cout << vectobjA.getLength() << " ";
      return 0;
    }
    I'm getting quite a few errors in VScode :
    E0245,EO254,E0040,E0028.
    I guess the errors are related to each other but I dont understand them.
    We did't have STD::Vector in the lectures.
    Last edited by Salem; 12-19-2022 at 12:25 PM. Reason: Removed crayola

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Code:
      unsigned int row;
      unsigned int column;
     
      double TDvect[row][column];
    The problem is, you don't know row and column until runtime.


    Code:
      unsigned int row;
      unsigned int column;
     
      double **TDvect;
     
    public:
       TDVect(unsigned int A, unsigned int B) {
        row = A;
        column = B;
        TDvect = new double*[row];
        for (unsigned int r = 0 ; r < row ; r++ )
          TDvect[r] = new double[column];
      }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Your error codes don't mean much as they are not standardized across compilers. It is better to post the text of the errors.

    This is wrong:
    Code:
      unsigned int row;
      unsigned int column;
      double TDvect[row][column];
    public:
      TDVect(unsigned int A, unsigned int B) {
        row = A;
        column = B;
      }
    Array dimensions in C++ must be constants, so you can't use row and column like that (and in particular you can't set them at instantiation time and expect their values to automatically (re)size the arrays). Instead, you need to dynamically allocate the memory, so TDvect should be a pointer. It's usually easiest to allocate it as a one-dimensional array and simulate access as a two-d array. BTW, a two-d vector is usually called a matrix.
    Code:
    #include <iostream>
    using namespace std;
     
    class Matrix {
    private:
        unsigned rows_, cols_;
        double*  mat_;
    public:
        Matrix(unsigned r, unsigned c)           
            : rows_(r), cols_(c), mat_(new double[r * c])
        {}
        ~Matrix() {
            delete[] mat_;
        }
        unsigned rows() const {
            return rows_;
        }
        unsigned cols() const {
            return cols_;
        }
        double get(unsigned r, unsigned c) const {
            return mat_[r * cols_ + c];
        }
        void set(unsigned r, unsigned c, double x) {
            mat_[r * cols_ + c] = x;
        }
    };
     
    int main() {
        Matrix a(4, 6);
        cout << a.rows() << ", " << a.cols() << "\n";
        a.set(2, 3, 42);
        cout << a.get(2, 3) << "\n";
        return 0;
    }
    You could use an underlying std::vector instead of explicit dynamic allocation.
    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
     
    class Matrix {
    private:
        vector<double> mat_;
        unsigned cols_;
    public:
        Matrix(unsigned r, unsigned c)           
            : mat_(r * c), cols_(c)
        {}
        unsigned rows() const {
            return mat_.size() / cols_;
        }
        unsigned cols() const {
            return cols_;
        }
        double get(unsigned r, unsigned c) const {
            return mat_[r * cols_ + c];
        }
        void set(unsigned r, unsigned c, double x) {
            mat_[r * cols_ + c] = x;
        }
    };
     
    int main() {
        Matrix a(4, 6);
        cout << a.rows() << ", " << a.cols() << "\n";
        a.set(2, 3, 42);
        cout << a.get(2, 3) << "\n";
        return 0;
    }
    Last edited by john.c; 12-19-2022 at 03:37 PM.
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 17
    Last Post: 08-05-2018, 06:59 PM
  2. Reading an arbitrarily long line
    By deathslice in forum C Programming
    Replies: 32
    Last Post: 03-23-2016, 09:45 PM
  3. multiply arbitrarily large real numbers
    By Martin0027 in forum C Programming
    Replies: 7
    Last Post: 05-17-2011, 01:59 PM
  4. Making stack class using vector class
    By spank in forum C++ Programming
    Replies: 9
    Last Post: 08-10-2007, 03:50 AM
  5. Help locate an ideal large number class
    By simpleid in forum C++ Programming
    Replies: 2
    Last Post: 08-08-2007, 08:05 AM

Tags for this Thread