下面的函数按体积对我的结构进行排序。
void selection_sort(struct cylinder my_cylinders[], int n)
{
int i, largest = 0;
double temp;
if (n == 1)
return;
for (i = 1; i < n; i++)
if (my_cylinders[i].volume > my_cylinders[largest].volume)
largest = i;
if (largest < n - 1) {
temp = my_cylinders[n - 1]; // ** Line 77
my_cylinders[n - 1] = my_cylinders[largest];
my_cylinders[largest] = temp; // ** Line 79
}
但是当我尝试编译时,我得到以下错误:
cylinders.c:77: error: incompatible types when assigning to type ‘int’ from type ‘struct cylinder’
cylinders.c:79: error: incompatible types when assigning to type ‘struct cylinder’ from type ‘int’
这是我的结构
struct cylinder {
double radius;
double height;
double weight;
double volume;
};
主要
int i, counter = 0;
fprintf(cFileOut, "# Radius Height Volume Weight\n");
for (i = 0; i < 6; i++)
{
while (fscanf(cFileIn, "%lf, %lf, %lf\n", &radius, &height, &weight) != EOF)
{
my_cylinders[counter].radius = radius;
my_cylinders[counter].height = height;
my_cylinders[counter].volume = volume;
my_cylinders[counter].weight = weight;
volume = PI * radius * radius * height;
fprintf(cFileOut, "%-d\t %-12.6lf\t %-12.6lf\t %-12.6lf\t %-12.6lf \n",
counter, radius, height, volume, weight);
counter++;
}
}
selection_sort(my_cylinders, counter);
我想我理解错误但不确定如何解决它们。我尝试过更改类型,但在结构方面我遗漏了一些东西。
答案 0 :(得分:0)
将温度类型从en:
activerecord:
errors:
messages:
record_invalid: "Validation failed: %{errors}"
更改为double
。
另请注意,您只是根据卷找到结构中的最大项,并使用最后一个索引进行交换。那不是排序。
要进行排序,必须在循环中调用此函数,同时每次递减计数器1。
像“
struct cylinder
并将函数内的if条件更改为:
while(counter>=0){
selection_sort(my_cylinders, counter);
counter--;
}