2011-09-16 10 views
0

한다고 가정 우리는이 같은 3 * 3 행렬있다 :행렬 및 벡터 곱셈

1 3 4 
2 6 8 
9 0 12 

그리고이 같은 일부 벡터 :

1 2 3 

내 질문은 : 그것을 구현하는 방법을 나는 번식 할 수 있도록 하나씩?

#include <cstdlib> 
#include <math.h> 
#include <iostream> 

using namespace std; 

int main(int argc, char *argv[]) 
{ 
    int a[3][3]={{ 2,4,3},{1,5,7},{0,2,3}}; 
    int b[]={2,5,6}; 
    int c[3]; 

    for (int i=0;i<3;i++){ 
     c[i]=0; 
    } 

    for (int i=0;i<3;i++){ 
     for (int j=0;j<3;j++){ 
      c[i]+=(a[i][j]*b[j]); 
     } 
    } 

    for (int i=0;i<3;i++){ 
     cout<<a[i]<<" "<<endl; 
    } 

    system("PAUSE"); 
    return EXIT_SUCCESS; 
} 

내가 가진 그 결과는 다음과 같습니다 :

0x22ff10 
0x22ff1c 
0x22ff28 
+1

그런 배열은 인쇄 할 수 없습니다. 각 요소를 개별적으로 인쇄하려면 루프를 작성해야합니다. – Mysticial

답변

4

변경 :

for (int i=0;i<3;i++){ 
     cout<<a[i]<<" "<<endl; 

에 :

for (int i=0;i<3;i++){ 
     cout<<c[i]<<" "<<endl; 
+0

네, 정확하게 제가 오타를 만들었습니다. –

3

난 당신이 인쇄되고 싶은 생각 나는 예제 코드가 c[i] 아니요, a[i] , 마지막 루프에서.

2

개체를 디자인 하시겠습니까?

// matrix of ints, floats, doubles, whatever numeric type you want 
template<typename T> 
class Matrix 
{ 
public: 
    Matrix(int rows, int cols) 
    { 
     // init m_values to appropriate rows and cols 
    } 

    Matrix<T> operator+(const Matrix<T>& rhs) 
    { 
     // add this matrix to the rhs matrix 
    } 

    Matrix<T> operator*(const Matrix<T>& rhs) 
    { 
     // verify both matrices have same dimensions (3x3 etc) 
     // multiple this matrix by rhs by indexing into m_values 
    } 

    // etc 

private: 
    // two dimensional dynamic array of type T values 
    std::vector<std::vector<T>> m_values; 
}; 

비 멤버 템플릿 기능을 사용하여 작업을 수행 할 수도 있습니다. 당신이 그것을 원한다면 나는 값, 평등, 행 연산 등을 가진 을 나타내는 클래스를 만들 것입니다. 그리고 클래스를 행의 벡터로 만듭니다.