0

This is the code. S (arithmetic mean) is done right, I have valid result. But for other calculations I'm getting this error: Floating point exception (core dumped).

Does anyone know what is wrong with my code?

#include <stdio.h>
#include <math.h>

main () {
   int a,b,c;

   double s,h,g,k;

   printf ("unesite 3 cela broja\n");

   scanf ("%d%d%d", &a, &b, &c);

   s=(a+b+c)/3;

   printf ("aritmeticka s.v. je: %.2lf\n", s);

   /* this should be formula for medium value of harmonic number */
   h=3/((1/a)+(1/b)+(1/c)); 

   printf ("harmonijska s.v. je: %.2lf\n", h);

   /* this should be http://upload.wikimedia.org/math/a/f/f/aff7a590d055d563ceea52fd66fe7ee2.png */
   k=sqrt ((pow(a,2)+pow(b,2)+pow(c,2))/3);

   printf ("kvadratna s.v. je: %.2lf\n", k);

   /* and this http://upload.wikimedia.org/math/e/3/4/e348daea2f4f2bb60f5cb40706fcbad4.png */
   g=pow(a*b*c,1/3); 

   printf ("geometrijska s.v. je: %.2lf\n", g);

   return 0;
}
Dominik Hadl
  • 3,609
  • 3
  • 24
  • 58
  • if you are getting core dump, then I guess you can just use gdb to get backtrace and it will provide more information – Kunal Nov 07 '14 at 09:24
  • 3/(1/a+1/b+1/c) etc will will not be "correct" very often, you need to use floating point arithmentics to get the result you are expecting. Same for 1/3 (which is 0) unlike 1.0/3 (0.33). Anyway, the reason for the exception is bound to be division by zero, you can find this out by using gdb – perh Nov 07 '14 at 09:25

1 Answers1

1

Almost all your divisions are integer divisions. And if you divide an integer with a larger value then the result will be truncated to zero. Division by zero is not good, and will abort your program.

Change those divisions to floating point divisions.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621