我正在尝试使用C中的qsort()对双精度的二维数组进行排序。这些数组包含三维点数据,这些数据是使用fscanf从文件中读取的。我的编程技能相当有限,但我有非常大的数据集需要处理。如果我的代码很糟糕,请提前道歉。
23127.947,23127.947,23127.947
523127.790,523127.790,523127.790
523127.747,523127.747,523127.747
523127.761,523127.761,523127.761
523127.768,523127.768,523127.768
(...for 3,158,632点)
我已经使用printf来隔离我的代码中的问题似乎是qsort()行,它导致了分段错误。从我读到的关于Stack Overflow的其他问题来看,这可能是我的“比较”函数的问题。做一维数组的例子似乎很简单,但我看到的二维数组的例子并没有涉及到比较其他维度(首先是X,如果X1 = X2,则比较Y,然后如果Y1 = Y2,则比较Z)。
int main(int argc, char *argv[]) {
int i,j,c;
double x,y,z;
int ROWS = 3158632;
int COLS = 3;
char buffer[100];
double** data = Make2DDoubleArray(ROWS, COLS);
//Open the plot file to read in, and have an output write file
FILE *fp = fopen("Plot_1-2.txt","r");
if(fp == NULL) {
printf("Can't open file\n");
exit;
}
fgets(buffer, 100, fp); //Ignore header
for(i=0; ; i++){
if ((c = fgetc(fp)) == EOF){
break;
}
fscanf(fp,"%lf, %lf, %lf",&x, &y, &z);
data[i][0] = x;
data[i][1] = y;
data[i][2] = z;
}
printf("First 5 unsorted numbers:\n");
for(j=0;j<5;j++){
printf("Line %d: %.3lf, %.3lf, %.3lf\n",j, data[j][0], data[j][0], data[j][0]);
}
printf("Last 5 unsorted numbers:\n");
for(j=ROWS-5;j<ROWS;j++){
printf("Line %d: %.3lf, %.3lf, %.3lf\n",j, data[j][0], data[j][0], data[j][0]);
}
/* Sort array using Quicksort algorithm: */
printf("Sorting...\n");
qsort(data, ROWS, COLS*sizeof(double), &compare);
printf("First 10 sorted numbers:\n");
for(j=0;j<10;j++){
printf("Line %d: %.3lf, %.3lf, %.3lf\n",j, data[j][0], data[j][0], data[j][0]);
}
fclose(fp);
for (i=0; i<ROWS; i++){
free(data[i]);
}
free(data);
return 0;
}
double** Make2DDoubleArray(int arraySizeX, int arraySizeY) {
double** theArray;
int i;
theArray = (double**) malloc(arraySizeX*sizeof(double*));
for (i = 0; i < arraySizeX; i++)
theArray[i] = (double*) malloc(arraySizeY*sizeof(double));
return theArray;
}
int compare(const void *arg1, const void *arg2) {
//double a, b, c, d, e, f;
double *a = (double*)arg1;
double *b = (double*)arg2;
double *c = ((double*)arg1 + 1);
double *d = ((double*)arg2 + 1);
double *e = ((double*)arg1 + 2);
double *f = ((double*)arg2 + 2);
if(a > b)
return 1;
else if(a < b)
return -1;
else {
if(c > d)
return 1;
else if(c < d)
return -1;
else {
if(e > f)
return 1;
else if(e < f)
return -1;
else
return 0;
}
}
}我在想,告诉qsort去"COLS * sizeof(double)“是不是对我为2D数组分配内存的错误方式?把这个问题当作一个一维数组来处理会不会让剩下的部分都能正常工作呢?如果可能的话,我更喜欢将其保留为2D数组。
发布于 2013-04-23 09:40:45
所有这些都不意味着没有像<stdio.h>、<stdlib.h>等头部的任何东西。
请解释一下exit;。我想你指的是exit(0);。
您的main中有一些问题。由于该fgetc,您的代码可能会丢失第一个值的最高有效位,这是一个微妙的错误。如果要测试EOF,请测试scanf (Jee!我没想到这一点!我希望他们把这些东西写在手册里!嗯,他们确实……)。文件末尾的示例比这更好,因为该示例确保fscanf实际解析了三个值。
for(size_t i=0; fscanf(fp,"%lf, %lf, %lf",&x, &y, &z) != EOF; i++){
data[i][0] = x;
data[i][1] = y;
data[i][2] = z;
}您的Make2DDoubleArray函数中存在问题。它分配了许多不相交的数组,这是qsort无法处理的。在一个步骤中分配数组不是更干净吗?
void *Make2DDoubleArray(size_t x) {
double (*theArray)[3] = malloc(x * sizeof *theArray);
return theArray;
}theArray被声明为指向3个双精度数组的指针。为此,您甚至不需要Make2DDoubleArray。
compare函数中有一个问题。
double *a = (double*)arg1;
double *b = (double*)arg2;a和b是指针,
if(a > b)
return 1;
else if(a < b)
return -1;..。然而,您的代码将它们作为整数进行比较,从而使排序出现故障。array[0]的地址将始终小于array[1]的地址。
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
int main(int argc, char *argv[]) {
int j,c;
double x,y,z;
size_t ROWS = 3158632;
size_t COLS = 3;
char buffer[100];
double (*theArray)[COLS] = malloc(ROWS * sizeof *theArray);
//Open the plot file to read in, and have an output write file
FILE *fp = fopen("Plot_1-2.txt","r");
if(fp == NULL) {
printf("Can't open file\n");
exit(0);
}
fgets(buffer, 100, fp); //Ignore header
for(size_t i=0; fscanf(fp,"%lf, %lf, %lf", &x, &y, &z) == 3; i++){
data[i][0] = x;
data[i][1] = y;
data[i][2] = z;
}
printf("First 5 unsorted numbers:\n");
for(size_t j=0; j<5; j++){
printf("Line %zu: %.3lf, %.3lf, %.3lf\n", j, data[j][0], data[j][0], data[j][0]);
}
puts("Last 5 unsorted numbers:");
for(size_t j=ROWS-5; j<ROWS; j++){
printf("Line %zu: %.3lf, %.3lf, %.3lf\n", j, data[j][0], data[j][0], data[j][0]);
}
/* Sort array using Quicksort algorithm: */
puts("Sorting...");
qsort(data, ROWS, sizeof *data, compare);
puts("First 10 sorted numbers:");
for(size_t j=0;j<10;j++){
printf("Line %zu: %.3lf, %.3lf, %.3lf\n", j, data[j][0], data[j][0], data[j][0]);
}
fclose(fp);
free(data);
return 0;
}
int compare(const void *arg1, const void *arg2) {
double (*x)[3] = arg1;
double (*y)[3] = arg2;
if ((*x)[0] > (*y)[0])
return 1;
else if ((*x)[0] < (*y)[0])
return -1;
else if ((*x)[1] > (*y)[1])
return 1;
else if ((*x)[1] < (*y)[1])
return -1;
else if ((*x)[2] > (*y)[2])
return 1;
else if ((*x)[2] < (*y)[2])
return -1;
else
return 0;
}发布于 2013-04-23 08:29:31
请尝试对数据使用结构:
typedef struct {
double x;
double y;
double z;
} point_data;那么你只需要一个这种新类型的一维数组:
point_data *array = malloc(linesRead * sizeof *array);并且您的比较函数仍然非常相似:
int compare(const void *arg1, const void *arg2) {
point_data *point1 = arg1,
*point2 = arg2;
if ( point1->x > point2->x ) {
return 1;
else if ( point1->x < point2->x ) {
return -1;
} else {
if ( point1->y > point2->y ) {
return 1;
else if ( point1->y < point2->y ) {
return -1;
} else {
if ( point1->z > point2->z ) {
return 1;
else if ( point1->z < point2->z ) {
return -1;
} else {
return 0;
}
}
}
}另外,请不要硬编码点数,而是计算你读入的点数。
https://stackoverflow.com/questions/16158601
复制相似问题