Thread: Reducing rational numbers - code not working properly

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    17

    Reducing rational numbers - code not working properly

    I have a problem with reducing two rational numbers in the form a/b and need to recude them to form where a and b do not have any common integer dividers.

    I tried something, but it didn't work. Here is the code:

    Code:
    #include <stdio.h>
    int main()
    {
    int i, j, k;
    int a, b;
    scanf("%d/%d", &a, &b);
    if(a > b) j = b;
    else j = a;
    for(i = j;i > 1; i--)
    {
    if(((a % j) == 0) && ((b % j) == 0)) { k = j; break; }
    }
    a = a / j;
    b = b / j;
    printf("%d/%d\n", a, b);
    return 0;
    }
    It always gives me result 1/1. Where is the problem ? I really can't figure it out.
    Thanks in advance.

  2. #2
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    You don't actually calculate something. You loop a or b times (depending which of the two is the largest number). If you have found a common divisor, you give assign k the value j which is pointless, because j is the largest of a and b, I think you want to do k = i;

    You break out of the loop and you don't use k. Your algorithm isn't functioning. Maybe take a look at Euclidean algorithm - Wikipedia, the free encyclopedia, the "Greatest Common Divisor" algorithm, see if you can use that.
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  3. #3
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by adrian2009 View Post
    I have a problem with reducing two rational numbers in the form a/b and need to recude them to form where a and b do not have any common integer dividers.

    I tried something, but it didn't work. Here is the code:

    Code:
    #include <stdio.h>
    int main()
    {
    int i, j, k;
    int a, b;
    scanf("%d/%d", &a, &b);
    if(a > b) j = b;
    else j = a;
    for(i = j;i > 1; i--)
    {
    if(((a % j) == 0) && ((b % j) == 0)) { k = j; break; }
    }
    a = a / j;
    b = b / j;
    printf("%d/%d\n", a, b);
    return 0;
    }
    It always gives me result 1/1. Where is the problem ? I really can't figure it out.
    Thanks in advance.
    u are doing a=a/j and b=b/j after the loop terminates,and inside the loop a,b,j are not getting changed so the result is always the same.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with Rational Numbers (C++)
    By cloudjc in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2008, 04:03 PM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  3. tic tac toe not working properly
    By h_howee in forum Game Programming
    Replies: 2
    Last Post: 01-01-2006, 01:59 AM
  4. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  5. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 10:01 PM