-3
shrt = r - sqrt(pow(x * 1.0, 2.0) + pow(y * 1.0, 2.0));

I used this code to get the result for shrt = r - square root(x2+y2). But I got the answer wrong. What's wrong with my code? When I print shrt, the answer is incorrect! Here is my code:

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

int main() {
    int i, x, y, r;
    double shrt, lng;
    for (i = 0; i < 100; i++) {
        scanf("%d %d %d", &x, &y, &r);
        shrt = r - sqrt(pow(x * 1.0, 2.0) + pow(y * 1.0, 2.0));
        lng = 2 * r - shrt;
        printf("%.02lf %.02lf\n", shrt, lng);
    }
    return 0;
}

If I run the code and input here as a first test case: 0 0 100. Then I got the output -3.00 -3.00. But I want the output 100.00 100.00.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • What is the values of all involved variables? What is the output you expect? What is the output you get? And please try to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) to show us. – Some programmer dude Jul 14 '18 at 16:25
  • Post your entire code, including the datatypes and the value which you are getting for `shrt` – Jose Jul 14 '18 at 16:25
  • 1
    What answer did you get and what answer did you expect? What is the type of `shrt` and how are you printing it? –  Jul 14 '18 at 16:25
  • And please take some time to read [the help pages](http://stackoverflow.com/help), especially ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also please [take the tour](http://stackoverflow.com/tour) and [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). Lastly please please read [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/), and all of http://idownvotedbecau.se/ – Some programmer dude Jul 14 '18 at 16:26
  • Also please read [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) as that might be related to your issue. – Some programmer dude Jul 14 '18 at 16:31
  • Print the values of x, y, and r as well as the calculated values. Also check that `scanf()` succeeds (returns 3) before continuing. Checking that your program is getting the input you think it is getting is one of the most basic but crucial debugging techniques. – Jonathan Leffler Jul 14 '18 at 17:15
  • When I run the code shown, with only the addition of `void` in the definition of `int main(void)`, and then enter `0 0 100`, I get the expected output. That probably means that the code you show isn't the code you're running. Check that your source matches what you say, and that you've recompiled it. As I noted before, you should check that your input was successful, and that the input was what you expected to get, either via your debugger or by adding print statements. The code _should_ check `scanf()` regardless; it's a good idea when debugging to print your inputs too. – Jonathan Leffler Jul 14 '18 at 17:35
  • Thanks.I have printed my inputs.I have given the values :0 0 100 as my input.But the printed inputs are 3 0 0..Why is this happening?I am not understanding : ( – Megh_Akash Jul 14 '18 at 17:56
  • You said at one point that you got output `-3.00 -3.00`. If the values in the variables are `x = 3`, `y = 0`, `r = 0`, you should be getting `-3.00 3.00` (a positive value for the second). However, if you thought you entered `0 0 100` and got `3 0 0`, then there's a problem with the input. Did you try `if (scanf("%d %d %d", &x, &y, &r) != 3) { fprintf(stderr, "Input failure!\n"); return 1; }`? Does it in fact fail? You could try initializing the variables: `int x = 999, y = 888, r = 777;` and then running your code. What do you see? – Jonathan Leffler Jul 14 '18 at 18:40
  • @JonathanLeffler I have also got my expected output.Actually I did a silly mistake while giving my input – Megh_Akash Jul 14 '18 at 19:44

2 Answers2

1

Your scanf has failed

Test it without it and you will get the correct results:

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

int main()
{
  int i,x=0,y=0,r=100;
  double shrt,lng;
  for(i=0; i<100; i++)
  {

      shrt=r-sqrt(pow(x*1.0,2.0)+pow(y*1.0,2.0));
      lng=2*r-shrt;
      printf("%.02f %.02f\n",shrt,lng);
  }


  return 0;
}

https://ideone.com/VqdUVI

0___________
  • 60,014
  • 4
  • 34
  • 74
  • But why has my scanf failed?And what can I do here to give my input and receive expected output? – Megh_Akash Jul 14 '18 at 18:10
  • 2
    you need to answer this question yourself. Check the return value of scanf. Print the scanned values. You need to learn how to debug your programs – 0___________ Jul 14 '18 at 18:17
0

You computation results depends on the actual types of x, y, r and shrt and whether you correctly defined the sqrt and pow functions.

The most likely source of an incorrect result would be the lack of a correct definition of sqrt and pow. Make sure the line below is present at the beginning of you source file:

#include <math.h>
chqrlie
  • 131,814
  • 10
  • 121
  • 189