Thread stopped C: \ ... Fault: integer divides by 0x401203 error in borland 5.02

2

Borland use 5.02 for a college job ... and for some reason the program is not able to assign variable values in other variables to simple mathematical functions.

This is my code:

#include <stdio.h>
#include <conio.h>
main()
{
  int m, s, cs, mt,h,km;
  float t1,t2,s2, v;
  printf("Introduzca minutos: "); scanf("%i",&m);
  printf("Introduzca segundos: "); scanf("%i",&s);
  printf("Introduzca centecimas de segundos: "); scanf("%i",&cs);
  printf("Introduzca metros: "); scanf("%i",&mt);

  if(m<60 && s<60 && cs<100 || 0>m && 0>s && 0>cs && 0>mt)
  {
    t1=m*60;
    t2=cs/100;
    s2=t1+t2+s;
    h=(s2/60)/60;
    km=mt/1000;
    v=h/km;
    printf("Su velocidad fue de: %8.2f kmh", (v));
  }
  else
  {
    printf("Error.");
  }
  getch();
}
    
asked by Carlos Alvarez 11.04.2016 в 00:54
source

1 answer

0

The problem is how you define the variables mt and km , and in these two lines:

km=mt/1000;
v=h/km;

mt is an integer ( int ), dividing it by 1000 will return a whole value rounded down. That means that if the user writes a value less than 1000 for mt , km will be 0. Then the second line will be a division by zero and that's why you get the error you indicate in the question.

The solution: convert both mt and km into floats and change the reading from mt (from %i to %f to be now a float), and the problem is solved:

  

You should also make h a float or for small values you will always get 0

#include <stdio.h>
#include <conio.h>
main()
{
  int m, s, cs;
  float t1,t2,s2, v, km, mt, h;
  printf("Introduzca minutos: "); scanf("%i",&m);
  printf("Introduzca segundos: "); scanf("%i",&s);
  printf("Introduzca centecimas de segundos: "); scanf("%i",&cs);
  printf("Introduzca metros: "); scanf("%f",&mt);

  if(m<60 && s<60 && cs<100 || 0>m && 0>s && 0>cs && 0>mt)
  {
    t1=m*60;
    t2=cs/100;
    s2=t1+t2+s;
    h=(s2/60)/60;
    km=mt/1000;
    v=h/km;
    printf("Su velocidad fue de: %8.2f kmh", (v));
  }
  else
  {
    printf("Error.");
  }
  getch();
}
    
answered by 11.04.2016 / 01:57
source