2012-07-30 77 views
2

我是編程新手,所以我想編寫一個代碼,讓我輸入一個2維數組(或者矩陣),然後打印出來。'int'的問題

#include <iostream> 
using namespace std; 

void printArray(const int *array, int count) 
    { 
     for (int i = 0; i < count; i++) 
      cout << array[ i ] << " "; 

     cout << endl; 
    } 

int main() { 
int n; 
cout<<"Please enter the length of your matrix : "<<endl; 
cin>>n; 
int * y=new int [n]; 
for (int w = 0; w <= n-1; w++) { 

    y[w] = new int [n]; 
    cout<<"Insert the elements "; 

      for (int z = 0; z <= n-1; z++) 
      { 
       cin >>y [w][z]; 
      } 
} 

printArray(y, n); 

} 

但是我得到「從‘詮釋*’到‘廉政’無效轉換」和錯誤「無效的類型int [INT]數組下標」。你能否檢查我的代碼並指出我的缺陷?

由於

+0

@RyanGray如果數組不是固定大小(不是),那麼它必須是新的。當然,應該真正使用諸如'std :: vector'之類的結構。 – juanchopanza 2012-07-30 16:46:44

+0

@RyanGray:使用'new []'是創建一個數組的合法方式,它與'new'不一樣。 – Rook 2012-07-30 16:46:50

+0

你需要使用雙指針,你的數組需要被聲明爲int ** y = new int * [n] – user1084113 2012-07-30 16:47:25

回答

6

您聲明yint*,它只會是一維的。您需要聲明yint**,因爲它是二維的。

代碼不編譯的原因是因爲int* y指向的存儲器中的單個(即是一個整數數組,換句話說,一堆int峯)。 y[w]是其中一個int s在這個數組中,因此y[w] = new int[n]無法編譯,因爲您試圖將int*指定給int

更改yint**意味着y可以指向的int*秒的陣列。由於每個int*都可以指向一個int的數組,因此您將擁有一個2維數組。爲10×10矩陣

實施例的代碼與int**

int** iShouldUseStdVector = new int*[10]; // allocate 10 int* <-- 
for (int i = 0; i < 10; i++) 
{ 
    iShouldUseStdVector[i] = new int[10]; // allocate 10 int <-- 
    for (int k = 0; k < 10; k++) 
    { 
     iShouldUseStdVector[i][k] = k; 
    } 
} 

實施例的代碼爲10×10矩陣,std::vector

std::vector<std::vector<int>> thisIsEasy; 
for (int i = 0; i < 10; i++) 
{ 
    thisIsEasy.push_back(std::vector<int>()); 
    for (int k = 0; k < 10; k++) 
    { 
     thisIsEasy[i].push_back(k); 
    } 
} 

我建議使用std::vector<std::vector<int>> y;代替,因爲它處理用於存儲器你通過方便地增長,因爲你想添加更多的元素,並在其被破壞時釋放內存。

0

int * y=new int [n];是指向int值的動態分配的陣列;

y[w] = new int [n];試圖將指針分配給數組的一個元素。

作爲一個學習練習,亂搞原始數組是一個好主意。一旦你知道如何使用它們,你就足夠了解停止使用它們(非常多),並使用自動存儲類型,如std::vector

+0

感謝您的幫助! – user1563544 2012-08-01 10:52:10

1

int * y = new int [n];

這是一個長度爲n的數組。你所需要的是:

int **y = new int*[n]; 
for (int i=0; i<n; i++) 
    y[i] = new int[n]; 

.... 

//delete[] y[i] in a loop 
delete[] y; 

由於您使用C++,爲什麼不:

#include <vector> 

... 
std::vector<std::vector<int> > matrix; 
+0

非常感謝! – user1563544 2012-08-01 10:50:21

0

1)你想聲明y爲指針的int數組:

int ** y = new int* [n]; 

2)在printArray你正在處理一個矩陣,而不是一個數組。要訪問一個矩陣的成員使用兩個嵌套循環for這樣的:

for (int i = 0; i < nLines; i++) { 
    for (int j = 0; i < nColumns; j++) { 
     std::cout << matrix[i][j] << std::endl; 
    } 
} 

3)需要一個指針傳遞給的int陣列,而不是一個指針到一個intprintArray方法。

void printArray(const int **array, int count) 

找到工作的代碼,這裏的上述修正:http://ideone.com/2h9dR

+0

謝謝你的幫助!欣賞鏈接。 – user1563544 2012-08-01 10:50:48

0

首先,閱讀其他的答案,然後也許重讀在一個良好的C++的書指針章。現在,除非你需要極高的速度,否則使用vectorvectordouble。用C++ 11(較新的C++標準),這是非常好的,可讀的,所以我張貼第一:

#include <iostream> 
#include <vector> 

void printArray(std::vector< std::vector<double> > & v) { 
    for (const auto & row : v){ 
     for (const auto & value : row){ 
     std::cout << value << " "; 
     } 
     std::cout << std::endl; 
    } 
} 

int main() { 
    int n; 
    std::cout<<"Please enter the length of your matrix : "<<std::endl; 
    std::cin>>n; 
    std::vector<std::vector<double>> y(n,std::vector<double>(n,0)); 
    for (auto & row : y){ 
     std::cout<<"Insert the elements of row :"; 
     for (auto & value : row){ 
     std::cin >> value; 
     } 
    } 
    printArray(y); 
} 

對於較舊的C++是這樣的:

void printArray(std::vector< std::vector<double> > & v) { 
    for (std::vector<std::vector<double> >::const_iterator it = v.begin(); it != v.end();it++){ 
     for (std::vector<double>::const_iterator it2 = it->begin(); it2!= it->end();it2++) { 
    std::cout << (*it2) << " "; 
     } 
     std::cout << std::endl; 
    } 
} 

int main() { 
    int n; 
    std::cout<<"Please enter the length of your matrix : "<<std::endl; 
    std::cin>>n; 
    std::vector<std::vector<double> > y(n,std::vector<double>(n,0)); 
    for (std::vector<std::vector<double> >::iterator it = y.begin(); it!= y.end();it++){ 
     std::cout<<"Insert the elements of row :"; 
     for (std::vector<double>::iterator it2 = it->begin(); it2!= it->end();it2++) { 
    std::cin >> (*it2); 
     } 
    } 
    printArray(y); 
} 

注意y(n,std::vector<double>(n,0))手段,使n載體,每個載體有n零。您也可以使用y[1][2]來獲取和設置值。如果您使用y.at(1).at(2),則可以進行適當的檢查,以便在讀取或寫入界限時收到異常。

+0

謝謝你的幫助! – user1563544 2012-08-01 10:52:30

+0

謝謝。另一種感謝的方式是贊成。 ;-) – 2012-08-01 21:31:41