I want to write a class that do operation on two matrices by 2D vector but i have some problems with the 2D vector , any one can help me?

Recommended Answers

All 7 Replies

and what are those problems?

It's kinda hard for us to figure out your problems since you didn't share them with us.

I want to let the user to input elements of the vector....
then i want to multiply two 2D vectors
i wrote a code but there is logic errors

PLZ.. write the code of entering two 2D vectors and multiply them ,then show results,
using class that take elements of the vector ,multiply and show result

Show the code and outline the errors. Unless you're willing to pay someone to do the work for you, in which case you should be in Jobs and Resumes forum.

it's the code , debug it and tell me what is the error

#include <iostream>
using namespace std;
#include <vector>
class matrix
{
protected:
    int r,c;                    //r is rows c is columns
    char op;                    // op is the operarion
    vector<vector<double> > elements;           //vector for matrix elements
public:
    matrix()                                    //constructor for initializing r,c,op and vector to 0
    {
         r='c';c='a'; op='b';
  for ( int i = 0; i < 5; i++ ) {
   elements.push_back ( vector<double>() );
    for ( int j = 0; j < 5; j++ )
        { 
    elements[i].push_back ( 0 );}
  }
    }
////////////////////////////////////////////////////////////

    void get_data()                          //get elements of the matrix from the user
    {
        char a;
        double n;
        cout<<"\n\nEnter type of the matrix in form r*c\n\n";
        cin>>r>>a>>c;
        cout<<"Enter the element of the matrix\n\n";
              for ( int i = 0; i < r; i++ ) {
   elements.push_back ( vector<double>() );
    for ( int j = 0; j < c; j++ )
    {

        cin>>n;
        elements[i].push_back (n );}
  }
    }
////////////////////////////////////////////////////////////

    void operation(matrix mat1,matrix mat2)                 //do operations on two matrices
    {

        cout<<"Enter the operation you want (+,-,*)\n\n";
        cin>>op;
        switch(op)                                           //if the operation +,-,*
        {
        case '+':
            if(mat1.r==mat2.r&&mat1.c==mat2.c)              //if the matrices have the same type it can sum
            {
                for(int i=0;i<mat1.r;i++){
                    for(int j=0;j<mat1.c;j++){
                        elements[i][j]=mat1.elements[i][j]+mat2.elements[i][j];}
                }
            }
            else
                cout<<"\nERROR You Enterd Tow Matrices Of Different Type\n\n";
            break;

            ////////////////////////////////////////////////////////////

        case'-':
            if(mat1.r==mat2.r&&mat1.c==mat2.c)         //if the matrices have the same type it can subtract
            {
                for(int i=0;i<mat1.r;i++){
                    for(int j=0;j<mat1.c;j++){
                        elements[i][j]=mat1.elements[i][j]-mat2.elements[i][j];}
                }
            }
            else
                cout<<"\nERROR You Enterd Tow Matrices Of Different Type\n\n";
            break;

            ////////////////////////////////////////////////////////////

        case'*':
            if(mat1.c==mat2.r)    //if number of first matrix columns equal number of second matrix rows
            {
                for ( int i = 0; i < mat1.r; i++ )           //initialize the vector of the result to 0
                {                  
                 elements.push_back ( vector<double>() );
                  for ( int j = 0; j < mat2.c; j++ ){

                      elements[i].push_back (0 );}}
                ////////////////////////////////////////////////////////////

                for(int i=0;i<mat1.r;i++)                //multiplying
                {
                    for(int j=0;j<mat2.c;j++)
                    {
                        for(int k=0;k<mat1.c;k++)
                            elements[i][j]=elements[i][j]+mat1.elements[i][k]+mat2.elements[k][j];
                    }
                }
  }
                             else
                    { cout<<"\n Input matrices can not be multiplied\n";
                 cout<<" The number of columns in first matrix must equal number of rows of the second matrix\n";}
            break;
        default:
            cout<<"\nYou Entered Error sign\n\n";
    }
            }
////////////////////////////////////////////////////////////

    void sow_data(matrix mat1,matrix mat2)               //show the result
    {
        if(op=='+'||'-')                                //if the operation is + or -
        {
            for(int i=0;i<mat1.r;i++){
                for(int j=0;j<mat1.c;j++)
                    cout<<elements[i][j]<<'   ';
                cout<<"\n\n";
            }
        }
        else if(op=='*')                                //if the operation is multiplying
        {
            for(int i=0;i<mat1.r;i++){
                for(int j=0;j<mat2.c;j++)
                    cout<<elements[i][j]<<'   ';
                cout<<"\n\n";
            }
        }
        else
            cout<<"\nERROR\n";
    }


};
int main()
{
    matrix mat,mat1,mat2;
    mat1.get_data();
    mat2.get_data();
    mat.operation(mat1,mat2);
    mat.sow_data(mat1,mat2);
    return 0;
}

outline the errors.

Lots of comments but nothing about what it's doing wrong

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.