Problem Description
The mathematics teacher has recently taught the formula for computing the distance between 2 points to her students. She gave them an asssignment on calculating the distance between the points. In order to prevent copying, she gave each student a different set of questions. But she found it very difficult to correct all 100 assignments. She seeks your help. Can you please help her out?
Given the vertices of a triangle ABC, determine the sides of the triangle AB, BC and AC. Can you write a C program to calculate the sides?
Input Format
Input consists of 6 integers. The first 2 integers correspond to the x-coordinate and y-coordinate of vertex A. The next 2 integers correspond to the x-coordinate and y-coordinate of vertex B. The next 2 integers correspond to the x-coordinate and y-coordinate of vertex C.
Output Format:
Refer Sample Input and Output for exact formatting specifications.
[All floating point values are displayed correct to 1 decimal place]Test Case 1
Input (stdin)5 10 10 10 8 4
Expected OutputLength of side AB is 5.0 Length of side BC is 6.3 Length of side AC is 6.7
Test Case 2
Input (stdin)3 4 4 5 5 6
Expected OutputLength of side AB is 1.4 Length of side BC is 1.4 Length of side AC is 2.8
Program
#include <stdio.h> #include<math.h> float distance(int x1,int x2,int y1,int y2) { float distance; distance=sqrt(((x1-x2)*(x1-x2))+((y1-y2)*(y1-y2))); return distance; }; int main() { int x1,x2,x3,y1,y2,y3; float a1,a2,a3; scanf("%d %d",&x1,&y1); scanf("%d %d",&x2,&y2); scanf("%d %d",&x3,&y3); a1=distance(x1,x2,y1,y2); a2=distance(x2,x3,y2,y3); a3=distance(x1,x3,y1,y3); printf("Length of side AB is %.1f\n",a1); printf("Length of side BC is %.1f\n",a2); printf("Length of side AC is %.1f\n",a3); return 0; }
No comments:
Post a Comment
Note: only a member of this blog may post a comment.