This is a program that is supposed to find the greatest common divisor
of two different numbers. It compiled without error but when I try to
run it, it spits out a error like "floating point error core dumped"
after I've entered the two numbers. Any help would be much
appreciated.
-Dylan Nash
#include <stdio.h
int gcd(int, int);
int main()
{
int x, y;
printf("Find the GCD of what two numbers: ");
scanf("%d%d", &x, &y);
printf("The GCD is %d\n", gcd(x, y));
return 0;
int gcd(int a, int b)
{
int i, result = 0;
for (i = 0; i <= a || i <= b; i++)
{
if (a % i == 0 && b % i == 0)
result = i;
}
return result;
Rating: 0%, 0 votes
The first time around the loop i is zero so you are trying to
calculate a % 0 and b % 0. This is division by zero which is an error.
Try counting from 1 instead.
--
Lawrence Kirby |
Wilts, England |
Was this solution helpful? Show your Appreciation by rating it:
Rating: 0%, 0 votes
What the problem is, is that the modulus operator has an implied division.
If the input value is zero, then all bets are off. Try something like one of
these:
/* Recursive: Simple, eh? */
unsigned
gcd1 (unsigned i, unsigned j)
{
return j ? gcd1 (j, i % j) : i;
/* Iterative: Still a snap */
unsigned
gcd2 (unsigned i, unsigned j)
{
while (j)
{
unsigned t = j;
j = i % j;
i = t;
}
return i;
/*
** A teensy bit more complicated, but worth the bother:
*/
//
/* This function uses the Euclidean Algorithm to */
/* calculate the greatest common divisor of two */
/* unsigned long integers. */
//
/* Programmer: Danniel R. Corbit */
/* */
/* Copyright (C) 1992 by Danniel R. Corbit */
/* All rights reserved. */
//
/* Reference: "Factorization and Primality Testing", */
/* by David M. Bressoud, */
/* Springer-Verlag 1989. */
/* pages 7-12. */
//
unsigned long
gcd (unsigned long a, unsigned long b)
{
int shiftcount = 0;
unsigned long tmp;
//
/* This zero testing stuff may seem odd, since zero is not likely. */
/* However, knowing that neither a nor b is zero will speed up */
/* later operations greatly by elimination of tests for zero. */
//
if (a == 0L)
{
tmp = b;
}
else if (b == 0L)
{
tmp = a;
}
else
{ /* Neither a NOR b is zero! */
//
/* While all this fuss about numbers divisible by 2 may seem */
/* like quite a bother, half of the integers in the universe */
/* are evenly divisible by 2. Hence, on a random sample of */
/* input values, great benefit will be realized. The odds */
/* that at least one of a,b is even is 1 - (1/2)*(1/2) = .75 */
/* since the probability that both are odd is .25. */
//
/* If the last bit is 0, the number is divisible by 2 evenly. */
/* If a & b are divisible by 2, gcd(a,b) = 2*gcd(a/2,b/2). */
//
while (!(a & 1L) && !(b & 1L))
{
a b shiftcount++;
}
//
/* If a is divisible by 2 and b is not divisible by 2, */
/* then gcd(a,b) = gcd(a/2,b). */
//
while (!(a & 1L))
{
a }
//
/* If b is divisible by 2 and a is not divisible by 2, */
/* then gcd(a,b) = gcd(a,b/2). */
//
while (!(b & 1L))
{
b }
//
/* Make sure the numbers are in the proper order (swap if necessary). */
//
if (b {
tmp = a;
a = b;
b = tmp;
}
//
/* Euclid's famous gcd algorithm: */
/* Iterate until the remainder is <= 1. */
//
while (b {
tmp = b;
b = a % b;
a = tmp;
}
if (b == 0)
tmp = a;
else
tmp = b;
//
/* If we divided BOTH numbers by factors of 2, we must compensate. */
//
if (shiftcount tmp <<= shiftcount;
}
return (tmp);
--
C-FAQ: #
"The C-FAQ Book" ISBN 0-201-84519-9
C.A.P. Newsgroup #
C.A.P. FAQ: #
Was this solution helpful? Show your Appreciation by rating it:
Rating: 0%, 0 votes
C:\tmp
C:\tmp PC-lint for C/C++ (NT) Ver. 7.00p, Copyright Gimpel Software 1985-1997
--- Module: foo.c
C:\tmp
--- Module: foo.c
_
if (a % i == 0 && b % i == 0)
foo.c(25) : Warning 414: Possible division by 0
foo.c(25) : Warning 414: Possible division by 0
--
C-FAQ: #
"The C-FAQ Book" ISBN 0-201-84519-9
C.A.P. Newsgroup #
C.A.P. FAQ: #
Was this solution helpful? Show your Appreciation by rating it:
Post a New problem for Gimpel Software PC-lint for C/C++ 8.0
Email this problem

