1

我编写了一个程序来查找数组中的最大数。问题是每次find_largest递归调用函数时,largest变量似乎都被内存中其他地方的垃圾填充了。我已经用调试器逐步完成了它,在递归调用之前它似乎工作正常。数组的指针和对 的更新largest(如果适用)显示预期值。

/*This program will find the largest integer in an array. It was written to practice
using recursion.*/

#include <stdio.h>
void find_largest(int *a, int n);
int main() {
    int a[] = {10, 27, 101, -8, 16, 93}; 
    int n = 6, i = 0;
    printf("Current array: ");
    while(i < n) {//print array
        printf("%d ", a[i]);
        i++;
    }
    find_largest(a, n);
    return 0;
}//end main

//This function will start at the last element, and then step through the array 
//in reverse order using pointers. 
void find_largest(int *a, int n) {  //formulate the size-n problem.
    int largest = 0;
    if(n == 0) {    //find the stopping condition and the corresponding return
        printf("\nThe largest number is: %d \n", largest);
    }
    else { //formulate the size-m problem.
        n--; //decrement so that the proper number is added to pointer reference
        if(largest <= *(a + n)) //check if element is larger
            largest = *(a + n); //if larger, assign to largest
        find_largest(a, n); //recursive call
    }
}

程序返回零作为最大整数。有任何想法吗?

4

4 回答 4

6

largest并非所有递归调用都共享,每个调用都有自己的副本。这意味着在基本情况下,您执行以下代码:

int largest = 0;
if (n == 0) { 
    printf("\nThe largest number is: %d \n", largest);
}

largest永远在哪里。_0

您可以制作largest static并且它会起作用,尽管这样做有点奇怪。我宁愿做这样的事情:

int find_largest(int *a, int n)
{
    int subproblem;

    // base case - single element array, just return that element
    if (n == 1)
    {
        return *a;
    }

    // recursion - find the largest number in the rest of the array (increase 
    // array pointer by one, decrease length by one)
    subproblem = find_largest(a + 1, n - 1);

    // if the current element is greater than the result of the subproblem,
    // the current element is the largest we've found so far - return it.
    if (*a > subproblem)
        return *a;

    // otherwise, return the result of the subproblem
    else
        return subproblem;
}
于 2013-06-13T04:42:02.560 回答
3

largest0在每个单独的函数调用中初始化为,这是一个快速修复:

int find_largest(int *a, int n) {  //formulate the size-n problem.
    static int largest = 0;
    if(!n) {    //find the stopping condition and the corresponding return
        int answer = largest;
        largest = 0;
        return answer;
    }
    else { //formulate the size-m problem.
        n--; //decrement so that the proper number is added to pointer reference
        if(largest <= *(a + n)) //check if element is larger
             largest = *(a + n); //if larger, assign to largest
        find_largest(a, n); //recursive call
    }
}

static属性告诉编译器您只想初始化变量一次,然后它应该保留它的数据。这将解决您的问题,因为在每次递归调用后,largest不会重置为零。相反,它将包含最后一次调用的值(在本例中为调用函数)。

在函数结束时,您应该重置largest为,0以便在下一次调用中,它仍然不包含上一次调用的值。这也是制作临时变量的原因——这样它就可以在设置为0.

于 2013-06-13T04:46:31.787 回答
0

每次调用 find_largest() 时,都会创建一个局部变量 int maximum 并将其赋值为零。因此,当 n 最终达到零时,它并不真正关心过去 5 次递归调用做了什么,它只是返回您刚刚设置为最大的零。要么使最大的全局变量,要么将其作为参数传递给函数(可能是更好的做法)。

于 2013-06-13T04:44:26.773 回答
0

使int最大= 0;进入静态int最大= 0;它可能会有所帮助。通过添加静态变量最大将在整个递归过程中仅初始化一次。

于 2017-08-30T10:05:11.633 回答