Monday 14 March 2016

Distance between 2 points


Write a program to find the distance between 2 points using functions.

Function specification: 
float findDistance(int x1, int y1, int x2, int y2) The function accepts 4 integer and returns a float. 

Input Format: 
Input consists of 4 integers. The first and second integer corresponds to the x-coordinate and y-coordinate of the first point. The third and fourth integer corresponds to the x-coordinate and y-coordinate of the second point.

Output Format: 
Output consists of a single floating point number (correct to 2 decimal places.) Refer sample output for formatting details.

Sample Input: 
3
6
4
3

Sample Output: 
Distance between the 2 points is 3.16

Code:
  #include<stdio.h>
#include<math.h>
float findDistance(int,int,int,int);
int main()
{
  int x1,x2,y1,y2;
  float r=0;
  scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
  
  r=findDistance(x1,y1,x2,y2);
  printf("Distance between the 2 points is %.2f",(float)r );
  return 0;
}
float findDistance(int x1, int y1, int x2, int y2)
{
  float d;
  d=sqrt((pow(x2-x1,2))+(pow(y2-y1,2)));
  return d;
}

2 comments:

  1. Candidates must bring the Admit Card KIITEE to the test centre on the exam day as identity proof.

    ReplyDelete
  2. distance = sqrt((x1 - y1) * (x1 - y1) + (x2 - y2) * (x2 - y2));

    use this formula to satisfy all the test cases.

    ReplyDelete