0

우리는 동적 곱셈을 사용하여 행렬 곱셈을위한 C++ 프로그램을 코딩하는 과제를 부여 받았습니다. 그는 우리에게 재귀를 사용하라고 말했고 우리에게 맞춤형 매트릭스 클래스를 주었다. 나는 그 이유는 이런 일이에 관해서는계산에 실패한 재귀 매트릭스 곱셈 알고리즘

Object& Matrix<Object>::at(uint, uint) [with Object = unsigned int, uint = unsigned int]: Assertions 'row < rows && col < cols' failed.

어떤 아이디어를 말한다 실행할 때 나는 그러나 내가 오류를 얻고, 다음 재귀 알고리즘을 썼다? 아래에 그의 행렬 클래스와 재귀 적 행렬 곱셈 방법을 포함했습니다.

#ifndef MATRIX_H 
#define MATRIX_H 

#include <cassert> 
typedef unsigned int uint; 

template <class Object> 
class Matrix 
{ 
public: 
    Matrix(uint rows, uint cols); 
    Object & at(uint row, uint col); 
    const Object & at(uint row, uint col) const; 
    ~Matrix(); 
    Matrix(const Matrix<Object> & m); // Copy constructor 
    Matrix & operator= (const Matrix<Object> & m); // Assignment operator 
    uint numrows() const; 
    uint numcols() const; 

private: 
    uint rows; 
    uint cols; 
    Object* data; 
}; 

template <class Object> 
Matrix<Object>::Matrix(uint rows, uint cols) 
: rows(rows), cols(cols) 
{ 
    assert(rows > 0 && cols > 0); 
    data = new Object[ rows * cols ]; 
} 

template <class Object> 
Matrix<Object>::~Matrix() 
{ 
    delete[] data; 
} 

template <class Object> 
Object & Matrix<Object>::at(uint row, uint col) 
{ 
    assert(row < rows && col < cols); 
    return data[ cols * row + col ]; 
} 

template <class Object> 
const Object & Matrix<Object>::at(uint row, uint col) const 
{ 
    assert(row < rows && col < cols); 
    return data[ cols * row + col ]; 
} 

template <class Object> 
uint Matrix<Object>::numrows() const 
{ 
    return rows; 
} 

template <class Object> 
uint Matrix<Object>::numcols() const 
{ 
    return cols; 
} 

int minmult(Matrix<uint> & P, 
     Matrix<uint> & M, 
     const vector<uint> & d, 
     uint i, 
     uint j) 
{ 


if(M.at(i,j) != INF) 
{ 
    return M.at(i,j);    //already has been defined 
} 
else if(i == j) 
{ 
    M.at(i,j) = 0;     //base case 
} 
else 
{ 
    //M.at(i,j) = UINT_MAX;   //initialize to infinity 
    for(uint k = i; k <= j-1; k++) 
    { 
     uint ops = minmult(P, M, d, i, k) 
      + minmult(P, M, d, k+1, j) 
      + d.at(i-1)*d.at(k)*d.at(j); 
     if(ops < M.at(i,j)) 
     { 
      M.at(i,j) = ops;   
      P.at(i,j) = k;   
     } 
    } 
} 
return M.at(i,j);     //returns the final cost 
} 

답변

1

오류는, 당신은 at 메소드를 호출하고 코드에 분명하다 행과 열의 수 ...보다 작은 rowcol 값을 전달하는 매우 분명한 것 같다 :

당신이 at에 인수 rowscols 주변이 아닌 다른 방법이기 때문에, 즉, M.at(j,i)를 호출에 의도 가정
uint i = M.numcols(); 
uint j = M.numrows(); 
if(i == j) { 
    M.at(i,j) = 0; // i == numcols() thus !(i<numcols()) 
         // j == numrows() thus !(j<numrows()) 

...

그 외의 경우 재귀의 다음 단계는 원래보다 작은 문제가 없으므로 (정확히 같은 크기입니다. minmult(M,P,d)minmult(M,P,d)이므로이 단계는 잘못되었습니다. 어설 션을 수정하면 스택 오버플로 형식으로 시작됩니다.

마지막으로 코드의 의도가 무엇인지 확실하지 않으므로 펜과 용지로 문제를 해결하고 솔루션을 원하는 프로그래밍 언어로 매핑해야합니다.

+0

조언을 주셔서 감사합니다. 질문에서 내 minmult 메소드를 편집했습니다. 나는 이제 내가 알아낼 수있는 또 다른 문제가있다. 행렬을 인쇄 할 때, 단지 0이 인쇄됩니다. 마치 계산이 이루어지는 것처럼 보이지 않습니다. 어떤 아이디어? 또한, minmult가 호출되기 전에 INF에 대한 모든 행렬 위치를 인스턴스화합니다. – Busch

+0

@SeanHellebusch : 변수가 무엇을 의미하는지 문서화해야합니다. 그들이 무엇인지 알아내는 데는 시간이 걸렸습니다. 그렇다면'INF'의 가치는 무엇입니까? 어떻게 초기화합니까? 여러분의 행렬이 0으로 초기화되면,'ops