Thread: problem in Program to find Transpose of Given Square Matrix

  1. #1
    Registered User
    Join Date
    Mar 2013
    Location
    iran
    Posts
    2

    problem in Program to find Transpose of Given Square Matrix

    hi .I want to find transpos of square matrix.but my program does not run complete.I think there is some problem.can you help me?
    Code:
    
    
    #include<iostream>
    using namespace std;
    const int size=3;                          
    void read(int a[size][size])
    {
    cout<<"enter"<<size*size<<numbers<<endl;                                 for(int i=0;i<size;i++){
    cout<< "row"<<i<<":";
    for(int j=0;j<size;j++)
    cin>>a[i][j];
    }}
    void trans(int at[size][size],int a[size][size])
    {
    int i,j;
    for(int i=0;i<size;i++){
    for(int j=0;j<size;j++)
    at[i][j]=a[j][i];
    cout<<at[i][j];
    cout<<endl;
    }}
    int main()
    {
    int a[3][3],at[3][3];
    read(a);
    trans(a,at);
    trans(a,at);
    return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    First, indent your code so that it is readable.
    http://sourceforge.net/apps/mediawik...le=Indentation
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User Vespasian's Avatar
    Join Date
    Aug 2011
    Posts
    181
    Quote Originally Posted by nazanin.padekan View Post
    hi .I want to find transpos of square matrix.but my program does not run complete.I think there is some problem.can you help me?
    Code:
    
    
    #include<iostream>
    using namespace std;
    const int size=3;                          
    void read(int a[size][size])
    {
    cout<<"enter"<<size*size<<numbers<<endl;                                 for(int i=0;i<size;i++){
    cout<< "row"<<i<<":";
    for(int j=0;j<size;j++)
    cin>>a[i][j];
    }}
    void trans(int at[size][size],int a[size][size])
    {
    int i,j;
    for(int i=0;i<size;i++){
    for(int j=0;j<size;j++)
    at[i][j]=a[j][i];
    cout<<at[i][j];
    cout<<endl;
    }}
    int main()
    {
    int a[3][3],at[3][3];
    read(a);
    trans(a,at);
    trans(a,at);
    return 0;
    }
    It seems you tried to add "numbers" as a string literal but accidentally added it in as a variable with no initialisation and hence the compiler complains.

    I think what you tried to do was the following; it works now; but this time it doesnt do what you want it to do:

    Code:
    #include<iostream>
    using namespace std;
    const int size=3;
    
    void read(int a[size][size])
    {
        cout<<"enter"<<size*size<<" numbers"<<endl;
        for(int i=0;i<size;i++){
            cout<< "row"<<i<<":";
            for(int j=0;j<size;j++)
            cin>>a[i][j];
        }
    }
    
    void trans(int at[size][size],int a[size][size])
    {
        int i,j;
        for(int i=0;i<size;i++){
            for(int j=0;j<size;j++)
                at[i][j]=a[j][i];
            cout<<at[i][j];
            cout<<endl;
        }
    }
    
    
    int main()
    {
        int a[3][3],at[3][3];
        read(a);
        trans(a,at);
        //trans(a,at);
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Matrix Transpose
    By onako in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2010, 05:47 AM
  2. Replies: 4
    Last Post: 05-25-2009, 09:29 PM
  3. transpose matrix
    By nynicue in forum C Programming
    Replies: 4
    Last Post: 12-27-2008, 07:50 PM
  4. Help with matrix transpose
    By sass208 in forum C Programming
    Replies: 6
    Last Post: 12-09-2006, 02:02 AM
  5. transpose matrix?
    By tmoney$ in forum C Programming
    Replies: 4
    Last Post: 05-03-2003, 03:51 PM