gpt4 book ai didi

c - C中通过函数跟踪指针对象的方法

转载 作者:太空宇宙 更新时间:2023-11-03 23:38:20 24 4
gpt4 key购买 nike

我一直在尝试实现算法书中的伪代码。我的代码编译并打印出正确的答案,除了我想打印的一些信息没有正确显示。控制台输出如下所示:

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;
}

最佳答案

您不能从 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,它可能会成为一个问题这样。

关于c - C中通过函数跟踪指针对象的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53290989/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com