Last night I successfully produced my first Cocoa application, a simple calculator. Simple or not, I was thrilled that it worked and I actually used it on my Mac! How exciting, made me want to keep charging forward! I guess it's Cocoa's equivalent to C's "hello, world" program. Anyway, today brings a new C challenge. Here is the problem, directly from my book.
"Write a program that asks the user to enter two integers, then calculates and displays their greatest common divisor (GCD)
Should look like this to the user.....
Enter two integers: 12 28
Greatest common divisor: 4
Hint: The classic algorithm for computing the GCD, known as Euclid's algorithm, goes as follows. Let m and n be variables containing the two numbers. Divide m by n. Save the divisor in m, and save the remainder in n. If n is 0, then stop. m contains the GCD. Otherwise repeat the process, starting with the division of m by n. "
So I worked the math out on paper for a few combinations of numbers to become re-acquainted with GCD and how this Euclid's algorithm will work. Seems easy enough.
I will
1. Declare int m, n ;
2. Ask the user to input two numbers
3. scanf and accept the two numbers into &m, &n
4. Then begin my while loop, because as long as (m % n != 0), I want the loop to continue.
5. The problem says to divide m by n. Then save the divisor in m. I don't know the point in doing the division. Since n is the divisor, I can just say m = n. Saving n into m.
6. Then it wants me to save the remainder into n. So I figure I will do something like (n = (m % n))
All of this of course is happening as long as (m % n != 0) (if the remainder is not 0, I want the loop to run. While the loop is running, is will assign n to m, then assign the REM to n, then check to see if (m % n != 0) again, and so on. That's a loop that will continue until the REM does result in 0.
Of course if the controlling expression (m % n != 0) is false and the REM is 0, then I expect it to break from the loop and printf (The GCD is %d, n)
Partial Pseudo code (not complete, but thought process)
int firstnum, secondnum ;
printf (blah blah blah)
scanf (%d,%d, &firstnum, &secondnum)
while (firstnum % secondnum != 0)
firstnum = secondnum and && secondnum = firstnum % secondnum
Now, I'm getting an error that I think is a problem with my loop. I will post two different codes that I wrote. They each have some things commented out in the while loop. They indicate several different ways I tried to write my loop to get it to work.
The reason I think the problem is with my loop is because in both code examples, I do get correct program performance and output if the user enters two integers that REM out to 0 the first time. (example if user input 10 and 5, the REM is 0, and the programs works) But anything else I'm getting a "floating point exception", whatever that is. Check it out below, thanks in advance.
I hope the code below shows that I'm starting to understand things a little bit and my thought process is hopefully correct. (even though the code is obviously wrong) I think it's of some merit that I thought of two different ways to solve the same problem. Maybe they can both work, maybe neither, but I'm encouraged that they both produce acceptable output under at least specific circumstances. It originally took me about 30 min to lay out the flow on paper, then write the code. But I've spent the past 4 hours tweeking it (commented lines) to see if I could get it to work.
Terminal Output:
Next Code Example:
Terminal Output:
"Write a program that asks the user to enter two integers, then calculates and displays their greatest common divisor (GCD)
Should look like this to the user.....
Enter two integers: 12 28
Greatest common divisor: 4
Hint: The classic algorithm for computing the GCD, known as Euclid's algorithm, goes as follows. Let m and n be variables containing the two numbers. Divide m by n. Save the divisor in m, and save the remainder in n. If n is 0, then stop. m contains the GCD. Otherwise repeat the process, starting with the division of m by n. "
So I worked the math out on paper for a few combinations of numbers to become re-acquainted with GCD and how this Euclid's algorithm will work. Seems easy enough.
I will
1. Declare int m, n ;
2. Ask the user to input two numbers
3. scanf and accept the two numbers into &m, &n
4. Then begin my while loop, because as long as (m % n != 0), I want the loop to continue.
5. The problem says to divide m by n. Then save the divisor in m. I don't know the point in doing the division. Since n is the divisor, I can just say m = n. Saving n into m.
6. Then it wants me to save the remainder into n. So I figure I will do something like (n = (m % n))
All of this of course is happening as long as (m % n != 0) (if the remainder is not 0, I want the loop to run. While the loop is running, is will assign n to m, then assign the REM to n, then check to see if (m % n != 0) again, and so on. That's a loop that will continue until the REM does result in 0.
Of course if the controlling expression (m % n != 0) is false and the REM is 0, then I expect it to break from the loop and printf (The GCD is %d, n)
Partial Pseudo code (not complete, but thought process)
int firstnum, secondnum ;
printf (blah blah blah)
scanf (%d,%d, &firstnum, &secondnum)
while (firstnum % secondnum != 0)
firstnum = secondnum and && secondnum = firstnum % secondnum
Now, I'm getting an error that I think is a problem with my loop. I will post two different codes that I wrote. They each have some things commented out in the while loop. They indicate several different ways I tried to write my loop to get it to work.
The reason I think the problem is with my loop is because in both code examples, I do get correct program performance and output if the user enters two integers that REM out to 0 the first time. (example if user input 10 and 5, the REM is 0, and the programs works) But anything else I'm getting a "floating point exception", whatever that is. Check it out below, thanks in advance.
I hope the code below shows that I'm starting to understand things a little bit and my thought process is hopefully correct. (even though the code is obviously wrong) I think it's of some merit that I thought of two different ways to solve the same problem. Maybe they can both work, maybe neither, but I'm encouraged that they both produce acceptable output under at least specific circumstances. It originally took me about 30 min to lay out the flow on paper, then write the code. But I've spent the past 4 hours tweeking it (commented lines) to see if I could get it to work.
Code:
#include <stdio.h>
main ()
{
int m = 0, n = 0 ;
printf ("Enter two numbers (separated by a space): ") ;
scanf ("%d%d", &m,&n) ;
while (m % n != 0) {
//(m = n) && (n = m % n) ; this line by itself
//m / n ; these three
//m = n ; lines
//n = m % n ; together
if (m % n != 0) //these three
m = n ; //lines
n = m % n ; // together
}
printf ("Greatest Common Divisor: %d\n", n) ;
return 0 ;
}
Terminal Output:
Code:
Scott-Deans-MacBook-Pro:~ scottdean$ gcc ~/documents/Ch_6_loop.c
Scott-Deans-MacBook-Pro:~ scottdean$ ./a.out
Enter two numbers (separated by a space): 100 50
Greatest Common Divisor: 50
Scott-Deans-MacBook-Pro:~ scottdean$ ./a.out
Enter two numbers (separated by a space): 34 28
Floating point exception
Next Code Example:
Code:
#include <stdio.h>
main ()
{
int m, n ;
printf ("Enter two numbers (separated by a space): ") ;
scanf ("%d%d", &m,&n) ;
if (m % n == 0 )
printf ("The GCD is: %d\n", n) ;
else
while (m % n != 0) {
//(m = n) && (n = m % n) ;
m = n ;
n = m % n ;
}
//printf ("The GCD is: %d\n", n) ;
return 0 ;
}
Terminal Output:
Code:
Scott-Deans-MacBook-Pro:~ scottdean$ ./a.out
Enter two numbers (separated by a space): 100 50
The GCD is: 50
Scott-Deans-MacBook-Pro:~ scottdean$ ./a.out
Enter two numbers (separated by a space): 50 100
Floating point exception
Last edited: