Hello I have wrriten a recursive program that will display a fraction, where the numerator is less than the denominator, out to a user specificed number of decimal places. My program works for numbers like 1/7 35/46 etc. But it doesnt work for some cases. If i try to output 1/10 or 1/100 it doesnt display the one, it will out put 0.00 and not the 1 that should be at the end. I also get a stack overflow when i try to do 2/8,1/4, and some other cases. I cant figure out why these things are happening. You can run the program yourself and check it out. Any help is appreciated. Thanks
Code:// This is the main project file for VC++ application project // generated using an Application Wizard. #include "stdafx.h" #using <mscorlib.dll> using namespace System; void divide(int,int,int,Int32 []); void print(Int32[]); int _tmain() { int n, d,size,i=0; //Variable declarations, i is index for array //Read In Information From User Console::Write(S"Input A Numerator:"); n=Int32::Parse(Console::ReadLine()); Console::Write(S"Input A Denominator(Bigger Than Numerator):"); d=Int32::Parse(Console::ReadLine()); Console::Write(S"Enter number of Decimal Places Desired:"); size=Int32::Parse(Console::ReadLine()); //Store The Answer In This Array Int32 a[]=new Int32[size]; //Compute & Output Answer Console::Write(S"The answer is 0."); divide(n,d,i,a); print(a); return 0; } //Recursive Division Function void divide(int n,int d, int i, Int32 a[]) { if(n<d) return divide(n*10,d,i,a); if(n>d && i<a->Length) { a[i]=n/d; return divide(n%d,d,i+1,a); } } //Print Array void print(Int32 d[]) { for (int i=0; i!=d->Length; i++) Console::Write(S"{0}", d[i].ToString()); Console::WriteLine(); }



LinkBack URL
About LinkBacks


