pls check it for me

vipul15singh's Avatar, Join Date: Feb 2010
Light Poster
there is a exponential program, it has some problem in result {if x=1 and n=5 the answer must be 2.7016}
pls tell me the problem and solution also

Code:
#include<stdio.h>
#include<conio.h>
float expo(float,float,int,float);
float term(float,float,float);
main()
{
 int n;
 float e=0.0,t=1.0,x;
 static float s=1.0;
 clrscr();
 printf("\nenter variable & no ofterms:=");
 scanf("%f%d",&x,&n);
 e=expo(x,t,n,s);
 printf("\nsum=%f",e);
 getch();
 return;
}
float expo(float x,float t,int n,float s)
{
 float c=1.0,e=0;
 if(t<n)
 {
  e=term(x,t,c);
  s=s+e;
  expo(x,t+1.0,n,s);
 }
  return(s);
}
float term(float x,float t,float c)
{
 if(t>=1)
 {
  c=c*(x/t);
  term(x,t-1,c);
 }
  return(c);
}

Last edited by shabbir; 6Mar2010 at 19:31.. Reason: Code blocks
0
virxen's Avatar, Join Date: Nov 2009
Pro contributor
Quote:
Originally Posted by vipul15singh View Post
there is a exponential program, it has some problem in result {if x=1 and n=5 the answer must be 2.7016}
pls tell me the problem and solution also

Code:
#include<stdio.h>
#include<conio.h>
float expo(float,float,int,float);
float term(float,float,float);
main()
{
 int n;
 float e=0.0,t=1.0,x;
 static float s=1.0;
 clrscr();
 printf("\nenter variable & no ofterms:=");
 scanf("%f%d",&x,&n);
 e=expo(x,t,n,s);
 printf("\nsum=%f",e);
 getch();
 return;
}
float expo(float x,float t,int n,float s)
{
 float c=1.0,e=0;
 if(t<n)
 {
  e=term(x,t,c);
  s=s+e;
  expo(x,t+1.0,n,s);
 }
  return(s);
}
float term(float x,float t,float c)
{
 if(t>=1)
 {
  c=c*(x/t);
  term(x,t-1,c);
 }
  return(c);
}

check the result again.i think it must be 2.7083

i made 2 corrections and also i changed how scanf gets its data.

Code:
#include<stdio.h>
#include<conio.h>
float expo(float,float,int,float);
float term(float,float,float);
int main()
{
 int n;
 float e=0.0,t=1.0,x;
 static float s=1.0;
 //clrscr();
 printf("\nenter variable:");
 scanf("%f",&x);
  printf("\nenter no of terms:");
 scanf("%d",&n);
 e=expo(x,t,n,s);
 printf("\nsum=%f",e);
 getch();
 return 0;
}
float expo(float x,float t,int n,float s){
 float c=1.0,e=0;
 if(t<n){
  e=term(x,t,c);
  s=s+e;
  t++;
  s=expo(x,t,n,s);
 }
  return(s);
}
float term(float x,float t,float c)
{
 if(t>=1){
  c=c*(x/t);
  c=term(x,t-1,c);
 }
  return(c);
}
0
vipul15singh's Avatar, Join Date: Feb 2010
Light Poster
Quote:
Originally Posted by virxen View Post
check the result again.i think it must be 2.7083

i made 2 corrections and also i changed how scanf gets its data.

Code:
#include<stdio.h>
#include<conio.h>
float expo(float,float,int,float);
float term(float,float,float);
int main()
{
 int n;
 float e=0.0,t=1.0,x;
 static float s=1.0;
 //clrscr();
 printf("\nenter variable:");
scanf("%f",&x);
 printf("\nenter no of terms:");
scanf("%d",&n);
 e=expo(x,t,n,s);
 printf("\nsum=%f",e);
 getch();
 return 0;
}
float expo(float x,float t,int n,float s){
 float c=1.0,e=0;
 if(t<n){
  e=term(x,t,c);
  s=s+e;
  t++;
  s=expo(x,t,n,s);
 }
  return(s);
}
float term(float x,float t,float c)
{
 if(t>=1){
  c=c*(x/t);
  c=term(x,t-1,c);
 }
  return(c);
}
thanks virxen, i m very happy to get sol.,pls tell me whats the actual problem in prog at (vipul.fifteen@yahoo.com)
once again thanks
0
virxen's Avatar, Join Date: Nov 2009
Pro contributor
a better solution is this

Code:
#include<stdio.h>
#include<conio.h>
float expo(float,float,int,float);
float term(float,float,float);
int main()
{
 int n;
 float e=0.0,t=1.0,x;
 static float s=1.0;
 //clrscr();
 printf("\nenter variable:");
 scanf("%f",&x);
  printf("\nenter no of terms:");
 scanf("%d",&n);
 e=expo(x,t,n,s);
 printf("\nsum=%f",e);
 getch();
 return 0;
}
float expo(float x,float t,int n,float s){
 float c=1.0,e=0;
 if(t<n){
  e=term(x,t,c);
  s=s+e;
  t++;
  return expo(x,t,n,s);
 }
  return(s);
}
float term(float x,float t,float c)
{
 if(t>=1){
  c=c*(x/t);
  return term(x,t-1,c);
 }
  return(c);
}

the error is that when you have a recursive function like above
you must keep a sequential line between its calls and not to break it.

for instance(each one calls its previous )

4!=3!*4=(2!*3)*4=( (1!*1*2)*3 )*4=((0!*1*2)*3)*4

without the return i added here you always return c as the result which is not
changing.

for more on this check this link
http://www.java2s.com/Tutorial/C/016.../Recursion.htm