我的代碼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