Thread: Simplifying Fractions

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    7

    Simplifying Fractions

    Hello,

    I appreciate that this is probably a stupid question but I'm new to programming so please go easy on me!

    I'm writing a program that finds the gcd of two numbers then returns a simplified fraction. I have a main function and a gcd function. The gcd function is done and is fine, but I don't know how to use the result in my gcd function in my main function to divide the numerator and denominator by the gcd! I don't even know if that's what I'm supposed to do!

    Any help would be awesome. Thanks.

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Can you post the code you currently have?
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You can return one value from the gcd function. Any other number you want changed in the gcd function, with lasting effect, will have to be a number brought into the gcd function via it's address. The & in front of the variable name, means "the address of"the variable:

    my_variable = a value
    &my_variable = an address
    Code:
    my_gcd_function(number1, number2, &myNumerator, &myDenominator);
    Would call a function, and pass two numbers (1 and 2) that could be changed, but the change will not have any effect after the function ends, and two addresses to numbers that, once changed in the function, will remain changed when the function ends.

    Making your gcd function first line, (definition), to look like:
    Code:
    void my_gcd_function(int number1, int number2, *myNumerator, *myDenominator) {
      //add your gcd code here
    }
    And Welcome to the forum, Kerniga!

    If you could post your code, with some example numbers, we can be much more specific.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    7
    Thanks for the warm welcome! This is my code so far:



    #include <stdio.h>


    int gcd(int a, int b);

    int main(void)

    {

    int num, den;

    printf("Please enter a numerator and denominator: ");
    scanf("%d%d\n", &num, &den);





    printf("Simplified fraction: %d %d", );

    return 0;

    }


    int gcd(int a, int b)

    {

    int rem;

    if((rem = a % b) == 0) {
    return b;
    }
    else {
    return gcd(b, rem);
    }

    }

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    7
    sorry, I think I was supposed to use this!:

    Code:
    #include <stdio.h>
    
    
    int gcd(int a, int b);
    
    int main(void)
    
    {
    
       int num, den;
    
       printf("Please enter two integers:  ");
       scanf("%d%d\n", &num, &den);
    
       
    
       printf("Simplified fraction:  ");
    
       return 0;
    
    }
    
    
    int gcd(int a, int b)
    
    {
       
       int rem;
    
       if((rem = a % b) == 0) {
          return b;
       }
       else {
          return gcd(b, rem);
       }
    
    }

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    So in main you just need to invoke gcd with the parameters you have input-ed via scanf.

    Code:
    printf("%d",gcd(num,den));
    However, first you want to verify that you are taking input correctly.
    Then, test your program to see if your gcd function works correctly with different values.

    Then, you are ready to print your simplified function, which I am assuming is obtained as follows:

    Code:
    int my_gcd = gcd(num,den);
    
    int simple_num = num / my_gcd;
    int simple_den = den / my_gcd;
    printf("%d / %d",simple_num,simple_den);
    Last edited by claudiu; 10-20-2010 at 07:41 AM.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    7
    Done it, and it works!! Thanks so much!

  8. #8
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Good job, glad to be of help.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fractions
    By zdream8 in forum C Programming
    Replies: 2
    Last Post: 05-21-2008, 09:54 PM
  2. Algebraic Fractions
    By treenef in forum C++ Programming
    Replies: 8
    Last Post: 12-20-2005, 05:10 AM
  3. Greatest Common Factors of Fractions
    By sonict in forum C++ Programming
    Replies: 1
    Last Post: 01-15-2003, 04:33 AM
  4. Simplifying fractions in c++
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 09-01-2002, 06:58 PM
  5. Fractions
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2002, 07:51 AM