2014-07-14 27 views
-1

我一直在試圖編寫一個程序,該程序需要用戶輸入的數字列表,這些數字存儲在二維數組中(像矩陣)。該程序以矩陣形式打印出來,然後使用函數對陣列進行轉置,然後打印轉置陣列。以2維數組爲參數調用函數時出錯(C++)

下面是程序:

#include <iostream> 
using namespace std; 

void transpose(int length, int width, int input[][length], int output[][width]) 
{ 
    for(int j=0; j<length; j++) 
     for(int i=0; i<width; i++) 
     { 
      output[j][i]=input[i][j]; 
     } 
} 


int main() 
{ 
    int width; 
    int length; 

    cout << "enter the width followed by the length of the matrix: "; 
    cin >> width >> length; 

    int input[width][length]; 
    int output[length][width]; 

    cout << "enter elements \n"; 

    for(int j=0; j<length; j++) 
     for(int i=0; i<width; i++) 
     { 
      cin >> input[i][j]; 
     } 

    cout << "your matrix is: \n"; 
    for(int j=0; j<length; j++) //output in matrix form 
     for(int i=0; i<width; i++) 
     { 
      cout << input[i][j] << " "; 
      if(i==width-1) 
       cout << endl; 
     } 

    /*for(int j=0; j<length; j++)  //to check if transpose works 
    for(int i=0; i<width; i++) 
    { 
    output[j][i]=input[i][j]; 
    }*/ 

    transpose(length, width, input, output); 

    cout << "the transpose is: \n"; 
    for(int j=0; j<width; j++) 
     for(int i=0; i<length; i++) 
     { 
      cout << output[i][j] << " "; 
      if(i==length-1) 
       cout << endl; 
     } 

    return 0; 
} 

當我嘗試調用主函數,它給出了錯誤:「否調用‘轉’匹配功能」,然後說:「候選人功能不可行:沒有已知的從'int [width] [length]'到'int(*)[length]'的第三個參數的轉換「。我不明白爲什麼。我嘗試過搜索,但我沒有運氣(儘管完全有可能我不瞭解解決方案,或者我正在尋找錯誤的東西)。

如果相關,我使用Xcode在Mac上。

+1

使用'的std :: VECTOR'代替。 – Jarod42

+0

我建議將矩陣存儲在1D數組中,並只傳遞一個指向函數第一個值的指針。像int input [] [length]這樣的參數非常混亂。 – dari

+0

看看這個:http://www.programmingsimplified.com/c-program-transpose-matrix – SerhatCan

回答

0

該問題可能源於使用可變長度數組。直到C++ 14之前,這些都不是標準的。你的編譯器可能會將它轉換爲int * []。既可以使用的載體(或其他容器STL)爲@ Jarod42建議的,或實際上指定大小

0

int input[width][length]與非編譯時間值是一個可變長度排列的一些編譯器的一種可能的擴展。

儘量使用std::vectorLive example

#include <iostream> 
#include <vector> 

std::vector<std::vector<int>> 
transpose (const std::vector<std::vector<int>>& input) 
{ 
    std::vector<std::vector<int>> res(input[0].size(), std::vector<int>(input.size())); 
    for (std::size_t i = 0; i != input.size(); ++i) { 
     for (std::size_t j = 0; j != input[i].size(); ++j) { 
      res[j][i] = input[i][j]; 
     } 
    } 
    return res; 
} 

void print(const std::vector<std::vector<int>>& m) 
{ 
    for (std::size_t i = 0; i != m.size(); ++i) { 
     for (std::size_t j = 0; j != m[i].size(); ++j) { 
      std::cout << m[i][j] << " "; 
     } 
     std::cout << std::endl; 
    } 
} 

int main() 
{ 
    int width; 
    int length; 

    std::cout << "enter the width followed by the length of the matrix: "; 
    std::cin >> width >> length; 

    std::vector<std::vector<int>> input(width, std::vector<int> (length)); 
    std::cout << "enter elements \n"; 
    for (int j = 0; j < length; j++) 
     for (int i = 0; i < width; i++) { 
      std::cin >> input[i][j]; 
     } 

    std::cout << "your matrix is: \n"; 
    print(input); 
    std::vector<std::vector<int>> output = transpose(input); 

    std::cout << "the transpose is: \n"; 
    print(output); 
    return 0; 
} 
+0

在那裏有很多我不明白的地方,所以我將不得不離開並學習更多我想。有沒有什麼特別的原因可以避免使用命名空間std? – James

+0

您可能會閱讀[爲什麼是使用命名空間標準認爲是壞的做法](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-考慮-bad-實踐) 。隨意問你不明白的地方。 – Jarod42

+0

我只是沒有看到它的大部分,所以在我問之前嘗試自己學習它可能會更好。 我一直在經歷這個:http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-096-introduction-to-c-january-iap-2011/index。 htm,我一直試圖做的問題是在第二組問題中。我沒有問題寫這個函數,但是在他們的解決方案中他們沒有說明如何在main中調用函數。我認爲只能使用前5次講座中涵蓋的內容。如果不是這樣,那麼當我學到更多東西時,我不妨繼續前進並回來。 – James