question on GCD and for loops

This is a discussion on question on GCD and for loops within the C Programming forums, part of the General Programming Boards category; ok im doing this other program on for loops for greatest common divisor, so the problem is i cant seem ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    25

    Lightbulb question on GCD and for loops

    ok im doing this other program on for loops for greatest common divisor, so the problem is i cant seem to display the common divisor of both numbers, so i seem to be having problems with the formula inside the for loop, so it doesn't work is there anything im missing

    Code:
    #include <math.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
       {
           int g,n,m,i;
           
           printf("Give me two numbers separated by space (p>q): ");
           scanf("%d %d",&n,&m);
           
                 for(i=0;i<=n%m;i++){
                                   
                   g=n%m;
                   g=m%n;
                 }
                   
                   printf("The GCD of %d and %d is %d ",n,m,g);
                   getchar();
                   getchar();
                   
       }
    also another question how can i do to insert two numbers in one enter (insert two numbers just by spacing them?)

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,264
    I think you'd learn a lot by trying to explain to us in your own words what this does:
    Code:
                 for(i=0;i<=n&#37;m;i++){
                                   
                   g=n%m;
                   g=m%n;
                 }
    It does not at all do what you want.

    If you need a reference source for this algorithm, here's one:
    http://en.wikipedia.org/wiki/Euclidean_algorithm
    btw this algorithm is extremely common in computer science teaching. If you google it, you can expect to find over a million hits.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    25
    the modulus equation in that part give me

    g=150/62=2.419 but the modulus sign rounds it off to the next lowest whole number

    ohhhh i see the modulus thing in the for statement inside the parethesis, i was trying out something to see if it worked but it didn't and i forgot to change it, anyways the Euclidean Algorithm helps, but it doesn't use what i want to use a for loop instead of a while loop

    i really don't know im really lost on what to do next

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    25
    i still haven't solved this problem, somehow always all the easy stuff throws me off

    anyways, if anyone can help me with this it'll be great
    thanks

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What does MODULUS (%) do? It seems like you are confused about that.

    Also the code-segment that iMalc posted is quite likely NOT what you want [at least not exactly like it is now].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    25
    oh modulus &#37; find the value of the remainder of the equation or division

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,264
    Quote Originally Posted by dals2002 View Post
    ohhhh i see the modulus thing in the for statement inside the parethesis, i was trying out something to see if it worked but it didn't and i forgot to change it, anyways the Euclidean Algorithm helps, but it doesn't use what i want to use a for loop instead of a while loop
    Why would you want to use a for-loop? A for-loop is the most logical choice when there are a fixed number of iterations, as well as a few other cases. In this case it is clearer with a while loop. It is easy to write any kind of loop using any of the loop constructs anyway, so you could always learn to convert between them.

    Anyway, ignoring the for-loop for now, are you at least able to now explain what the folowing does?:
    Code:
                   g=n%m;
                   g=m%n;
    Do you understand what is wrong with that?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    Feb 2008
    Posts
    25
    Quote Originally Posted by iMalc View Post
    Why would you want to use a for-loop? A for-loop is the most logical choice when there are a fixed number of iterations, as well as a few other cases. In this case it is clearer with a while loop. It is easy to write any kind of loop using any of the loop constructs anyway, so you could always learn to convert between them.

    Anyway, ignoring the for-loop for now, are you at least able to now explain what the folowing does?:
    Code:
                   g=n&#37;m;
                   g=m%n;
    Do you understand what is wrong with that?
    yea that gives me the remainder of the division, it gets higher the higher the numbers, but it doesn't give me what i want which is the opposite of the remainder. i was asked in the book to use a for loop, since i haven't got to read the chapter on while loops yet

  9. #9
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    You might want to search for the words "Euclidean algorithm", and look at the pseudo-codes
    presenting the algorithm, as you have it clearly a little messed up.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,264
    Quote Originally Posted by dals2002 View Post
    yea that gives me the remainder of the division, it gets higher the higher the numbers, but it doesn't give me what i want which is the opposite of the remainder. i was asked in the book to use a for loop, since i haven't got to read the chapter on while loops yet
    Okay, nope you don't understand at all what is wrong with those two lines.
    Forget loops and modulus, you need to go back to understanding variable assignment.

    What does this code snippet do?:
    Code:
    int x;
    x = 1;
    x = 2;
    Can you simplify it?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

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