Cagri Candan
Cagri Candan

Reputation: 90

recursive function code::blocks vs. visual studio 2008

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

I wrote this program on code blocks 12.11 and its works properly, but when i compiled the program on visual2008 i got an error with recursivesum. It always gives me same result. Thanks for your help.

Upvotes: 0

Views: 406

Answers (2)

BLUEPIXY
BLUEPIXY

Reputation: 40145

recersivemax(length,len-1);//my recursive fonction it call itself
...
recersivesum(length,len-1);// my recursive fonction it call itself anless len goes to 0

should be

return recersivemax(length,len-1);//my recursive fonction it call itself
...
return recersivesum(length,len-1);// my recursive fonction it call itself anless len goes to 0

and

sum+=length[len-1];//when len == 0 , It has access an array out of range
if(len==0){return sum;}

should be

if(len==0){return sum;}
sum+=length[len-1];

you have the same problem in max version.

Upvotes: 0

ciphermagi
ciphermagi

Reputation: 748

Okay, for one thing, you're not returning anything from main(). You should always return from main(). For another, you should never ever call main(). If you need an infinite loop, use while(1) with an escape sequence inside.

Also, you're calculating, but not returning. You need to give something back on your other functions, too, or else yo lose the values entirely. The sum function, for example, gives back the sum on the top of the stack, but once that stack pops, every sum function under that pop will pop without returning anything at all.

Upvotes: 1

Related Questions