我在主要功能
中有以下内容 void checkGrid(int rows, int cols, int **src){
cout<<"Testing";
int neighbors;
for(int i = 0; i < rows; i++){
for(int j = 0; i < cols; j++){
neighbors = getNeighborsCount(rows, cols, i , j, src);
}
}
}
int main(int argc, char *argv[])
{
if (argc != 3) {
cerr << "Invalid command line - usage: <input file> <number of threads>" << endl;
exit(-1);
}
// Extract parameters
ifstream ifile(argv[1]);
int num_threads = atoi(argv[2]);
// Set the number of threads
task_scheduler_init init(num_threads);
// Get the size of the problem - on the first line of the input file.
int size;
ifile >> size;
// TODO: Create and initialize data structures
int rows = size;
int cols = size;
int **grid_A = new int*[rows];
for(int i = 0; i < rows; i++){
grid_A[i] = new int[cols];
}
for(int i = 0; i < size; i++){
for(int j = 0; j < size; j++){
ifile >> grid_A[i][j];
}
}
for(int i = 0; i < size; i++){
for(int j = 0; j < size; j++){
cout << grid_A[i][j];
}
}
// Start the timer
tick_count start = tick_count::now();
// TODO: Execute the parallel algorithm
cout<<"Testing";
checkGrid(rows, cols, grid_A);
// Stop the timer
tick_count end = tick_count::now();
double run_time = (end-start).seconds();
cout << "Time: " << (end-start).seconds() << endl;
// TODO: Print the output to a file
// ofstream outfile("output.txt");
//
//
// outfile.close();
//
// // Append the peformance results to the results file.
// ofstream ofile("life.csv", ios::app);
// ofile << size << "," << num_threads << "," << run_time << endl;
// ofile.close();
for(int i = 0; i < rows; i++){
delete[] grid_A[i];
}
delete []grid_A;
return 0;
}
我能够编译程序,但是当我运行构建文件时,它不会打印出&#34;测试&#34;声明。有人可能会指出这是因为传递二维数组的方式还是别的。
编辑:根据请求发布整段代码,我也是C ++的新手,只是关注如何将2d数组作为参数传递给函数的一些例子
答案 0 :(得分:2)
可能是因为您没有冲洗 stdout
。试试cout<<"Testing" << flush;
。
其余部分甚至无法打印的原因是因为您的checkGrids
函数中存在循环:
for(int i = 0; i < rows; i++){
for(int j = 0; i < cols; j++){
neighbors = getNeighborsCount(rows, cols, i , j, src);
}
}
for(int j = 0; i < cols; j++)
应
for(int j = 0; j < cols; j++)
目前该循环只是一个死锁。
另外,请不要使用这样的指针,你一定会遇到麻烦。 C ++有std::vector
的原因。或者对于编译时已知的长度和C ++ 11 std::array
。这也将为您节省必须将长度传递给checkGrid
的麻烦。