我的代码atm有问题,我不知道是什么问题,因为我在startFire函数中操作数组时遇到状态访问冲突异常。我相当肯定没有任何数组超出界限错误,但也许我没有看到它:/或它没有看到的指针/引用错误。希望另一双眼睛可以看看。二维矩阵STATUS_ACCESS_VIOLATION错误
这里是我的代码:
#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线得到的错误。
http://i53.tinypic.com/2qu4078.jpg
好吧,这是真的,它的工作感谢信息:) – Novazero 2011-05-03 04:21:00