1

私はC++を初めて使用します。ここでは、スパース行列の転置を見つけようとしています。コードは次のとおりです。

#include<iostream>
using namespace std;
class transposeM{
    int m1[20][20],m2[20][20],i,j,row,column,t;
public:
    void read(){
        t=0;
        cout<<"Enter the number of row: \n";
        cin>>row;
        cout<<"enter the number of column: \n";
        cin>>column;

        for(i=0;i<row;i++){
            for(j=0;j<column;j++){
                cin>>m1[i][j];

                if(m1[i][j]){
                    t++;
//                  cout<<"first t is:"<<t;
                //if non zero
                            m2[t][0]=i+1;
                            m2[t][1]=j+1;
                            m2[t][2]=m1[i][j];

                }


            }
        }

        m2[0][0]=row;
        m2[0][1]=column;
        m2[0][2]=t;
    }

    void displaysp(){
cout<<"sparse matrix is: \n";
        for(i=0;i<=t;i++){
            for(j=0;j<3;j++){
                cout<<m2[i][j]<<" ";
            }
            cout<<"\n";
        }
    }

    void transpose(){
        int transpose[20][3];

                        transpose[0][0]=m2[0][0];
                        transpose[0][1]=m2[0][1];
                        transpose[0][2]=m2[0][2];
        cout<<"Transpose is: \n";
        int q=1;
        for(i=1;i<=column;i++){
            for(int p=1;p<=t;p++){
                if(m2[p][1]==i){
                    transpose[q][0]=m2[p][0];
                    transpose[q][1]=m2[p][1];
                    transpose[q][2]=m2[p][2];
                    q++;

                }
            }
        }

        for(i=0;i<=column;i++){
            for(j=0;j<3;j++){
                cout<<transpose[i][j]<<" ";
            }
            cout<<"\n";
        }

    }
    void display(){
        for(i=0;i<row;i++){
                for(j=0;j<column;j++){
                    cout<<m1[i][j]<<" ";


                }
                cout<<"\n";
            }

    }
};
int main(int argc,char ** argv){
    transposeM obj;
    obj.read();
    obj.display();
    obj.displaysp();
    obj.transpose();
    return 0;
}

出力:

Enter the number of row: 
2
enter the number of column: 
2
0
1
2
0
0 1 
2 0 
sparse matrix is: 
2 2 2 
1 2 1 
2 1 2 
Transpose is: 
2 2 2 
2 1 2 
1 2 1 

しかし、何かがうまくいかなかった。行列を読み取り、それをスパース行列に変換することは問題ありません。しかし、転置を見つけると、論理的なエラーが発生しました。

4

3 に答える 3

2

転置の単純なコードスニペットは次のようになります:(transposeの転置になりますm2

for(i=0;i<column;i++) {
    for(j=0;j<row;j++) {
       transpose[i][j] = m2[j][i];
    }
}

あなたは不必要に複雑なことをしています。

于 2013-03-08T14:20:11.630 に答える
2

いくつかの問題を回避し、問題を単純化するのに役立つ可能性があるのは、行列を転置するときに、ケースのとインデックスを切り替えることができるというrowことcolumnです。したがって、1つのメソッドを記述して、転置フラグまたは引数を設定するだけで済みます。例:ijdisplay()

void displaysp()
{
    // m is the number of rows in matrix
    // n is the number of columns in matrix
    unsigned int tmpM=m, tmpN=n ; 

    if( transposeFlag )
    {
       tmpM = n ;
       tmpN = m ;
    }         

    for(unsigned i=0;i<tmpM;i++)
     {
        for(unsigned j=0;j<tmpN;j++)
        {
            if( transposeFlag )
            {
                cout<<m2[j][i]<<" ";
            }
            else
            {
               cout<<m2[i][j]<<" ";
            }
        }
        cout<<"\n";
    }
}
于 2013-03-08T14:20:20.253 に答える
0

問題は修正されました、私はコードを投稿しています:

#include<iostream>
using namespace std;
class transposeM{
    int m1[20][20],m2[20][20],i,j,row,column,t;
public:
    void read(){
        t=0;
        cout<<"Enter the number of row: \n";
        cin>>row;
        cout<<"enter the number of column: \n";
        cin>>column;

        for(i=0;i<row;i++){
            for(j=0;j<column;j++){
                cin>>m1[i][j];

                if(m1[i][j]){
                    t++;
                            m2[t][0]=i+1;
                            m2[t][1]=j+1;
                            m2[t][2]=m1[i][j];

                }


            }
        }

        m2[0][0]=row;
        m2[0][1]=column;
        m2[0][2]=t;
    }

    void displaysp(){
cout<<"sparse matrix is: \n";
        for(i=0;i<=t;i++){
            for(j=0;j<3;j++){
                cout<<m2[i][j]<<" ";
            }
            cout<<"\n";
        }
    }

    void transpose(){
        int transpose[20][3];

                        transpose[0][0]=m2[0][1];
                        transpose[0][1]=m2[0][0];
                        transpose[0][2]=m2[0][2];
        cout<<"Transpose is: \n";
        int q=1;
        for(i=1;i<=column;i++){
            for(int p=1;p<=t;p++){
                if(m2[p][2]==i){
                    transpose[q][0]=m2[p][1];
                    transpose[q][1]=m2[p][0];
                    transpose[q][2]=m2[p][2];
                    q++;

                }
            }
        }

        for(i=0;i<=column;i++){
            for(j=0;j<3;j++){
                cout<<transpose[i][j]<<" ";
            }
            cout<<"\n";
        }

    }
    void display(){
        for(i=0;i<row;i++){
                for(j=0;j<column;j++){
                    cout<<m1[i][j]<<" ";


                }
                cout<<"\n";
            }

    }
};
int main(int argc,char ** argv){
    transposeM obj;
    obj.read();
    obj.display();
    obj.displaysp();
    obj.transpose();
    return 0;
}

出力:

Enter the number of row: 
2
enter the number of column: 
3
0
1
2
0
0
2
0 1 2 
0 0 2 
sparse matrix is: 
2 3 3 
1 2 1 
1 3 2 
2 3 2 
Transpose is: 
3 2 3 
2 1 1 
3 1 2 
3 2 2 
于 2013-03-08T14:58:13.107 に答える