Transpose Matrix // C++

后端 未结 1 1783
北海茫月
北海茫月 2021-01-29 07:17

Here is My code, it\'s finding the Transpose of a Matrix, but columns and rows are the same as the input 10*5.

If entered:

1   2  3  4  5  6  7  8  9 10
11         


        
1条回答
  •  迷失自我
    2021-01-29 07:44

    You have 5 rows and 10 columns of input, so

            int r = 10;
            int c = 5;
    

    should be

            int r = 5;
            int c = 10;
    

    Also to match the output to the desired output,

                    cout << " " << trans[i][j];
                    if(j == r - 1)
                        cout << endl;
    

    should be

                    cout << trans[i][j];
                    if (trans[i][j] < 10) cout << " ";
                    if(j == r - 1)
                        cout << endl;
                    else
                        cout << " ";
    

    because

    • One more space should be printed after one-digit numbers
    • No space should be printed before the first numbers in each rows

    0 讨论(0)
提交回复
热议问题