Thursday, July 30, 2009

Help. turbo c program!! ASAP!?

i have this program with me that will (supposedly) convert a rational number to a factorial. here is the code:


#include%26lt;stdio.h%26gt;





int quo,prod;


float point;


int multiply(int quotient){


int i=0,k=0;


k=fact2(quotient);


do{


prod=k*i;


i++;


}while(prod%26lt;=quotient);


prod=prod-(k);


i=i-2;


return(i);


}





int fact2(int quotient) {


int f = 1,i=0;


do {


i++;


f=f*i;


}while(f%26lt;=quotient);


f=f/(i--);


return(f);


}





int factorial(int quotient) {


int f = 1,i=0;


do {


i++;


f=f*i;


}while(f%26lt;=quotient);


i--;


return(i);


}





main() {


int a,b,f;


double ans,anss;


clrscr();


printf("Input rational: ");


scanf("%d/%d",%26amp;a,%26amp;b);


ans=(float)a/b;


printf("%d/%d = %f\n",a,b,ans);


if(ans%26gt;0)


printf("\n[0,[");


else{


printf("\n[1,[");


anss=ans*-2;


ans=ans+anss;


}


quo=ans;


point=ans-(quo+0.0);


loop(quo);


getch();


loop2(point);


getche();


}








int loop() {


int fact,mul;


while(fact%26gt;=1){


fact=factorial(quo);


mul=multiply(quo);


printf("%d,",mul);


quo=quo-prod; }


printf("],["); }

Help. turbo c program!! ASAP!?
int factorial2(float poinnt)


{


int f = 1,i=0;


do {


i++;


f=f*i;


}while(f%26lt;=point);


i--;


return(i); }





Right here, in factorial2(), the paramenter is "poinnt" -- which is never used in/by the function. The "point" used by the function is the global declared near the top of your code:





int quo,prod;


float point;





...bet that's why you're not getting expected results.





TIP1: if you turn up the warning level your compiler, then it should warn you about unused parameters like "poinnt", and it'll help you find these problems more rapidly/easily.





TIP2: this is why i always avoid use of globals whenever possible.

rosemary

No comments:

Post a Comment