使用比率对数组进行排序

3

我需要帮助编写一个函数,可以按以下步骤排序两个数组:

1 - 找到值:ratio = array1[]/array2[]

2 - 对结果进行排序,并根据所得的结果对2个数组进行排序

3 - 确保更改发生在参数中提供的数组上

这是我尝试编写的方式,但是我遇到了错误:

    ||=== Build: Debug in test (compiler: GNU GCC Compiler) ===|
C:\Users\Amine\Desktop\Knapsack\test\main.c||In function 'main':|
C:\Users\Amine\Desktop\Knapsack\test\main.c|40|warning: passing argument 1 of 'triVariable' from incompatible pointer type [-Wincompatible-pointer-types]|
C:\Users\Amine\Desktop\Knapsack\test\main.c|7|note: expected 'int **' but argument is of type 'int (*)[4]'|
C:\Users\Amine\Desktop\Knapsack\test\main.c|40|warning: passing argument 2 of 'triVariable' from incompatible pointer type [-Wincompatible-pointer-types]|
C:\Users\Amine\Desktop\Knapsack\test\main.c|7|note: expected 'int **' but argument is of type 'int (*)[4]'|
||=== Build finished: 0 error(s), 2 warning(s) (0 minute(s), 0 second(s)) ===|
||=== Run: Debug in test (compiler: GNU GCC Compiler) ===|

我找不到解决方法。
void triVariable(int **a, int **c, int n){
    int i, j, temp, tempa, tempc;
    int *ratio = malloc(n*sizeof(int));

    for(i=0;i<n;i++){
        ratio[i]= (*c)[i] / (*a)[i];
    }

    for(i=0; i<n; i++) { 
        for(j=i+1;j<n; j++) {
            if(ratio[j]<ratio[i]) {
                temp=ratio[i];
                ratio[i]= ratio[j];
                ratio[j]= temp;

                tempa=(*a)[i];
                (*a)[i]=(*a)[j];
                (*a)[j]=tempa;

                tempc=(*c)[i];
                (*c)[i]=(*c)[j];
                (*c)[j]=tempc;

            }
        }


    }
}

int main(){
    int n=5;
    int c[]={12,8,2,5};
    int a[]={5,4,1,3};

    triVariable(&a, &c, n);

    printf("C : ( ");
    for(int i=0;i<4;i++){
        printf("%d ", c[i]);
    }
    printf(")\n");
    printf("A : ( ");
    for(int i=0;i<4;i++){
        printf("%d ", a[i]);
    }
    printf(")\n");
}

如果有人能指出我遗漏的东西,那就太好了!

1
指向数组的指针和指向指针的指针不是同一件事。而且,无需传递指向数组的指针,因为数组自然会衰减为指向它们的第一个元素的指针。也就是说,如果在期望 int * 时传递了例如 a,那么自动传递的是 &a[0] - Some programmer dude
1
请注意,您存在内存泄漏问题,因为您从未将“ratio”传递给“free”。首先没有必要动态分配它,因为C语言(自C99标准以来)允许可变长度数组。因此,简单的int ratio[n];就可以正常工作,而且不会有任何泄漏问题。 - Some programmer dude
1
最后,您将会超出数组ac的范围。您迭代了五个元素,但是ac只有四个。这当然会导致未定义的行为 - Some programmer dude
@Someprogrammerdude 我已经纠正了最后两条评论的代码,但我需要对第一条评论进行澄清。我仍然有一些关于指针如何工作的大问题,所以我尝试应用在先前编写的程序中有效的方法,在这种情况下并没有按计划进行。基本上,我使用 **a 和 **c,因为我期望这是通过函数改变我的数组的唯一方法,并且每次在函数中使用 *a[i] *c[i] 时,我都会使用它们,因为这是我在编译时不出错的方式。我做错了什么? - Amine Chentouf
@Someprogrammerdude 我太蠢了,我才明白!非常感谢:D - Amine Chentouf
1个回答

0

感谢 @someprogrammerdude 帮我纠正了代码!

void triVariable(int *a, int *c, int n){
    int i, j, temp, tempa, tempc;
    int ratio[n];

    for(i=0;i<n;i++){
        ratio[i]= (c)[i] / (a)[i];
    }

    for(i=0; i<n; i++) { //On met à jour notre liste d'objets pour qu'elle soit trier du plus grand ratio au plus petit
        for(j=i+1;j<n; j++) {
            if(ratio[j]<ratio[i]) {
                temp=ratio[i];
                ratio[i]= ratio[j];
                ratio[j]= temp;

                tempa=(a)[i];
                (a)[i]=(a)[j];
                (a)[j]=tempa;

                tempc=(c)[i];
                (c)[i]=(c)[j];
                (c)[j]=tempc;

            }
        }
    }
    free(ratio);
}

int main(){
int n=4;
int c[]={12,8,2,5};
int a[]={5,4,1,3};

triVariable(&a[0], &c[0], n);

printf("C : ( ");
for(int i=0;i<n;i++){
printf("%d ", c[i]);
}
printf(")\n");
printf("A : ( ");
for(int i=0;i<n;i++){
printf("%d ", a[i]);
}
printf(")\n");
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接