gpt4 book ai didi

c - 递归函数代码::blocks 与 Visual Studio 2008

转载 作者:行者123 更新时间:2023-11-30 15:42:04 24 4
gpt4 key购买 nike

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

int recersivemax(int length[9],int len);//the prottotype for max fonction
int recersivesum(int length[9],int len);// the prortoty for sum fonction
void recursivemenu();

int main()
{
int x;
recursivemenu();//we call recursive menu as requirement as us
printf("\nif you want to exit press 3 otheriwise program will continue\n");
scanf("%d",&x);// 3 for the exit oprion other wise program call main and start itself
if(x==3){
printf("\nthanks:\n");
}else
main();
}

void recursivemenu(){

int length[10];//an array for our eguation
int i=0;
int selector=0;

printf("please enter integer length of '10':\n");
while(i<10)//we get the array numbers 0 to 9
{
scanf("%d",&length[i]);

i++;
}

printf("which program you wantto execute:\nform maximum fonction press 1: \n function press 2:\n ");
scanf(" %d",&selector);// selector fonction for determinening either sum fonction or max fonction
if(selector==1){
printf("you entered max fonction:\n");

printf("%d",recersivemax(length,10));}
else if(selector==2){
selector=0;
printf("you enetered sum foncion:\n");
printf("%d",recersivesum(length,10));
}
}

int recersivemax(int length[10],int len){//the explanation of max fonction

static int max=0;// we must use static int other wise program forget the all number and start with initial number which is 0

if(length[len-1]>max){
max=length[len-1];
}

if(len==0){return max;}// when ist stop countin len==0 program return the max value

recersivemax(length,len-1);//my recursive fonction it call itself
}

int recersivesum(int length[10],int len){// the recursive foction for summing numbers

static int sum=0;// we must termine statiic int because we need the preveus value of number
sum+=length[len-1];
if(len==0){return sum;}
recersivesum(length,len-1);// my recursive fonction it call itself anless len goes to 0
}

我在代码块 12.11 上编写了这个程序,它工作正常,但是当我在 Visual2008 上编译该程序时,出现了递归和错误。它总是给我相同的结果。感谢您的帮助。

最佳答案

好吧,一方面,您没有从 main() 返回任何内容。您应该始终从 main() 返回。另一方面,您永远不应该调用 main()。如果您需要无限循环,请使用 while(1) 并在内部添加转义序列。

此外,您正在计算,但没有返回。你也需要在你的其他职能上回馈一些东西,否则你就会完全失去值(value)。例如,sum 函数返回堆栈顶部的总和,但是一旦该堆栈弹出,该弹出窗口下的每个 sum 函数都会弹出,而不返回任何内容。

关于c - 递归函数代码::blocks 与 Visual Studio 2008,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20339654/

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