Square Root in C

Asked

Viewed 2,623 times

0

Follows the code:

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

    int main()
{

     double distancia;
     double x1, y1, x2, y2;

     scanf("%lf %lf ", &x1, &y1);
     scanf("%lf %lf ", &x2, &y2);

     distancia = sqrt (((x2 - x1)*(x2 - x1)) + ((y2 - y1)*(y2 - y1)));

     printf("%.4lf\n", distancia);
     return 0;
}

When executing the program, instead of asking for 4 entries as written in the entry, it asks for 5 and the fifth does not interfere in the final value. What is this last entry and why it occurs?

  • try to take the spaces from the scanf("%lf %lf ", &x2, &y2);

  • That’s right, thank you.

2 answers

0


Clear the spaces in the scanf:

scanf("%lf%lf", &x1, &y1);

0

As said above, just take out the spaces before closing the quotes from the scanf Follow the code working:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
     double distancia;
     double x1, y1, x2, y2;

     scanf("%lf %lf", &x1, &y1);
     scanf("%lf %lf", &x2, &y2);

     distancia = sqrt(((x2 - x1)*(x2 - x1)) + ((y2 - y1)*(y2 - y1)));

     printf("%.4lf\n", distancia);
     return 0;

    system("PAUSE");
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.