C Program Document everyline please The Euclidean distance b

C Program, Document everyline please

The Euclidean distance between points p and q is the length of the line segment connecting them in Cartesian system. If p = {p_1 p_2 p_n) and q = (q_1 q_2, ..., q_n) are two points in Euclidean n-space, then the distance (d) from p to q, or from q to p, in general, for an n-dimensional space, is d (p, q) = Squareroot (p_1 - q_1)^2 + (p_2 - q_2)^2 + + (p_i - q_i)^2 + + (p_n - q_n)^2. Write a C program to calculate the Euclidean distance for two points in 3-dimensional space. Assign your own test values as the coordinates of the points p and q for test examples. You are NOT allowed to use sqrt() and pow() functions of C math library. Develop your own functions for those. For square root, use Newton\'s method. You can find more about the method here. https://en.wikipedia.ora/wiki/Newton%27s_method#Square_ root_of_a_number 3-digit accuracy after the decimal point in all floating-point operations is enough.

Solution


// C code
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <assert.h>
#include <string.h>
#include <ctype.h>
#include <inttypes.h>
#include <stdbool.h>
#include <unistd.h>

// function sqaure root using newtons method
double squareRoot(double number)
{
double temp1, result;

temp1 = 1; result = number;
while (temp1 < result)
{
temp1 = temp1*2;
result = result/2;
}

do
{
result = temp1;
// recurrence formula
double t = (number / temp1 + temp1);
temp1 = t/2;
} while (temp1 < result);

return result;
}


int main()
{
double p1,p2,p3,q1,q2,q3;

printf(\"Enter p1: \");
scanf(\"%lf\",&p1);
printf(\"Enter p2: \");
scanf(\"%lf\",&p2);
printf(\"Enter p3: \");
scanf(\"%lf\",&p3);
printf(\"Enter q1: \");
scanf(\"%lf\",&q1);
printf(\"Enter q2: \");
scanf(\"%lf\",&q2);
printf(\"Enter q3: \");
scanf(\"%lf\",&q3);

double distance = (p1-q1)*(p1-q1) + (p2-q2)*(p2-q2) + (p3-q3)*(p3-q3);
distance = squareRoot(distance);

printf(\"Euclidean distance: %0.3lf\ \", distance);
return 0;
}

/*
output;


Enter p1: 1
Enter p2: 2
Enter p3: 3
Enter q1: 2
Enter pq2: 3
Enter q3: 4

Euclidean distance: 1.732

*/

C Program, Document everyline please The Euclidean distance between points p and q is the length of the line segment connecting them in Cartesian system. If p =
C Program, Document everyline please The Euclidean distance between points p and q is the length of the line segment connecting them in Cartesian system. If p =

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site