-4

I want to get roots of many double type variables (with some operators in it) in a line of C.

Example: In normal mathematics, √(b-a)2+(d-c)2

I had tried this in different ways, like

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

int main() {
    int i, Test;
   double a, b, c, d, e, f, g, h, j, num, root;
scanf("%d",&Test);
    for(i=1; i <= Test; i++) {
    scanf("%lf %lf %lf %lf", &a, &b, &c, &d);
    e = b - a;
    f = d - c;
    g = e * e;
    h = f * f;
    j = g + h;
   root = sqrt(j);
   printf("Case %d: %.4lf\n",i,root);
    }
   return 0;
}

}

But, I don't get the correct answer.

Description Sample Input Output

Please anyone help me with this.

Tamim
  • 11
  • 1
  • 2
  • 6
    `sqrt` is correct. There is not enough information here to tell what the issue is. Post a [reprex]. – interjay Jun 25 '21 at 11:13
  • 2
    What values are you entering, and what value is it printing? – Tom Karzes Jun 25 '21 at 11:18
  • You may want to check the return value of `scanf`, to make sure that the function was successful. If you make one small mistake when typing the input for the 4 floating-point numbers, then the function will likely fail. – Andreas Wenzel Jun 25 '21 at 11:19
  • looks okay to me: https://ideone.com/tZhYoS What is your input and expected/actual output? – mch Jun 25 '21 at 11:19
  • It is very easy to print the values of a,b,c,d too. – wildplasser Jun 25 '21 at 11:23
  • 2
    Please remove the `\n` from `scanf` format strings and check that `scanf` returns 1 from the first call and 4 from the second. call – Weather Vane Jun 25 '21 at 11:25
  • You have `return 0;` inside the loop. – Weather Vane Jun 25 '21 at 11:31
  • The problem statement needs to be read more carefully: *"For each case **print the case number** and print the value...* – Weather Vane Jun 25 '21 at 11:33
  • Your code and your square root computation look mostly fine. That's certainly one way to do it. I don't seen anything wrong with the computations of `e`, `f`, `g`, `h`, `j`, and `root`. You can also jam everything into a single expression: `root = sqrt((b-a)*(b-a) + (d-c)*(d-c))`. You can also exponentiate by calling `pow`: `root = sqrt(pow(b-a, 2) + pow(d-c, 2))`. – Steve Summit Jun 25 '21 at 11:42
  • In your posted code, you have 2 opening braces (`{`), but 3 closing braces (`}`). Also, your indentation is not correct. Therefore, it is impossible to tell which opening brace corresponds to which closing brace. – Andreas Wenzel Jun 25 '21 at 11:44
  • 1
    Don't post pictures of text but post text as text – Jabberwocky Jun 25 '21 at 11:44

1 Answers1

2

The source code in the question will not compile because it is missing a }.

When the missing } is inserted, it prints incorrect answers because it uses b - a and d - c. However, the inputs scanned into a, b, c, and d are x1, y1, x2, and y2, but the differences computed should be x1−x2 and y1−y2, not x1−y1 and x2−y2.

When this error is corrected, the program prints the sample output given the sample input except with more digits after the decimal places. This can be adjusted by changing %f in the printf to %.4f.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312