Would you check my code, please?

This is a discussion on Would you check my code, please? within the C Programming forums, part of the General Programming Boards category; Hello, I'm new to C programming and would appreciate if someone would help with this. Every time I run the ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    3

    Unhappy Would you check my code, please?

    Hello, I'm new to C programming and would appreciate if someone would help with this.
    Every time I run the following the program "stops" at the line where the gCD function takes in values (I've tried it without this line and it doesn't stop).

    Code:
    int abs(int a)
    {
        int A=a;
        if (a<0) A=-a;
        return(A);
    }
    
    
    
    int gCD(int a, int b)
    {
        int gcd = 1;
    
        while(a % 2 == 0 && b % 2 == 0)
        {
            gcd *= 2;
            a /= 2;
            b /= 2;
        }
    
        while (a % 2 == 0 || b % 2 == 0)
        {
            a /= 2;
            b /= 2;
        }
    
        int c = abs(a - b);
        while (c % 2 == 0)
        {
            c /= 2;
        }
    
        gcd *= c;
    
        return(gcd);
    }
    
    
    
    
    
    int main()
    {
        int N;
        int D;
    
        printf("Type a numerator:  ");
        scanf("%d", &N);
        getchar();
    
        printf("\nType a denominator:  ");
        scanf("%d", &D);
        getchar();
    
        int gcdCN = gCD(N,D);
    
        N /= gcdCN;
        D /= gcdCN;
    
        printf("Simplified numerator:  %d\n", N);
        printf("Simplified denominator: %d\n\n", D);
        getchar();
    
        return 0;
    }

    Thanks!
    Last edited by Salem; 08-23-2010 at 10:03 PM. Reason: And use [code][/code] tags

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    It gets caught in an infinite loop depending upon your input, and even when it doesn't get caught, it returns the wrong answer. For example, with inputs N = 46, D = 146, it finishes, but it thinks the gcdCN is 50. It should be 2. An input that gets caught in the infinite loop is N = 2, D = 2. You might want to try again on your gcd algorithm and learn to use code tags.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    And Welcome to the forum!

  4. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    3
    Quote Originally Posted by pianorain View Post
    It gets caught in an infinite loop depending upon your input, and even when it doesn't get caught, it returns the wrong answer. For example, with inputs N = 46, D = 146, it finishes, but it thinks the gcdCN is 50. It should be 2. An input that gets caught in the infinite loop is N = 2, D = 2. You might want to try again on your gcd algorithm and learn to use code tags.
    Thanks! I was thinking so, but I couldn't find the prob. Now that I had some rest,
    I noticed that
    Code:
    c=int(a-b)
    could be zero then it starts the infinite loop.

    Sorry for my lack of touch I couldn't find the code tags.

    Thanks again.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    74
    on the abs function, you could simplify that.

    You don't really need the 'A' variable. Just use the if and return either a or -a

  6. #6
    Registered User
    Join Date
    Aug 2010
    Posts
    3
    I just thought I didn't want to change the variable's value, I guess C doesn't change it after all. Thanks.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    74
    You're not. That 'a' variable is not the same variable as the one in the previous function, its only local to the abs() function. In c the parameters are copies of the variables that you passed.

  8. #8
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    230
    just a word of advice you have a getchar() after a scanf() which I think is to pause the program. You dont need that because scanf already "pauses" the program.

    And welcome to the forum

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    2,372
    Here's an algorithm in case you can not find one.
    Euclidean algorithm - Wikipedia, the free encyclopedia

    Note: I suggest changing

    Code:
    int gcdCN = gCD(N,D);
    To two lines and adding the use of abs function to the call to prevent problems later for negative input.
    Note: My code for Euclidean algorithm assumed non-negative input; your solution might not need abs on call.
    Some C compilers will not like the declare and statement like you did it.

    Code:
    int gcdCN;
    Code:
    gcdCN = gCD(abs(N),abs(D));
    Tim S.
    Last edited by stahta01; 08-24-2010 at 12:56 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. check my c code.
    By rfm in forum C Programming
    Replies: 4
    Last Post: 09-24-2009, 05:47 AM
  2. How to read a txt file into an array
    By Hitsugaya_KK in forum C Programming
    Replies: 49
    Last Post: 08-22-2009, 02:22 PM
  3. HELP!!!!emergency ~expert please help
    By unknowppl in forum C Programming
    Replies: 1
    Last Post: 08-19-2008, 07:35 AM
  4. Check my code Please
    By AdioKIP in forum C++ Programming
    Replies: 1
    Last Post: 03-12-2002, 07:52 PM

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