Question on Lecture 3 Notes

8 views
Skip to first unread message

James Gallagher

unread,
Sep 19, 2011, 5:27:36 PM9/19/11
to 091lab...@googlegroups.com
Reading through the lecture notes as instructed I'm stuck on one of the slides (27):

Compute the gcd using the Euclidean algorithm: 
i n t  gcd ( i n t  a ,  i n t  b )  { 
while  ( b )  {  / ∗ i f  a  <  b ,  per forms  swap  ∗ / 
i n t  temp  =  b ; 
b  =  a % b ; 
a  =  temp ; 
}

return  a ;

}

I don't get the while loop - the comment says it's testing the condition a < b. Slide 7 says "Expression is non-zero ⇒ condition true", so is while testing for b <> 0 ?

James

Antóin Óg Ó Cuinneagáin

unread,
Sep 20, 2011, 4:19:55 AM9/20/11
to 091lab...@googlegroups.com
You would give me a hard one right off the bat wouldn't you :-)

while will repeat until b is 0 (false).

I think the comment is not intended specifically for the test in the while statement but maybe for the whole algorithm (although I'm not sure)
http://en.wikipedia.org/wiki/Euclidean_algorithm#Implementations

Antóin Óg Ó Cuinneagáin

unread,
Sep 20, 2011, 4:29:06 AM9/20/11
to 091lab...@googlegroups.com
I wrote the code up...don't expect this kind of service each time :-)
I think the comment relates to the nature of the modulus (remainder) operator, %
if a=8 and b=12 then a is less than b, then in the statement b=a%b it will be 8%12, which is a(8) so b becomes a, and the next line a=temp(b), so a becomes b.  They are swapped in the first loop iteration
if a=12 and b=8 then b constantly decreases to 0 and a is set to the previous b values
/*
 ============================================================================
 Name        : gcd.c
 Author      :
 Version     :
 Copyright   : Your copyright notice
 Description : Hello World in C, Ansi-style
 ============================================================================
 */

#include <stdio.h>
#include <stdlib.h>
int gcd(int a, int b);
int main(void) {
    puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */
    int gcdInt=gcd(12,8);
    int gcdInt2=gcd(8,12);
    return EXIT_SUCCESS;
}
int gcd(int a, int b) {
    while (b) {
        /* if a < b , per forms swap */
        int temp = b;

        b = a % b;
        a = temp;
    }

    return a;

}


2011/9/20 Antóin Óg Ó Cuinneagáin <anthony.c...@gmail.com>

James Gallagher

unread,
Sep 21, 2011, 7:09:15 AM9/21/11
to 091lab...@googlegroups.com
Thank you :) I got confused about whether while(b) would result in an infinite loop because my thought process kept throwing while(1) as I tried to process it. Even though I read earlier about what while(0) would mean it didn't 'click'. I think issues like these are what have gotten in my way of properly learning to code - so thanks for helping out. I've also decided that my standard of programming and why I managed to do some as a job is 'monkey see, monkey do'. Ideally, I can get beyond that and code creatively instead of derivatively.

Antóin Óg Ó Cuinneagáin

unread,
Sep 21, 2011, 8:22:19 AM9/21/11
to 091lab...@googlegroups.com
In Java there are real booleans, ie. true and false, so its easier.

___ ___

unread,
Sep 22, 2011, 4:50:46 PM9/22/11
to 091labs-code
On Sep 21, 1:22 pm, Antóin Óg Ó Cuinneagáin
<anthony.cunning...@gmail.com> wrote:
> In Java there are real booleans, ie. true and false, so its easier.

#define TRUE 1
#define FALSE 0

has been useful to me whenever I really needed bools in C...

Duncan Thomas

unread,
Sep 22, 2011, 5:14:02 PM9/22/11
to 091lab...@googlegroups.com

Don't do that... It will cause problems whenever you do == TRUE or != TRUE

Antóin Óg Ó Cuinneagáin

unread,
Sep 23, 2011, 6:00:57 AM9/23/11
to 091lab...@googlegroups.com
Can you just use the FALSE one, and use ==FALSE or !=FALSE?

Duncan Thomas

unread,
Sep 23, 2011, 9:35:04 AM9/23/11
to 091lab...@googlegroups.com

You can, but it is a fairly unusual thing to see, so you will struggle with code that assumes you understand how boolean tests work... I think you'd be far better off biting the bullet and learning it properly... C is not a kind or forgiving language

Antóin Óg Ó Cuinneagáin

unread,
Sep 23, 2011, 9:38:49 AM9/23/11
to 091lab...@googlegroups.com
Do you mean just use 0 and not 0 as opposed to constants

Duncan Thomas

unread,
Sep 23, 2011, 9:45:42 AM9/23/11
to 091lab...@googlegroups.com
A more common idiom is to miss out the == completely, e.g.

if (a) {
printf("A was true\n");
}

This also lead to common patterns such as:

char *buf = malloc(255);
if (!a) {
perror("malloc");
exit(1);
}

2011/9/23 Antóin Óg Ó Cuinneagáin <anthony.c...@gmail.com>:

--
Duncan Thomas

Reply all
Reply to author
Forward
0 new messages