通过C

时间:2018-11-13 23:26:36

标签: c function pointers

我被困于尝试从算法书中实现伪代码。我的代码会编译并打印出正确的答案,只是我要打印的某些信息未正确显示。控制台输出如下所示:

Correct Solution Tested: 
max_left= 7 
max_right= 10 
sum= 43


Failing Outputs:
curr_cross_low = 17; curr_cross_high = -1; curr_cross_sum = 38
curr_cross_low = -1; curr_cross_high = -1; curr_cross_sum = 18
curr_cross_low = 32766; curr_cross_high = -272632720; curr_cross_sum = 43

max_left_full= 32766 
max_right_full= -272632512
sum_full= 43

Program ended with exit code: 0

打印出来的前三个值是正确算法的结果,该结果是通过蛮力实现算法的一部分而得出的。在代码中,这就是函数“ findMaxCrossingSubarray”本身。输出的第二部分是当我执行完整算法“ findMaximumSubarray”时。我认为应该打印出表明正在接近解决方案的结果。变量“ sum_full”给出的最终答案似乎是正确的,因为它与书中所说的蛮力解决方案相匹配。

我一直在尝试找到如何打印正确的max_left_full和max_right_full值,而不是我认为是内存地址的值。我的意思是,如果我在一个地方更改指针,它会使解决方案不正确或打印出内存地址。

有没有一种简单的方法可以找到我可能会丢球的地方?

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

//returns a pointer to a value equal to the set of changes
int * returnPriceChanges(int sz, int A[]){
    int MAX_SIZE = 256;
    static int* C;
    C =  malloc(MAX_SIZE *sizeof(int));
    int i;
    for(i=0;i<sz-1;i++){
        C[i]=A[i+1]-A[i];
    }
    return C;
}

int findMaxCrossingSubarray(int A[], int low, int mid, int high, int* max_left, int* max_right){
    double left_sum = -INFINITY;
    int sum = 0;
    for(int i=mid;i>=low;i--){
        sum=sum+A[i];
        if(sum > left_sum){
            left_sum = sum;
            *max_left = i;
        }
    }
    double right_sum = -INFINITY;
    sum = 0;
    for(int j=mid+1; j<=high;j++){
        sum=sum+A[j];
        if(sum > right_sum){
            right_sum = sum;
            *max_right = j;
        }
    }
    return (*max_left, *max_right, left_sum+right_sum);
}

int findMaximumSubarray(int A[], int low, int high){
    int curr_left_low, curr_left_high, curr_left_sum;
    int curr_right_low, curr_right_high, curr_right_sum;
    int curr_cross_low, curr_cross_high, curr_cross_sum;
    int mid = 0;
    int* temp_max_left, temp_max_right;
    if(high==low){
        return(low, high, A[low]);
    }
    else{
        mid =floor((high+low)/2);
        curr_left_low, curr_left_high, curr_left_sum = findMaximumSubarray(A, low, mid);
        curr_right_low, curr_right_high, curr_right_sum = findMaximumSubarray(A, mid+1,high);
        curr_cross_low, curr_cross_high, curr_cross_sum = findMaxCrossingSubarray(A, low, mid, high, &temp_max_left, &temp_max_right);
        if(curr_left_sum>=curr_right_sum && curr_left_sum>=curr_cross_sum){
            return (curr_left_low, curr_left_high, curr_left_sum);
        }
        else if(curr_right_sum>= curr_left_sum && curr_right_sum>=curr_cross_sum){
            return (curr_right_low, curr_right_high, curr_right_sum);
        }
        else{
            printf("curr_cross_low = %d; curr_cross_high = %d; curr_cross_sum = %d\n", curr_cross_low, curr_cross_high, curr_cross_sum);
            return (curr_cross_low, curr_cross_high, curr_cross_sum);
        }
    }
}

int main(){
    int prices[] = {100,113,110,85,105,102,86,63,81,101,94,106,101,79,94,90,97};
    int szPrices = sizeof(prices)/sizeof(prices[0]);
    int changes[szPrices-1];
    int *P;
    P = returnPriceChanges(szPrices,prices);
    //set C = to list of changes
    for(int i=0; i<szPrices-1; i++){
        changes[i]=*(P+i);
    }
    int max_left, max_right, sum;
    max_left, &max_right, sum = findMaxCrossingSubarray(changes, 0, 8, 16, &max_left, &max_right);
    printf("\nCorrect Solution Tested: \nmax_left= %d \nmax_right= %d \nsum= %d\n\n", max_left, max_right, sum);
    printf("\nFailing Outputs:\n");
    int max_left_full, max_right_full, sum_full;
    max_left_full, &max_right_full, sum_full = findMaximumSubarray(changes, 0, 16);
    printf("\nmax_left_full= %d \nmax_right_full= %d\nsum_full= %d\n\n", max_left_full, max_right_full, sum_full);
    return 0;
}

1 个答案:

答案 0 :(得分:1)

您不能从C中的函数返回元组。当您在C中使用逗号分隔值时,整个表达式将仅求值最后一个成员。

所以当您写:

a, b, c = some_function();

这真的意味着:

/* do nothing */, /* do nothing */, c = some_function();

如果要返回复合数据结构,请使用struct,即

struct subarray
{
    int low;
    int high;
    int sum;
};

void findMaximumSubarray(int A[], int low, int high, struct subarray * result);

如果结构很小,并且您使用的是现代编译器,并且未在嵌入式系统上运行,那么您还可以按值返回该结构:

struct subarray findMaximumSubarray(int A[], int low, int high);

后一种语法简化了用法,但是如果您以这种方式启动returning huge structs,则可能成为问题。