Welcome Guest ( Log In | Register )

Outline · [ Standard ] · Linear+

 C++ need help, my asgmt halfway =\

views
     
TSdirtinacan
post Jan 25 2006, 09:44 PM, updated 20y ago

Casual
***
Junior Member
381 posts

Joined: Jan 2003


hi all.so,this is my asgmt.need to use the binomial theorem.

http://en.wikipedia.org/wiki/Binomial_theorem
here a link about the theorem

but i only need to the the (a+b)^n
where a=1 and n is a integer and 0<b<1

here's my code.can't seem to execute properly.no programming error.i think so la blush.gif

CODE
#include <stdio.h>
#include <math.h>


double facto (double fac);
double kire_2(int a,double b,int n);

double kire_2(int a,double b,int n)
{
      double sum1=0,sum2=0,count=0,temp_m=1,temp_n;
      for(temp_n=1;count>=0;count++)
      {
          sum1+=temp_m*pow(a,n-count)*pow(b,count);
          temp_m*=(n-count)/facto(count+1);
         
          if(n-count==count)
          return sum1+sum2;
         
          sum2+=temp_n*pow(a,count)*pow(b,n-count);
          temp_n*(n-count)/facto(count+1);
      }      
}

double facto (double fac)
{
      double bogey;
      if(fac==0)
          return 1;
      for(bogey=1;fac>=2;fac--)
          bogey*=fac;
      return bogey;
}

int main()
{
   int n,a=1;
   double b;
   printf("Enter b and n\n");
   scanf("%lf %d",&b,&n);
   printf("(%d+%.2lf)^%d",kire_2(a,b,n));
   scanf("lf",&n);
   
   return 0;
}
(use the CODE bbtag in the future when quoting code -wKkaY)

hope u all can help me biggrin.gif

This post has been edited by wKkaY: Jan 27 2006, 07:38 PM
Geminist
post Jan 25 2006, 11:20 PM

- ドSな彼女 -
Group Icon
VIP
2,928 posts

Joined: Mar 2005
Wrong section, thread will be moved to Code Masters smile.gif
H@H@
post Jan 26 2006, 02:40 AM

I'M THE TEAMKILLING F***TARD!!!
Group Icon
VIP
6,727 posts

Joined: Jan 2003
From: 6 feet under at Bloodgulch Outpost Alpha Number 1



QUOTE(dirtinacan @ Jan 25 2006, 09:44 PM)
      for(temp_n=1;count>=0;count++)
*
Since its almost 3 in the morning, I won't look through properly... But I did notice this... Its an infinite loop as you'll never hit your delimiter (Since count will always be positive)
TSdirtinacan
post Jan 27 2006, 11:38 AM

Casual
***
Junior Member
381 posts

Joined: Jan 2003


QUOTE(H@H@ @ Jan 26 2006, 02:40 AM)
Since its almost 3 in the morning, I won't look through properly... But I did notice this... Its an infinite loop as you'll never hit your delimiter (Since count will always be positive)
*
yup.it's an infinite loop.i purposely did that so it will do the equation from left and right until in the end will get same term and will sum up both left and right smile.gif
anthony_yio
post Jan 27 2006, 02:19 PM

........
Group Icon
Elite
1,828 posts

Joined: Jan 2003


QUOTE(dirtinacan @ Jan 27 2006, 11:38 AM)
yup.it's an infinite loop.i purposely did that so it will do the equation from left and right until in the end will get same term and will sum up both left and right smile.gif
*
Consider of using break or continue.
Read any C++ programming book for what it does. Or modify your for loop condition instead of making it infinite. You can actually have multiple condition expression
e.g.
for(int p = 0; p < 10, i < 5; p++)

or even ignore it entirely

e.g.
for(int p = 0; ; p++)

I am not sure how proficient of you to your IDE. For simple debugging, you can printf all the value out to justify which one is the error. It is not difficult anyway.





Avex
post Jan 28 2006, 01:40 AM

On my way
****
Junior Member
570 posts

Joined: Jan 2003
From: /k/ isle

give u some hint
use recursive
public static int c(int n, int k)
{
if(k==0 || k==n)
return 1;
return c(n-1, k-1) +c(n-1,k);
}

public static int c(int n, int k)
{
if(n<2 || k==0 || k==n) return 1;
int c=1;
for(int j=1; j<=k; j++)
{ c=c*(n-j+1)/j;
return c;
}


this code may not be relevant to your coding but the concept is almost the same...go figure. Don't ask me for any explanation ok wink.gif

This post has been edited by Avex: Jan 28 2006, 01:54 AM
TSdirtinacan
post Jan 29 2006, 09:28 PM

Casual
***
Junior Member
381 posts

Joined: Jan 2003


avex,is that for the factorial?

CODE

#include <stdio.h>
#include <math.h>


double facto (double fac);
double kire_2(int a,double b,int n);

double kire_2(int a,double b,int n)
{
      double sum1=0,sum2=0,count=0,temp_m=1,temp_n;
      for(temp_n=1;count>=-1;count++)
      {
          sum1+=temp_m*pow(a,n-count)*pow(b,count);
          temp_m*=(n-count)/facto(count+1);
         
          if((n-count)==count)
          return sum1+sum2;
         
          sum2+=temp_n*pow(a,count)*pow(b,n-count);
          temp_n*=(n-count)/facto(count+1);
      }      
}

double facto (double fac)
{
      double bogey;
      if(fac==0)
          return 1;
      for(bogey=1;fac>=2;fac--)
          bogey*=fac;
      return bogey;
}

int main()
{
   int n,a=1;
   double b;
   printf("Enter b and n\n");
   scanf("%lf %d",&b,&n); //the code executed until here
   printf("(%d + %.2lf)^%d = %lf",a,b,n,kire_2(a,b,n)); //this one doesn't show
   scanf("lf",&n);
   
   return 0;
}





This post has been edited by dirtinacan: Jan 29 2006, 09:29 PM
TSdirtinacan
post Jan 29 2006, 09:35 PM

Casual
***
Junior Member
381 posts

Joined: Jan 2003


the factorial function is alrite.nothing wrong.i dunno wat's wrong sad.gif(((
TSdirtinacan
post Jan 30 2006, 10:26 AM

Casual
***
Junior Member
381 posts

Joined: Jan 2003


finished!
tq all whistling.gif thumbup.gif flex.gif

 

Change to:
| Lo-Fi Version
0.0184sec    0.71    5 queries    GZIP Disabled
Time is now: 23rd December 2025 - 02:33 AM