[英]Printing an struct that includes an 2d array
EDITED What I am trying to do is (after reading from file and putting the information in 2d array defined in a struct,that part works) call a method that finds out if there is any zeroes in the array and if so change it and prints it again. 编辑我想做的是(从文件中读取信息并将其放入结构中定义的2d数组中后,该部分有效)调用一种方法,该方法可以找出数组中是否有零,如果是,则进行更改并打印再来一次。 I know I´m missing pointers but I don't know where. 我知道我缺少指针,但不知道在哪里。 Thanks in advance. 提前致谢。
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);
}
The array in first place : 数组放在第一位:
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
The output that I get after those methods (calling the method annotation(s,1,1,2);
) 我在这些方法之后得到的输出(调用方法annotation(s,1,1,2);
)
2686428 0 0 2686524 8989288 4733208 0 0 -17974607
1 0 4201360 4662484 0 8989288 8989340 9005760 0
.
.
.
Im reading the array from a file,and the method is 我从文件中读取数组,方法是
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;
}
}` }`
The matrix you're passing to readMatrix
and annotation
isn't modified by the functions. 这些函数不会修改您传递给readMatrix
和annotation
的矩阵。
You're passing the matrix by value, so you're only modifying a copy of it. 您要按值传递矩阵,所以您只修改它的一个副本。
Change your functions to take a reference to a matrix
: 更改函数以引用 matrix
:
bool annotation(matrix& s, int row, int column, int num)
bool readMatrix(matrix& s)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.