Otto's C++ Corner

OttoDestructOttoDestruct Join Date: 2002-11-08 Member: 7790Members
Er... I don't get this assignment (not really assignment more of a quiz, not a full program):

Eucidean Algorithm
1: Find the remainder of a divided by b
2: Assign a the value of b
3: Assign b the value of the remainder
4: if b = 0 then quit, a is the greatest common divisor
5: Otherwise, go to step a and repeat

Define function gcd() that returns an int and has two int arguments. gcd() returns the greatest common divisor of its arguments. Be sure to state the assumptios on the values of actual arguments.


Er... ok.. so am I only supposed to use two variables?
<!--QuoteBegin--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> </td></tr><tr><td id='QUOTE'><!--QuoteEBegin-->Define function gcd() that returns an int and has two int arguments. <!--QuoteEnd--></td></tr></table><span class='postcolor'><!--QuoteEEnd-->

And WTH state the assumptions?
<!--QuoteBegin--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> </td></tr><tr><td id='QUOTE'><!--QuoteEBegin-->Be sure to state the assumptios on the values of actual arguments.
<!--QuoteEnd--></td></tr></table><span class='postcolor'><!--QuoteEEnd-->

I see no way to even get past the first part without using three variables (remainder, a, b). If the assignment means in the function header that I'm supposed to have two ints that makes more sense. Other than that I know I can just use a loop to accomplish everything.

Comments

  • TenSixTenSix Join Date: 2002-11-09 Member: 7932Members
    If you can't use it to program games then.... whats the use? <!--emo&:(--><img src='http://www.unknownworlds.com/forums/html/emoticons/sad.gif' border='0' style='vertical-align:middle' alt='sad.gif'><!--endemo-->
  • OttoDestructOttoDestruct Join Date: 2002-11-08 Member: 7790Members
    Heres what I got for code... I don't know what she means for "be sure to state the assumption blah blah blah"

    <!--c1--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
    int gcd (int a, int b)
    {
    int remainder;

    remainder = a / b;
    a = b;
    b = remainder;

    while (b == 1)
    {
    cout << "Enter A: ";
    cin >> a;
    cout << "Enter B: ";
    cin >> b;
    remainder = a/b;
    a = b;
    b = remainder;
    }

    return a;
    }
    <!--c2--></td></tr></table><span class='postcolor'><!--ec2-->
  • CrystalSnakeCrystalSnake Join Date: 2002-01-27 Member: 110Members
    edited November 2003
    OttoDestruct, your C++ topics greatly amuse us. Keep them coming!

    We'll give you two hints:
    1) recursion
    2) you're assuming that (a >= b), right?

    And the following line is not correct, can you figure out why?
    remainder = a / b;
  • CrystalSnakeCrystalSnake Join Date: 2002-01-27 Member: 110Members
    <!--QuoteBegin--TenSix+Nov 7 2003, 02:50 AM--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (TenSix @ Nov 7 2003, 02:50 AM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin-->If you can't use it to program games then.... whats the use?  <!--emo&:(--><img src='http://www.unknownworlds.com/forums/html/emoticons/sad.gif' border='0' style='vertical-align:middle' alt='sad.gif'><!--endemo--><!--QuoteEnd--></td></tr></table><span class='postcolor'><!--QuoteEEnd-->
    You'd be surprised how the most boring computer science things can end up being useful in a game.
  • OttoDestructOttoDestruct Join Date: 2002-11-08 Member: 7790Members
    <!--QuoteBegin--CrystalSnake+Nov 6 2003, 09:29 PM--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (CrystalSnake @ Nov 6 2003, 09:29 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> OttoDestruct, your C++ topics greatly amuse us. Keep them coming!

    We'll give you two hints:
    1) recursion
    2) you're assuming that (a >= b), right?

    And the following line is not correct, can you figure out why?
    remainder = a / b; <!--QuoteEnd--> </td></tr></table><span class='postcolor'> <!--QuoteEEnd-->
    No clue why the line isn't correct because I included a main function to setup the variables to be sent in, and it works fine.
  • NumbersNotFoundNumbersNotFound Join Date: 2002-11-07 Member: 7556Members
    <!--QuoteBegin--CrystalSnake+Nov 6 2003, 09:29 PM--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (CrystalSnake @ Nov 6 2003, 09:29 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> OttoDestruct, your C++ topics greatly amuse us. Keep them coming!

    We'll give you two hints:
    1) recursion
    2) you're assuming that (a >= b), right?

    And the following line is not correct, can you figure out why?
    remainder = a / b; <!--QuoteEnd--> </td></tr></table><span class='postcolor'> <!--QuoteEEnd-->
    You COULD use recursion, but recursion is most of the times less efficient (yet smaller in code) and harder to understand... Just stick with loops unless you wanna proclaim how "elite" you are <!--emo&:)--><img src='http://www.unknownworlds.com/forums/html/emoticons/smile.gif' border='0' style='vertical-align:middle' alt='smile.gif'><!--endemo-->
  • OttoDestructOttoDestruct Join Date: 2002-11-08 Member: 7790Members
    <!--QuoteBegin--404NotFound+Nov 6 2003, 09:48 PM--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (404NotFound @ Nov 6 2003, 09:48 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> <!--QuoteBegin--CrystalSnake+Nov 6 2003, 09:29 PM--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (CrystalSnake @ Nov 6 2003, 09:29 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> OttoDestruct, your C++ topics greatly amuse us. Keep them coming!

    We'll give you two hints:
    1) recursion
    2) you're assuming that (a >= b), right?

    And the following line is not correct, can you figure out why?
    remainder = a / b; <!--QuoteEnd--></td></tr></table><span class='postcolor'><!--QuoteEEnd-->
    You COULD use recursion, but recursion is most of the times less efficient (yet smaller in code) and harder to understand... Just stick with loops unless you wanna proclaim how "elite" you are <!--emo&:)--><img src='http://www.unknownworlds.com/forums/html/emoticons/smile.gif' border='0' style='vertical-align:middle' alt='smile.gif'><!--endemo--> <!--QuoteEnd--> </td></tr></table><span class='postcolor'> <!--QuoteEEnd-->
    The assignment was to use loops anyway.
  • CrystalSnakeCrystalSnake Join Date: 2002-01-27 Member: 110Members
    <!--QuoteBegin--OttoDestruct+Nov 7 2003, 03:39 AM--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (OttoDestruct @ Nov 7 2003, 03:39 AM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--><!--QuoteBegin--CrystalSnake+Nov 6 2003, 09:29 PM--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (CrystalSnake @ Nov 6 2003, 09:29 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin-->And the following line is not correct, can you figure out why?
    remainder = a / b; <!--QuoteEnd--></td></tr></table><span class='postcolor'><!--QuoteEEnd-->
    No clue why the line isn't correct because I included a main function to setup the variables to be sent in, and it works fine.
    <!--QuoteEnd--></td></tr></table><span class='postcolor'><!--QuoteEEnd-->
    The remainder of a divided by b is:
    a % b

    Also, why does gcd() ask for input?
    I thought gcd()'s only purpose was to return the greatest common divisor of a and b.
  • OttoDestructOttoDestruct Join Date: 2002-11-08 Member: 7790Members
    edited November 2003
    <!--QuoteBegin--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> </td></tr><tr><td id='QUOTE'><!--QuoteEBegin-->int gcd (int a, int b)<!--QuoteEnd--></td></tr></table><span class='postcolor'><!--QuoteEEnd-->

    Thats why.

    *Edit*

    Uh.. yea.. *slaps self for forgetting mod*
  • CrystalSnakeCrystalSnake Join Date: 2002-01-27 Member: 110Members
    <!--QuoteBegin--OttoDestruct+Nov 7 2003, 04:11 AM--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (OttoDestruct @ Nov 7 2003, 04:11 AM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--><!--QuoteBegin--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> </td></tr><tr><td id='QUOTE'><!--QuoteEBegin-->int gcd (int a, int b)<!--QuoteEnd--></td></tr></table><span class='postcolor'><!--QuoteEEnd-->

    Thats why.
    <!--QuoteEnd--></td></tr></table><span class='postcolor'><!--QuoteEEnd-->
    Let me guess...
    You teacher forces you to put a and b in gcd()'s parameter list, even though a and b are only used locally?
    Sorry if my questions sound stupid, but the programming style they teach you is very weird.
  • OttoDestructOttoDestruct Join Date: 2002-11-08 Member: 7790Members
    <!--QuoteBegin--CrystalSnake+Nov 6 2003, 10:20 PM--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (CrystalSnake @ Nov 6 2003, 10:20 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> <!--QuoteBegin--OttoDestruct+Nov 7 2003, 04:11 AM--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (OttoDestruct @ Nov 7 2003, 04:11 AM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--><!--QuoteBegin--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> </td></tr><tr><td id='QUOTE'><!--QuoteEBegin-->int gcd (int a, int b)<!--QuoteEnd--></td></tr></table><span class='postcolor'><!--QuoteEEnd-->

    Thats why.
    <!--QuoteEnd--></td></tr></table><span class='postcolor'><!--QuoteEEnd-->
    Let me guess...
    You teacher forces you to put a and b in gcd()'s parameter list, even though a and b are only used locally?
    Sorry if my questions sound stupid, but the programming style they teach you is very weird. <!--QuoteEnd--> </td></tr></table><span class='postcolor'> <!--QuoteEEnd-->
    It was part of the assignment to assume a and b were being passed into the function.
  • CrystalSnakeCrystalSnake Join Date: 2002-01-27 Member: 110Members
  • FieariFieari Join Date: 2002-10-22 Member: 1566Members, Constellation
    The way this was set up, it sounds exactly like it's describing a recursive function. So recursion looks ugly... but that description DESCRIBES recursion...

    But yes, use modulous. I can't think of any other way to get a remainder in C++ off the top of my head.

    Actually, as a tangent, CrystalSnake, -is- there any way to perform a modulous operation without using modulous?
  • BurrBurr Join Date: 2002-11-19 Member: 9358Members
    programing is evil.....I only need one more class out of two of it to get my diploma, and It is the one class that I had the most trouble with (I dropped it within a week, but I still have to take it)

    Of course, I have to know Java, not C++, but its still evil.
  • GwahirGwahir Join Date: 2002-04-24 Member: 513Members, Constellation
    it seems to me that recursion should not be used here. Although it can be done that way.

    Also, where in that description does it say to take in new values at every iteration in a loop?

    AND the way you set up that loop it'll only return the correct answer if it gets the right answer immediately, and in that case (with your code) it won't ask for new values.

    The way it seems to me, from your initial description:
    1. You can use additional variables if you need them.
    2. focus on the loop, always the loop
    3. don't ask for new values in the function.
    4. just use the variables passed to the function and output the answer.
    5. check the logic of that boolean statement in your while loop, it isn't helping.
  • SkulkBaitSkulkBait Join Date: 2003-02-11 Member: 13423Members
    <!--QuoteBegin--Burr+Nov 6 2003, 11:00 PM--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (Burr @ Nov 6 2003, 11:00 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> programing is evil.....I only need one more class out of two of it to get my diploma, and It is the one class that I had the most trouble with (I dropped it within a week, but I still have to take it)

    Of course, I have to know Java, not C++, but its still evil. <!--QuoteEnd--> </td></tr></table><span class='postcolor'> <!--QuoteEEnd-->
    Um... right.....


    The problem I am having is seeing what your code is doing. I mean, gcd implies that it should return the Gratest Common Factor of A and B, but your code doesn't do that. Why are you taking input in the loop? Why aren't you using recursion? It makes sense for this problem....
  • CrystalSnakeCrystalSnake Join Date: 2002-01-27 Member: 110Members
    <!--QuoteBegin--Fieari+Nov 7 2003, 04:56 AM--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (Fieari @ Nov 7 2003, 04:56 AM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin-->Actually, as a tangent, CrystalSnake, -is- there any way to perform a modulous operation without using modulous?<!--QuoteEnd--></td></tr></table><span class='postcolor'><!--QuoteEEnd-->
    You can write (a % b) like this:
    a - ((a / b) * b)
  • OttoDestructOttoDestruct Join Date: 2002-11-08 Member: 7790Members
    <!--QuoteBegin--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> </td></tr><tr><td id='QUOTE'><!--QuoteEBegin-->
    Also, where in that description does it say to take in new values at every iteration in a loop?
    <!--QuoteEnd--></td></tr></table><span class='postcolor'><!--QuoteEEnd-->
    I assumed it had to be as I programmed it so you don't get an infinite loop.

    <!--QuoteBegin--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> </td></tr><tr><td id='QUOTE'><!--QuoteEBegin-->AND the way you set up that loop it'll only return the correct answer if it gets the right answer immediately, and in that case (with your code) it won't ask for new values.<!--QuoteEnd--></td></tr></table><span class='postcolor'><!--QuoteEEnd-->

    Whats the point of asking for new values if its already correct? That would be like passing 1 + 1 = 2 in then asking you to do it again.

    <!--QuoteBegin--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> </td></tr><tr><td id='QUOTE'><!--QuoteEBegin-->
    5. check the logic of that boolean statement in your while loop, it isn't helping.
    <!--QuoteEnd--></td></tr></table><span class='postcolor'><!--QuoteEEnd-->
    Logic? What logic doesnt work about while ( b == 1)?
  • minskminsk Join Date: 2003-01-09 Member: 12077Members
    Now, to rewind things a little bit, and try to solve the initial problem...

    Variable: Declared locally by the procedure
    Parameter: Passed from outside the procedure

    <!--c1--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
    int make_four(int x) {
     int y = x / (x / 2);
     return y + 2;
    }
    <!--c2--></td></tr></table><span class='postcolor'><!--ec2-->

    The above is a really pointless little function that takes one parameter (x), returns an int (y + 2), and declares one local variable (y).

    In my main function (elsewhere in the code), I might have something like this:
    <!--c1--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
    int input;
    cin >> input;
    cout << make_four(input);
    <!--c2--></td></tr></table><span class='postcolor'><!--ec2-->

    The value of 'input' in the main function will determine what the value of 'x' is within make_four. When make_four returns the cout << will receive the value that was given to return.

    For assumptions, think about what would make the code not work correctly. For example, the division in make_four can result in a divide by zero, which is a Bad Thing ™, and we assume the user would never be so nasty as to give us that input.

    Hopefully this clears something up, if not, then meh...
  • GwahirGwahir Join Date: 2002-04-24 Member: 513Members, Constellation
    edited November 2003
    The loop will not be infinite if you follow the algorithm correctly. More specifically, the while loop should run as long as the final result isn't yet found. The algorithm you're using reuses the values from the last iteration of the loop and so no new values should be asked for.

    edit: note that the loop may be infinite if the assumptions for the values aren't correct, in which case the algorithm fails, and so would your code. You may want to check for that at the start of the function. Of course, there is also the problem that you would need to pre-specify an error value for return.... meh, just leave the check out and indicate the assumption.
  • OttoDestructOttoDestruct Join Date: 2002-11-08 Member: 7790Members
    <!--QuoteBegin--Gwahir+Nov 7 2003, 12:07 AM--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (Gwahir @ Nov 7 2003, 12:07 AM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> edit: note that the loop may be infinite if the assumptions for the values aren't correct, in which case the algorithm fails, and so would your code. You may want to check for that at the start of the function. Of course, there is also the problem that you would need to pre-specify an error value for return.... meh, just leave the check out and indicate the assumption. <!--QuoteEnd--> </td></tr></table><span class='postcolor'> <!--QuoteEEnd-->
    Thats exactly the point I was making. The loop only runs when b == 1. Otherwise it just skips it and returns a, as its already done those calculations and found a to be the greatest common divisor. Why would you want to prove twice that a is the greatest common divison? Other note - yes every assignment we have assumes correct data is entered unless we're told otherwise to dummy proof it.
  • pielemuispielemuis Join Date: 2002-01-25 Member: 72Members, NS1 Playtester
    edited November 2003
    <!--QuoteBegin--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> </td></tr><tr><td id='QUOTE'><!--QuoteEBegin-->Actually, as a tangent, CrystalSnake, -is- there any way to perform a modulous operation without using modulous?<!--QuoteEnd--></td></tr></table><span class='postcolor'><!--QuoteEEnd-->

    For powers of 2 there is. For instance if you want to do i%8 you could write i & 7 , just remember that the number after & is 1 smaller than the modulous you want and that it only works for modulous that are powers of 2.
  • SpookSpook Join Date: 2003-07-29 Member: 18542Members
    <!--QuoteBegin--OttoDestruct+Nov 7 2003, 08:34 AM--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (OttoDestruct @ Nov 7 2003, 08:34 AM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin-->Thats exactly the point I was making. The loop only runs when b == 1. Otherwise it just skips it and returns a, as its already done those calculations and found a to be the greatest common divisor.<!--QuoteEnd--></td></tr></table><span class='postcolor'><!--QuoteEEnd-->
    Monkey wrench time. And here is the reason why you always look for non-trivial test cases:

    Let's try a=6 b=9. The greatest common divisor of 6 and 9 is 3. Calling your provided gcd(6,9) returns 9 (assuming the remainder = a / b bug is fixed). 9 most definitely does not divide 6. Thus (b==1) is not the correct loop condition. Hint for the correct loop conditions:

    <!--QuoteBegin--OttoDestruct+Nov 6 2003,08:29 PM--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (OttoDestruct @ Nov 6 2003,08:29 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin-->4: if b = 0 then quit, a is the greatest common divisor
    5: Otherwise, go to step a and repeat
    <!--QuoteEnd--></td></tr></table><span class='postcolor'><!--QuoteEEnd-->
    Let "step a" = "step 1" for clarity.

    The algorithm was handed to you on a silver platter for this one. The entire assignment appears to be "given 5 steps can you convert each step one by one to code?" So given

    <!--c1--></span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->int gcd(int a, int b);
    <!--c2--></td></tr></table><span class='postcolor'><!--ec2-->
    as the declaration of the function (which is what you have), you know that a and b are passed in a arguments to the function. From there just take it one line at a time - there's nothing fancy to do. No input, no output. Just crank. You're making it way too hard on yourself...
Sign In or Register to comment.