Thread: Need some help to symply a fraction

  1. #1
    Registered User
    Join Date
    Jun 2012

    Need some help to symply a fraction

    Hello to everyone as I am new and this is my first post.
    My mother lenguage isn´t english but I´ll try my best to explain my self .
    I am doing this program in which there is a function which is supposed to symplify a fraction such as 22/24 into 11/12.
    The program doesn´t give any compilation errors but it only executes well when you introduce fractions like 24/36 and turns them into 2/3 because the highest common factor of both is found at the end of their vectors (va[c] and vb[c1]).
    #define d 15#include <stdio.h>
    int function1(int a, int b){
    int va[d]={0}, vb[d]={0}; int i=0,j=0,c=0,c1=0,r,y;        
    for(i=1;i<d;++i){if(a%i==0) {va[c]=i;++c;}
                     if(b%i==0) {vb[c1]=i;++c1;}}
    printf("\nThe factor vectors are:\n");
    for(i=0;i<d;++i)printf(" %d",va[i]); putchar('\n');
    for(i=0;i<d;++i)printf(" %d",vb[i]);
    printf("\nValue in c: %d and in c1: %d",c, c1);
    /*This is where the problem must be*/
    i=c-1; j=c1-1;
                             if(va[i]==vb[j]) return va[i];
                             else printf("\n\nva[%d]=%d   !=   vb[%d]=%d",i,va[i],j,vb[j]);
    /*It should never return '98'. it should have returned '1'
     before becouse '1' is a factor of every number */
    return 98;
    int main(){int num, den,r;
    printf("Introduce your fraction´s numerator: ");
    scanf("%d", &num);
    printf("Introduce your fraction´s denominator: ");
    scanf("%d", &den);
    r=function1(num, den);
    printf("\nYour symplified fraction is: %d/%d", num/r, den/r);
    printf("\nResult picked up from function1 is: %d", r);
    return 0;}
    If this doubt I have has been discussed before please just send me to that thread.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    It might help for you to search the Web for "Euclid's algorithm".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2012
    That was a great idea after reeding about that magical-mathematical trick Euclid's algorithm.
    I changed my function for this one:
    int function1(int a, int b){
        if(a>b) a=a-b;
        if(b>a) b=b-a;
    	return a;

    thank you laserlight for your help!

  4. #4
    Registered User
    Join Date
    Dec 2011
    If I'm not mistaken, all that code does is find the greatest common factor (so you have to do the division after calling it).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to solve with fraction
    By fong1213 in forum C Programming
    Replies: 2
    Last Post: 08-30-2008, 01:42 AM
  2. Replies: 18
    Last Post: 03-26-2008, 09:01 AM
  3. Help with fraction problem
    By jrahhali in forum C++ Programming
    Replies: 4
    Last Post: 04-04-2004, 10:37 PM
  4. fraction help
    By abrege in forum C++ Programming
    Replies: 4
    Last Post: 12-12-2002, 05:44 PM
  5. Fraction
    By Nicknameguy in forum C++ Programming
    Replies: 5
    Last Post: 11-19-2002, 11:06 AM