FixYa.com
Technical Support, Instructions & Repair Service


Tags:

Gimpel Software PC-lint for C/C++ 8.0

Can't spot the problem with this.. (novice...

By kioner - usenet poster


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;

This Problem has been added to the Share Your Expertise Page under "My Work Queue".
Best Solution
posted on Aug 02, 2007
FixYa! (100)

Chandler

Chandler - usenet poster

Rank:Apprentice Apprentice
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:

Solution #2
posted on Aug 02, 2007
Not Rated (0)

paulrmc

paulrmc - usenet poster

Rank:Apprentice Apprentice
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:

Solution #3
posted on Aug 02, 2007
Not Rated (0)

Horner

Horner - usenet poster

Rank:Apprentice Apprentice
Rating: 0%, 0 votes
Did you leave out a test there, sparky?
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:

Can you Help with these Programming Tools problems?

Programming Tools
please help me
I am struggling to code how i... Answer This...
Programming Tools
usb.2.0-ser
locate this drive usb2.0-ser Answer This...
Programming Tools
extracting data in a...
I am struggling to code how i... Answer This...
Programming Tools
Burgler Alarm
I recently moved into a new... Answer This...
Programming Tools
Adobe photoshop cse extended
i watched the tutorial on... Answer This...
Repair Service
Find Programming Tool Repairman Near You:

FixYa does not evaluate or guarantee the accuracy of any information provided through its proposed solutions, posts, or Expert Assistance Sessions. By entering this site you declare you read and agreed to its Terms. You may NOT copy or distribute the content that appears on this site without written permission from FixYa Inc.
© 2005-2008, FixYa, Inc. or its affiliates
When the original poster rates a solution that was given to his own problem, that rating is locked!
X

Are you sure the solution content is Inappropriate?
   
Tech buddies can communicate directly to answer questions. Become a Tech Buddy and have direct access to your favorite expert for FREE!