Click here to Skip to main content
16,021,580 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: I can't compile and put it as a variable and matrix Pin
Member 149848945-Nov-20 11:57
Member 149848945-Nov-20 11:57 
GeneralRe: I can't compile and put it as a variable and matrix Pin
jeron15-Nov-20 12:29
jeron15-Nov-20 12:29 
GeneralRe: I can't compile and put it as a variable and matrix Pin
Member 149848945-Nov-20 12:47
Member 149848945-Nov-20 12:47 
GeneralRe: I can't compile and put it as a variable and matrix Pin
jeron15-Nov-20 14:21
jeron15-Nov-20 14:21 
GeneralRe: I can't compile and put it as a variable and matrix Pin
jsc4226-Nov-20 5:55
jsc4226-Nov-20 5:55 
GeneralRe: I can't compile and put it as a variable and matrix Pin
David Crow9-Nov-20 2:52
David Crow9-Nov-20 2:52 
GeneralRe: I can't compile and put it as a variable and matrix Pin
jeron19-Nov-20 4:05
jeron19-Nov-20 4:05 
Questioni need your help Pin
Member 149820522-Nov-20 11:09
Member 149820522-Nov-20 11:09 
I have a project and this is what the project is about :
Implement a template library handling matrices.
the library must offer operation like:
•	submatrix: return a matrix that contains a contiguous subrange of rows and columns of the original matrix
•	transpose: return a matrix with rows and columns inverted
The matrices returned by such operators must share the data with the original matrix, but direct copy of the matrices and vectors must result in a deep copy (sharing a matrix can be obtained by taking a submatrix extending the whole row and column ranges).
Create iterators for the matrix traversing the whole data in any order.
Provide matrix addition and (row-by-column) multiplication as overloaded operators



so i did everything and i am stuck in how to implment operator overloaded for multiplication and addition, i mean should i creat a class for this operation or should i implment them in the main and how to do this.  





this is the matrix interface: <pre>#include <iostream>
#include <vector>
#include <memory>

using namespace std;
// creating matrux interface
template < typename T>
class matrix_interface {

public:
	// virtual keyword  allows derived classes to replace the implementation provided by the base class
    virtual int getCol() const = 0;
    virtual int getRow() const = 0;
    virtual T get(int row, int col) const = 0;
    virtual T& get_ref(int row, int col) = 0;
    virtual ~matrix_interface() {};
 
};



//Base

template < typename T>
class standard_matrix : public matrix_interface <T>{
  
 private:

    int row,col;
  
    vector <T> data;

    
public:

    int getRow() const override {
        return row;
    }

    int getCol() const override {
        return col;
    }

    standard_matrix(const vector<T> &data, int row, int col) : row(row), col(col), data(data) {
        if (data.size() < row*col)
            throw ("the vector is invalid");
        if (row < 1 || col < 1 ){
            throw ("the dimensions are invalid");
        }
    }

  

	 T &getRef(int r, int c) override {
        if (r < 1 || r > row) {
            throw "Row is off limit";
        } else if (c < 1 || c > col) {
            throw "Column is off limits";
        } else{
            T& t= data.at(col * (r - 1) + c - 1);
            return t;
        }
    }

    T get(int r, int c) const override {
        if (r < 1 || r > row) {
            throw "Row is off limit";
        } else if (c < 1 || c > col) {
            throw "Column is off limit";
        } else
            return data[col * (r - 1) + c - 1];
    }

   

  
};

// Matrix transpose

template <typename T>
class transposed_matrix : public matrix_interface <T>{

   private:

    shared_ptr< matrix_interface<T> > mat_ptr;


public:

    transposed_matrix (shared_ptr < matrix_interface <T> > x):mat_ptr(x){
    }

    int getRow() const override {
        return mat_ptr->getCol();
    }

    int getCol() const override {
        return mat_ptr->getRow();
    }

    T get(int row, int col) const override {
        if (row < 1 || row > getRow()) {
            throw "Row is off limit";
        } else if (col < 1 || col > getCol()) {
            throw "Col is off limit";
        } else
            return mat_ptr->get(col, row);
    }

    T &getRef(int row, int col) override {
        if (row < 1 || row > getRow()) {
            throw "Row is off limit";
        } else if (col < 1 || col > getCol()) {
            throw "Col is off limit";
        } else
            return mat_ptr->getRef(col, row);
    }

 
};


// submatrix

template <typename T>
class submatrix_interface : public matrix_interface <T>{
 
  private:

    shared_ptr< matrix_interface<T> > mat_ptr;

    int rowSet, colSet, row, col;
  

    
public:
        
    submatrix_interface(shared_ptr < matrix_interface <T> > x,
		int row_start, int col_start, int row_end, int col_end)
		:mat_ptr(x){
       if (row_start < 1 || col_start < 1 || row_end > mat_ptr->getRow() || col_end > mat_ptr->getCol() 
		   || row_start > row_end || col_start > col_end)
	   {
           throw "the input of the submatrix entered is not valid.";
       }
	   else {
           rowSet = row_start - 1;
           colSet = col_start - 1;
           row = row_end - row_start + 1;
           col = col_end - col_start + 1;
       }
    }

    int getRow() const override {
        return row;
    }

    int getCol() const override {
        return col;
    }

    T get(int row, int col) const override {
        if (row < 1 || row > getRow()) {
            throw "Row is off limit";
        } else if (col < 1 || col > getCol()) {
            throw "Column is off limit";
        } else {
            return mat_ptr->get(row + rowSet, col + colSet);
        }
    }

    T &getRef(int row, int col) override {
        if (row < 1 || row > getRow()) {
            throw "Row is off limit";
        } else if (col < 1 || col > getCol()) {
            throw "Column is off limit";
        } else {
            return mat_ptr->getRef(row + rowSet, col + colSet);
        }
    }
};










this is the matrix header:
<pre>#include "iterator.h"
#include<iostream>
using namespace std;


template < typename Type>
class matrix_base;

template < typename Type >
class matrix {



protected:

    shared_ptr < matrix_interface <Type> > matrixIntPtr;


public:
	
    matrix() {}


    matrix (const matrix & m){
        vector < Type > v;
        for (auto i = m.begin(); i != m.end() ; i++) {

            v.push_back(*i);// this function add or insert an element at the end of the vector 
        }
        matrixIntPtr = make_shared <standard_matrix <Type> > (v,m.row(),m.col()); // make_shared Allocates and constructs an object of type T passing args to its constructor, and returns an object of type shared_ptr<T> that owns and stores a pointer to it (with a use count of 1).

    }

    matrix(const vector<Type> &data, int r, int c) {
        matrixIntPtr = make_shared <standard_matrix <Type> > (data,r,c);
    }

	
    Type row() const{
        return matrixIntPtr->row();
    }

    Type col() const{
        return matrixIntPtr->col();
    }

    Type operator ()(int r, int c) const{
        return matrixIntPtr->get(r,c);
    }

    Type& operator ()(int r, int c) {
        return matrixIntPtr->getRef(r,c);
    }


		
    row_iter <Type> begin() const{
        return row_iter <Type> (matrixIntPtr, 1, 1);
    }

	column_iter <Type> col_begin() const{
        return column_iter <Type> (matrixIntPtr, 1, 1);
    }

    row_iter <Type> end() const{
        return row_iter <Type> (matrixIntPtr, matrixIntPtr->row()+1 , 1);
    }

    column_iter <Type> col_end() const{
        return column_iter <Type> (matrixIntPtr, 1 , matrixIntPtr->col()+1);
    }

	

	// submatrix
    matrix_base <Type> submatrix(int first_row, int first_col, int last_row, int last_col) const
	
	{
        return matrix_base <Type> (make_shared <submatrix_interface <Type> >(matrixIntPtr, first_row, first_col, last_row, last_col));
    }
	
	
	//transpose
    matrix_base <Type> transpose() const {
        return matrix_base <Type> (make_shared <transposed_matrix <Type> >(matrixIntPtr));
    }










and this is the iterators header:
<pre>#include "matrix_interface.h"



template < typename T>
class column_iter{


private:

    shared_ptr < matrix_interface <T> > sharedPtr;
    unsigned row, col;
   
    
public:

    column_iter(shared_ptr<matrix_interface<T> > ptr, unsigned row, unsigned col)
            : sharedPtr(ptr) , row(row) , col(col) {}

    column_iter& operator ++(){
        row++;
        if (row == sharedPtr->col() + 1) {
            row = 1;
            col++;
        }
        return *this;
    }

    column_iter& operator ++(int){
        column_iter tmp(*this);
        operator++();
        return tmp;
    }

    const T& operator *(){
        return sharedPtr->getRef(row,col);
    }

    bool operator == (const column_iter& x){
        return col == x.col && row == x.row;
    }

    bool operator != (const column_iter& x){
        return ! operator == (x);
    }
};

template < typename T>

class row_iter{

private:

    shared_ptr < matrix_interface <T> > sharedPtr;
    unsigned row;
    unsigned col;

public:

    row_iter(shared_ptr<matrix_interface<T> > ptr, unsigned row, unsigned col)
            : sharedPtr(ptr) , row(row) , col(col) {}

    row_iter& operator ++(){
        col++;
        if (col == sharedPtr->col() + 1) {
            col = 1;
            row++;
        }
        return *this;
    }

    row_iter& operator ++(int){
        row_iter tmp(*this);
        operator++();
        return tmp;
    }

    const T& operator *(){
        return sharedPtr->getRef(row,col);
    }

    bool operator == (const row_iter& x){
        return col == x.col && row == x.row;
    }

    bool operator != (const row_iter& x){
        return ! operator == (x);
    }


};

































AnswerRe: i need your help Pin
Victor Nijegorodov2-Nov-20 20:36
Victor Nijegorodov2-Nov-20 20:36 
QuestionDifference between current system time and time from user input in c Pin
Member 1497910831-Oct-20 6:00
Member 1497910831-Oct-20 6:00 
AnswerRe: Difference between current system time and time from user input in c Pin
Mircea Neacsu31-Oct-20 6:59
Mircea Neacsu31-Oct-20 6:59 
GeneralRe: Difference between current system time and time from user input in c Pin
Richard Andrew x6431-Oct-20 8:03
professionalRichard Andrew x6431-Oct-20 8:03 
GeneralRe: Difference between current system time and time from user input in c Pin
Mircea Neacsu31-Oct-20 8:14
Mircea Neacsu31-Oct-20 8:14 
GeneralRe: Difference between current system time and time from user input in c Pin
Richard MacCutchan31-Oct-20 9:38
mveRichard MacCutchan31-Oct-20 9:38 
GeneralRe: Difference between current system time and time from user input in c Pin
Richard Andrew x6431-Oct-20 10:28
professionalRichard Andrew x6431-Oct-20 10:28 
GeneralRe: Difference between current system time and time from user input in c Pin
Richard MacCutchan31-Oct-20 21:22
mveRichard MacCutchan31-Oct-20 21:22 
GeneralRe: Difference between current system time and time from user input in c Pin
Richard Andrew x641-Nov-20 9:58
professionalRichard Andrew x641-Nov-20 9:58 
GeneralRe: Difference between current system time and time from user input in c Pin
Richard MacCutchan1-Nov-20 21:58
mveRichard MacCutchan1-Nov-20 21:58 
AnswerRe: Difference between current system time and time from user input in c Pin
Daniel Pfeffer31-Oct-20 11:24
professionalDaniel Pfeffer31-Oct-20 11:24 
QuestionSearching and displaying a record in Array of Structure in c Pin
Member 1497910829-Oct-20 16:45
Member 1497910829-Oct-20 16:45 
AnswerRe: Searching and displaying a record in Array of Structure in c Pin
CPallini29-Oct-20 21:27
mveCPallini29-Oct-20 21:27 
QuestionHow to convert my MDI Application to open each window as a separate instance of the application Pin
manoharbalu23-Oct-20 0:09
manoharbalu23-Oct-20 0:09 
AnswerRe: How to convert my MDI Application to open each window as a separate instance of the application Pin
trønderen23-Oct-20 0:34
trønderen23-Oct-20 0:34 
GeneralRe: How to convert my MDI Application to open each window as a separate instance of the application Pin
manoharbalu23-Oct-20 0:49
manoharbalu23-Oct-20 0:49 
GeneralRe: How to convert my MDI Application to open each window as a separate instance of the application Pin
trønderen23-Oct-20 8:04
trønderen23-Oct-20 8:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.