Need some help to symply a fraction

This is a discussion on Need some help to symply a fraction within the C Programming forums, part of the General Programming Boards category; Hello to everyone as I am new and this is my first post. My mother lenguage isn´t english but I´ll ...

  1. #1
    Registered User
    Join Date
    Jun 2012
    Location
    Madrid
    Posts
    9

    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]).
    Code:
    #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;
    while(i!=0){while(j!=0){
                             if(va[i]==vb[j]) return va[i];
                             else printf("\n\nva[%d]=%d   !=   vb[%d]=%d",i,va[i],j,vb[j]);
                             j=j-1;
                             }
                i=i-1;}
    
    
    /*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
    Location
    Singapore
    Posts
    21,451
    It might help for you to search the Web for "Euclid's algorithm".
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2012
    Location
    Madrid
    Posts
    9
    That was a great idea after reeding about that magical-mathematical trick Euclid's algorithm.
    I changed my function for this one:
    Code:
     
    Code:
    int function1(int a, int b){
    while(a!=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
    Posts
    795
    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, 04:44 PM
  5. Fraction
    By Nicknameguy in forum C++ Programming
    Replies: 5
    Last Post: 11-19-2002, 10:06 AM

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