实现矩阵ADT [英] Implementing matrix ADT

查看:134
本文介绍了实现矩阵ADT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮帮我。



Please help me out.

#include <iostream>
#include <cstring>

using namespace std;

struct matrixType{
    int matDimension;
    int matValues[10][10];
};

class MatrixADT{

    private:
        matrixType resultMatrix;

    public:

       //Member function declarations
  
        void intializeResultMatrix(int);
matrixType add(matrixType, matrixType);
        matrixType subtract(matrixType,matrixType);
        matrixType multiply(matrixType,matrixType);
        void printResult();
};

//Member functions of Matrix class to be defined here
matrixType MatrixADT::add(matrixType M1, matrixType M2){
//Insert code here
}

matrixType MatrixADT::subtract(matrixType M1, matrixType M2){
//Insert code here
}

matrixType MatrixADT::multiply(matrixType M1, matrixType M2){
//Insert code here
}

void MatrixADT::intializeResultMatrix(int dim){

}

int main(){

    MatrixADT maX;
    matrixType M1, M2;
    char op;
    int dim;

/*Enter your code here to accept two input matrices as instances of class Matrix and perform the operations using member functions, display the result matrix using member function*/


maX.printResult();
            
}


void MatrixADT::printResult(){

    int i,j;
    for (i=0;i<resultmatrix.matdimension;i++){>
        for (j=0; j<resultmatrix.matdimension-1;j++){>
            cout<<resultMatrix.matValues[i][j]<<" ";
        }
       cout <<resultMatrix.matValues[i][j]<<"\n";
    }
    cout <<"Done";
}

推荐答案

Quote:

struct matrixType {

int matDimension;

int matValues [10] [10];

};

struct matrixType{
int matDimension;
int matValues[10][10];
};

目的是什么 matDimension 成员,因为你有硬线尺寸(即 10x10 )?



首次尝试时,您可以通过以下方式实现 Matrix 类:

What is the purpose of the matDimension member, since you have hard-wired dimensions (namely 10x10)?

As first attempt, you could implement your Matrix class, this way:

#include <iostream>
#include <iomanip>
using namespace std;
class Matrix
{
  static const int N = 10;
  int m[N][N];

public:
  Matrix();
  //... other ctors here
  Matrix & add(const Matrix & other);
  //... other operations here
  void show();
};

Matrix::Matrix()
{
  for (int i=0; i<N; ++i)
    for (int j=0; j<N; ++j)
      m[i][j] = 0;
}
//... other ctors here
Matrix & Matrix::add(const Matrix & other)
{
  for (int i=0; i<N; ++i)
    for (int j=0; j<N; ++j)
      m[i][j] += other.m[i][j];

  return *this;
}
 //... other operations here
void Matrix::show()
{
  for (int i=0; i<N; ++i)
  {
    for (int j=0; j<N; ++j)
      cout << m[i][j] << " ";
    cout << endl;
  }
}

int main()
{
  Matrix m;
  m.show();
}


这篇关于实现矩阵ADT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆