Accenture Coding Question 14

Coding Question 14

Instructions: You are required to write the code. You can click on compile & run anytime to check the compilation/ execution status of the program. The submitted code should be logically/syntactically correct and pass all the test cases.

Ques: The program is supposed to calculate the distance between three points.

For,

x1 = 1,  y1 = 1
x2 = 2 , y2 = 4
x3 = 3,  y3 = 6

Distance is calculated as : sqrt(x2-x1)2 + (y2-y1)2

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

int isDistance(float *pt1, float *pt2, float *pt3)
{
float a, b, c;
a = sqrt(((pt2[0]-pt1[0])*(pt2[0]-pt1[0]))+((pt2[1]-pt1[1])*(pt2[1]-pt1[1])));
printf(“%f”,a);
b = sqrt(((pt3[0]-pt2[0])*(pt3[0]-pt2[0]))+((pt3[1]-pt2[1])*(pt3[1]-pt2[1])));
printf(“%f”,b);
c = sqrt(((pt3[0]-pt1[0])*(pt3[0]-pt1[0]))+((pt3[1]-pt1[1])*(pt3[1]-pt1[1])));
printf(“%f”,c);
}

int main()
{
int t;
float p1[2], p2[2], p3[2];
printf("enter x1 and y1 : ");
scanf("%f%f",&p1[0],&p1[1]);
printf("enter x2 and y2 : ");
scanf("%f%f",&p2[0],&p2[1]);
printf("enter x3 and y3 : ");
scanf("%f%f",&p3[0],&p3[1]);
t = isDistance(&p1, &p2, &p3);
printf("%d",t);
return 0;
}
import math

x1, y1 = 1, 1
x2, y2 = 2, 4
x3, y3 = 3, 6

first_diff = math.sqrt(math.pow(x2-x1, 2) + math.pow(y2-y1, 2))
second_diff = math.sqrt(math.pow(x3-x2, 2) + math.pow(y3-y2, 2))
third_diff = math.sqrt(math.pow(x3-x1, 2) + math.pow(y3-y1, 2))

print(round(first_diff,2), round(second_diff,2), round(third_diff,2))
#include <bits/stdc++.h>
using namespace std;

int isDistance(float *pt1, float *pt2, float *pt3)
{
float a, b, c;

a = sqrt(((pt2[0]-pt1[0])*(pt2[0]-pt1[0]))+((pt2[1]-pt1[1])*(pt2[1]-pt1[1])));
cout<<a;

b = sqrt(((pt3[0]-pt2[0])*(pt3[0]-pt2[0]))+((pt3[1]-pt2[1])*(pt3[1]-pt2[1])));
cout<<b;

c = sqrt(((pt3[0]-pt1[0])*(pt3[0]-pt1[0]))+((pt3[1]-pt1[1])*(pt3[1]-pt1[1])));
cout<<c;
}

int main()
{
float p1[2], p2[2], p3[2];

cout<<"Enter x1 and y1 : ");
cin>>p1[0]>>p1[1];

cout<<"Enter x2 and y2 : ");
cin>>p2[0]>>p2[1];

cout<<"Enter x3 and y3 : ");
cin>>p3[0]>>p3[1];

isDistance(&p1, &p2, &p3);

return 0;
}
import java.util.*;
class Solution
{

public static void main (String[]args)
{
Scanner sc = new Scanner (System.in);
int x1 = 1, y1 = 1;
int x2 = 2, y2 = 2;
int x3 = 3, y3 = 3;
int firstDiff =(int) Math.sqrt (Math.pow (x2 - x1, 2) + Math.pow (y2 - y1, 2));
int secondDiff =(int) Math.sqrt (Math.pow (x3 - x2, 2) + Math.pow (y3 - y2, 2));
int thirdDiff =(int) Math.sqrt (Math.pow (x3 - x1, 2) + Math.pow (y3 - y1, 2));

System.out.println (firstDiff + " " + secondDiff + " " + thirdDiff);
}
}