Fraction outputted as decimal

This is a discussion on Fraction outputted as decimal within the C++ Programming forums, part of the General Programming Boards category; Hello I have wrriten a recursive program that will display a fraction, where the numerator is less than the denominator, ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    41

    Fraction outputted as decimal

    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();
    }

  2. #2
    Registered User
    Join Date
    May 2005
    Location
    Germany
    Posts
    12
    Quote Originally Posted by blindman858
    void divide(int,int,int,Int32 []);
    void print(Int32[]);

    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);
    }
    }
    [/CODE]
    Hi,

    I think you should try dividing two Doubles and not Integers. Your array should also be Double.
    If you are trying to do something like
    Code:
    int a = 1/10;
    the compiler will do 1/10 = (int) 0,1 = 0

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    41
    If i use two doubles I cant use the modulus operator.

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    41
    Ok I figured out why numbers like 1/100 wont print correctly. Its not the data type actually. I have to add another case in the recursion,

    Code:
    if(n==d)
    a[i]=1;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with fraction division function
    By trippedwire in forum C++ Programming
    Replies: 6
    Last Post: 10-24-2004, 12:38 PM
  2. fractions
    By joeshmoe1337 in forum C Programming
    Replies: 21
    Last Post: 09-11-2004, 02:34 AM
  3. Overloading insertion and extraction problem
    By Curwa in forum C++ Programming
    Replies: 1
    Last Post: 01-15-2003, 09:20 PM
  4. Help with Fraction class
    By cheeisme123 in forum C++ Programming
    Replies: 0
    Last Post: 06-04-2002, 08:48 AM
  5. reducing a fraction
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 03-13-2002, 08:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21