2015-01-16 50 views
0

EDITED 我想要做的是(從文件中讀取數據並將信息放入結構中定義的2d數組中),調用一個方法來查找是否存在數組中的任何零,如果是這樣,請將其更改並重新打印。我知道我缺少指針,但我不知道在哪裏。提前致謝。打印包含二維數組的結構

struct matrix{ 
const static int N=9; 
int Ar[N][N]; 
}; 

void iprint(matrix s){ //my method to print the array 
    for(int i = 0; i < 9; i++) { 
     for(int j = 0; j < 9; j++) { 
      cout << (s).Ar[i][j] << ' '; 
     } 
     cout << endl; 
    } 
} 

bool annotation(matrix s, int row, int column, int num){ 
    if(s.Ar[row][column] == 0){ 
     (s).Ar[row][column] = num; 
     return true; 
    } 
    else if(s.Ar[row][column] != 0){ 
     cout << "NO" << endl; 
     return false; 
    } else { 
     cout << "No" << endl; 
     return false; 
    } 
    iprint(s); 
} 

在首位的陣列:

0 0 6 5 0 0 1 0 0 
4 0 0 0 0 2 0 0 9 
0 0 0 0 3 0 0 0 8 
0 7 0 1 0 0 5 0 0 
0 8 0 0 0 0 0 6 0 
0 0 3 0 9 0 0 4 0 
2 0 0 0 4 0 0 0 0 
9 0 0 7 0 0 0 0 3 
0 0 5 0 0 8 2 0 0 

,我的那些方法(調用方法annotation(s,1,1,2);)後得到的輸出

2686428 0 0 2686524 8989288 4733208 0 0 -17974607 
1 0 4201360 4662484 0 8989288 8989340 9005760 0 
. 
. 
. 

林讀從文件的陣列,且方法是

bool readMatrix(matrix s){ 
ifstream f; 
f.open("nuMatrix.txt"); 
if (f.is_open()) { 
    while(!f.eof()){ 
    for(int i=0;i<9;i++){ 
     for(int j=0;j<9;j++){ 
      f>>(s).Ar[i][j]; 
      } 
     } 
    } 
    f.close(); 
    iprint(s); 
    return true; 
} 
else { 
    cerr << "NO"; 
    return false; 
} 

}`

+2

你怎麼看在陣列中,擺在首位? – FailedDev

+0

* iprint(s)*在每個if和else中都有返回時有什麼用途 – user7

+1

輸出應該是什麼? –

回答

0

您傳遞給readMatrixannotation的矩陣未被函數修改。
您正在按值傳遞矩陣,因此您只修改其副本。

改變你的函數採取參考matrix

bool annotation(matrix& s, int row, int column, int num) 
bool readMatrix(matrix& s)