Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 使用它们的比率对数组进行排序_C_Sorting - Fatal编程技术网

C 使用它们的比率对数组进行排序

C 使用它们的比率对数组进行排序,c,sorting,C,Sorting,我需要一些帮助,使一个函数,将排序2数组 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':

我需要一些帮助,使一个函数,将排序2数组

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");
}
void三变量(int**a,int**c,int n){
int i,j,temp,tempa,tempc;
int*比率=malloc(n*sizeof(int));

因为(i=0;i多亏了@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");
}
void三变量(int*a,int*c,int-n){
int i,j,temp,tempa,tempc;
整数比[n];

对于(i=0;iA指向数组的指针与指向指针的指针不同。而且不需要向数组传递指针,因为数组自然会衰减为指针(指向它们的第一个元素)。也就是说,如果在需要
int*
时传递例如
a
,则自动传递的是
&a[0]
。请注意,您存在内存泄漏,因为您从未将
ratio
传递给
free
。因为C(自C99标准)允许可变长度数组,所以不需要动态分配它。因此,普通
int ratio[n]
可以正常工作,不会有任何泄漏。最后,你将超出数组
a
c
的界限。你迭代了五个元素,但是
a
c
只有四个元素。这当然会导致未定义的行为。@Someprogrammerdude我更正了我的代码中的最后两个注释,但我需要一些澄清关于第一个问题,我在理解指针如何工作方面仍然有一些大问题,所以我尝试应用我在本例中编写的前一个程序上的工作原理,但没有按计划进行。基本上,我使用**a和**c,因为我希望这是通过函数更改数组的唯一方法,我使用了*a[i]*c[i]每次我在函数中使用它们,因为这就是我在编译时不会出错的原因。我做错了什么?@Someprogrammerdude我太笨了,我就知道了!非常感谢:D