我的代码atm有问题,我不知道是什么问题,因为我在startFire函数中操作数组时遇到状态访问冲突异常。我很确定没有任何数组超出界限错误,但也许我没有看到它:/或它的指针/引用错误,我没有看到。希望另一双眼睛可以看看。
这是我的代码:
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
string cfile, ccommand;
int cnum1,cnum2,cnum3,cnum4,cx;
//bool fired = false;
bool flag = true;
const double h = .2;
/*
*
*/
void printMatrix(double **x, int n)
{
int size = n;
for(int i=0; i<size; i++)
{
for(int j=0; j<size; j++)
{
std:: cout << x[i][j] << " " ;
}
std:: cout << std::endl;
}
}
void readFile(string file,double **x, int n)
{
const char* filename = file.c_str();
std::ifstream inFile(filename);
int size = n;
if(!inFile)
{
cout<<endl<<"Failed to open file " << filename;
}
for(int i=0; i<size; i++)
{
for(int j=0; j<size; j++)
{
inFile >> x[i][j];
}
}
}
void startFire(double **x, int n, int it)
{
int size = n;
int iteration = it;
/* Initialize 2D Array */
double **matrix;
matrix = new double*[n];
for(int i = 0; i<n; i++)
matrix[i] = new double[n];
for(int j=0; j<n; j++)
for(int i=0; i<n;i++)
matrix[i][j] = 0;
for()
for(int iter=1; iter<=iteration; iter++)
{
cout<<"Iteration #: "<<iter<<endl;
for(int i=1; i<size; ++i)
{
for(int j=1; j<size; ++j)
{
matrix[i][j] = .25 *(x[i-1][j]+x[i+1][j]+x[i][j-1]+x[i][j+1]);
}
}
printMatrix(matrix,size);
}
}
void GetCommandLineArguments(int argc, char **argv,string &file, int &n, int &k, int &m, int &i)
{
if(argc = 6)
{
cfile = argv[1];
cnum1 = atoi(argv[2]);
cnum2 = atoi(argv[3]);
cnum3 = atoi(argv[4]);
cnum4 = atoi(argv[5]);
}
file = cfile;
n = cnum1;
k = cnum2;
m = cnum3;
i = cnum4;
}
int main(int argc, char** argv) {
int k; //Factor of n
int m; //Innner matrix size
int i; //Iteration
int n; //Matrix Size
string file;
int input;
/*Takes in the initial cmd line values*/
GetCommandLineArguments(argc, argv, file, n, k, m, i);
/* Initialize 2D Array */
double **matrix;
matrix = new double*[n];
for(int i = 0; i<n; i++)
matrix[i] = new double[n];
for(int j=0; j<n; j++)
for(int i=0; i<n;i++)
matrix[i][j] = 0;
/*Reads a file with numbers to make matrix*/
readFile(file,matrix,n);
/*Prints array displaying a matrix*/
printMatrix(matrix,n);
/*To call the fire command that will access the middle matrix*/
while(flag != false)
{
cout<<endl
<<"MENU:\n"
<<"1 - Start Fire.\n"
<<"2 - Stop Fire.\n"
<<"3 - QUIT.\n"
<<" Enter your choice and press return: ";
cin >> input;
switch(input)
{
case 1:
cout<<"Starting fire.\n";
startFire(matrix, n,i);
//fired = true;
break;
case 2:
cout<<"Stop fire.\n";
//fired = false;
break;
case 3:
cout<<"Ending program.\n";
flag = false;
break;
default:
cout<<"Not a valid choice.\n";
cout<<"Choose again.\n";
cin>>input;
break;
}
}
return 0;
}
我的名为sample.input的文件输入采用以下数字
20.0
20.0
20.0
20.0
20.0
20.0
20.0
20.0
20.0
20.0
20.0
20.0
20.0
20.0
20.0
20.0
20.0
20.0
20.0
20.0
20.0
20.0
20.0
20.0
20.0
20.0
20.0
200.0
200.0
20.0
20.0
20.0
20.0
20.0
20.0
200.0
200.0
20.0
20.0
20.0
20.0
20.0
20.0
20.0
20.0
20.0
20.0
20.0
20.0
20.0
20.0
20.0
20.0
20.0
20.0
20.0
20.0
20.0
20.0
20.0
20.0
20.0
20.0
20.0
这是我通过cmd行的错误。
答案 0 :(得分:3)
您正在分配的数组大小之外访问:
matrix[i][j] = .25 *(x[i-1][j]+x[i+1][j]+x[i][j-1]+x[i][j+1]);
对于内核操作,您应该在每条边上分配一个更大的矩阵(总共+2),并将边值归零或复制(取决于矩阵运算)。您需要调整索引以考虑此偏移量。
(或者,您可以将索引钳位在矩阵内部。)
答案 1 :(得分:2)
for(int i=1; i<size; ++i)
{
for(int j=1; j<size; ++j)
{
matrix[i][j] = .25 *(x[i-1][j]+x[i+1][j]+x[i][j-1]+x[i][j+1]);
}
}
注意部分
x[i][j+1]
在最后一次迭代,即i = size-1,j = size-1将到达矩阵之外