英文:
Recursion works in reverse, then how this program printing data in sequence?
问题
递归对我来说总是有困难的。我理解当我们使用递归时,基本上是在创建一些堆栈,创建完所有堆栈后,递归按相反的顺序工作,然后打印结果。
我正在尝试理解下面的这个程序:
#include<stdio.h>
//函数声明
int RecursvFcnTogetPrcntge();
float Var;
unsigned int count=1;
float Prcntge;
int main()
{
printf("Enter a value to split in percentage: ");
scanf("%f",&Var);
Var=Var/100;
RecursvFcnTogetPrcntge();
return 0;
}
int RecursvFcnTogetPrcntge()
{
if(count==3)
{
return 1;
}
Prcntge=Var*count;
printf("\n%3d Percent = %.02f",count, Prcntge);
count++;
RecursvFcnTogetPrcntge();
return 0;
}
我们有一个计数器count为3,这意味着我们将创建4个内存堆栈。一个用于main函数,两个用于else分支,一个用于if (count == 3) 返回1。
如果它走到了最后一个堆栈(其中count为3),那么它会让我们返回1。现在它返回了1,我们处于第三个堆栈中,在第三个堆栈中有一个打印语句,应该打印出计数器为2和相应数字的百分比。然后它将依次返回到第二个和第一个堆栈。
因此,输出应该是:
2 Percent = 0.14
1 Percent = 0.07
但为什么它输出:
1 Percent = 0.07
2 Percent = 0.14
如果递归是反向工作的,那么它是如何按顺序调用函数的?
也许我对这个概念理解不够清楚,但这是我对递归的理解。我会感激任何帮助,我在这方面遇到了困难。
英文:
Recursion is always trouble to me. I understand that when we use recursion we are basically creating some stack and after creating all of the stack, recursion works in reverse order then print the result.
I am trying to understand this program bellow
#include<stdio.h>
//function declaration
int RecursvFcnTogetPrcntge();
float Var;
unsigned int count=1;
float Prcntge;
int main()
{
printf("\nEnter a value to split in percentage: ");
scanf("%f",&Var);
Var=Var/100;
RecursvFcnTogetPrcntge();
return 0;
}
int RecursvFcnTogetPrcntge()
{
if(count==3)
{
return 1;
}
Prcntge=Var*count;
printf("\n%3d Percent = %.02f",count, Prcntge);
count++;
RecursvFcnTogetPrcntge();
return 0;
}
We have count 3, which means we are going to create 4 stacks of memory. 1 for main, 2 for else, 1 for if (count == 3) then return 1.
if it goes to the last stack(where count is 3) then it will say us to return 1.
Now it returns 1 and we are in the third stack, in the third stack there is a print which supposed to print count 2 and percentage of the number. after the third stack it will come to second and first stack respectively
Therefore, the output should look like
2 Percent = 0.14
1 Percent = 0.07
but why it is outputting
1 Percent = 0.07
2 Percent = 0.14
if its recursion work backward then how its calling the function in a sequential order?
I am not maybe clear about the idea but this is what I understand about the recursion. I will appreciate any help. I in trouble with this.
答案1
得分: 2
递归不是按照“倒序”工作的,它按照你递归调用函数的顺序工作。在这种情况下,你在递归调用函数之前调用了打印语句
int RecursvFcnTogetPrcntge()
{
if(count==3)
{
return 1;
}
...
printf("\n%3d Percent = %.02f",count, Prcntge); // 这部分先执行
...
RecursvFcnTogetPrcntge(); // 这部分其次执行
...
}
因此,它会先打印当前递归帧,然后移动到下一帧。如果你在递归调用后放置了一个打印语句,你会看到它以相反的顺序执行。
递归并没有什么特殊之处,它就像任何其他函数调用一样。当你调用它时,它会完全执行函数,然后返回。只是有时递归调用可以一次又一次地调用自身。在调用之前的任何内容都会在调用之前执行,而在调用之后的任何内容都会在调用之后执行。
此外,while、if、for 和 else 语句不会创建堆栈帧。
英文:
Recursion does not work in "reverse", it works in whatever order you call the function recursively. In this case you call print before you call the function recursively
int RecursvFcnTogetPrcntge()
{
if(count==3)
{
return 1;
}
...
printf("\n%3d Percent = %.02f",count, Prcntge); // THIS COMES FIRST
...
RecursvFcnTogetPrcntge(); // THIS COMES SECOND
...
}
So it will print the current frame of recursion, and then move to the next frame. If you had put a print statement after the recursive call, you would see it execute in reverse.
Recursion isn't special, it's just like any other function call. When you call it, it executes the function completely, and then returns. It's just that sometimes that recursive call can call itself again and again. Anything before a call will execute before the call, and anything after the call will execute after the call
Also, while, if, for, and else statements don't create stack frames.
答案2
得分: 1
给定:
typedef struct node
{
int value;
struct node* next; // a null value here marks the end of the list.
} Node;
考虑以下两个递归函数,它们分别打印一个链表:
// 以正序打印链表
void print(Node* list)
{
if (list == NULL) return;
printf("%d\n", list->value);
print(list->next);
}
void printReverse(Node* list)
{
if (list == NULL) return;
printReverse(list->next);
printf("%d\n", list->value);
}
第一个函数打印参数的值,然后递归打印剩余部分的链表。它以正序打印节点的值,从第一个节点到最后一个节点。
第二个函数在递归调用自身的同时,创建堆栈帧,并在到达链表末尾时,在堆栈展开时打印每个节点的值(以相反顺序)。
递归可以在任何方向上工作。
英文:
Given:
typedef struct node
{
int value;
struct node* next; // a null value here marks the end of the list.
} Node;
Consider the following two recursive functions, each of which prints a linked list.
// prints list in forward order;
void print(Node* list)
{
if (list == NULL) return;
printf("%d\n", list->value);
print(list->next);
}
void printReverse(Node* list)
{
if (list == NULL) return;
printReverse(list->next);
printf("%d\n", list->value);
}
The first function prints the value of the argument, and then recursively prints the rest of the list. It prints the node values in forward order, from the first node to the last.
The second function recursively calls itself, creating stack frames as it goes, and then, when it reaches the end of the list, prints the value of each node as the stack unwinds (in reverse order).
Recursion will work in either direction.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论