sinat_22235451 于 2016.03.04 16:29 提问

Science上发表的聚类算法，C++编程实现，有一个bug不知道怎么解决！！跪求大神！！！ 1C
`````` include<iostream>
include<fstream>using namespace std;
typedef struct sourcedata     //声明了一个原始数据结构体
{
int m;                    //矩阵的行rows
int n;                   //矩阵的列columns
double **data;           //保存数据的二维指针
}SourceData;

SourceData getdata()  //读取文档中的数据，以二维指针的方式对数据进行存储，数据保存为txt格式
{
const  char *File = "f:\\test1213.txt";  //数据的位置
SourceData dat;
ifstream testdata;
testdata.open(File);
if (!testdata)
{
cout << "cannot open" << endl;
}
testdata >> dat.m;
testdata >> dat.n;
dat.data = new double*[dat.m];
for (int i = 0; i < dat.m; i++)
dat.data[i] = new double[dat.n];
for (int i = 0; i < dat.m; i++)
for (int j = 0; j < dat.n; j++)
testdata >> dat.data[i][j];
return dat;
testdata.close();
}
void main()
{
sourcedata pp;
pp = getdata();

double **x = NULL;    //原始数据指针
x = pp.data;

int rows = pp.m;
int columns = pp.n;
double **Distance;

long double sum;
Distance = new double *[rows];
for (int i = 0; i < rows; i++)
Distance[i] = new  double[columns];

for (int i = 0; i < rows; i++)
{
for (int j = 627; j < rows; j++)
{
double sum = 0;
for (int k = 0; k < columns; k++)
{
**sum += (*(*(x + i) + k) - *(*(x + j) + k))*(*(*(x + i) + k) - *(*(x + j) + k)); //总是在此处出bug**
}
sum = sqrt(sum);
Distance[i][j] = sum;
*(*(Distance + i) + j) = sum;
}
}
ofstream  file1;
file1.open("距离计算的test.txt");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
file1 << *(*(Distance + i) + j) << "  ";
}
file1 << "\n";
}

for (int i = 0; i < rows; i++)              //释放指针
delete[rows]Distance[columns];
delete[rows]Distance;
}
``````

1个回答

caozhy      2016.03.06 07:22