Your first program is a simple mathematical calculation program. As we know, the
ID: 3849490 • Letter: Y
Question
Your first program is a simple mathematical calculation program. As we know, the straight-line distance between two points in the Cartesian coordinate plane, namely (x_1, y_1) and (x_2, y_2), is given by: d = squareroot (x_1 - x_2)^2 + (y_1 - y_2) is given by d = squareroot (x_1-x_2)^2 + (y1_y_2)^2 Write a C program that gets 4 integer values from the user, namely, x_1, x_2, y_1, and y_2, and then calculates and returns the distance according to the above formula. Your program should prompt the user for these values, accept input from the keyboard, and then print out the result. Include a screenshot that shows the results for the following values: (2, 3), (4, 5) (4, 3), (6, 5) (1, 3) (4, 2) Note that these inputs are (x, y) pairs. You may use the function double squareroot (double), found in to calculate the Square root.Explanation / Answer
Code:
#include <stdio.h>
#include <math.h>
/* distance between two points */
int main()
{
/* declaring variables */
int x1, x2, y1, y2;
double dist;
/* fetching inputs to calculate distance between two points */
printf(" Enter the integer value of x1? ");
scanf("%d", &x1);
printf(" Enter the integer value of y1? ");
scanf("%d", &y1);
printf(" Enter the integer value of x2? ");
scanf("%d", &x2);
printf(" Enter the integer value of y2? ");
scanf("%d", &y2);
/* distance formula is distance = sqrt((x1-x2)^2 + (y1-y2)^2). the '^' symbol is equivalent to pow function below */
dist = sqrt(pow((x1-x2), 2) + pow((y1-y2), 2));
printf(" Distance between two points is: %f ", dist);
return 0;
}
Execution and output:
Unix Terminal> ./a.out
Enter the integer value of x1? 1
Enter the integer value of y1? 3
Enter the integer value of x2? 4
Enter the integer value of y2? 2
Distance between two points is: 3.162278
Unix Terminal> ./a.out
Enter the integer value of x1? 4
Enter the integer value of y1? 3
Enter the integer value of x2? 6
Enter the integer value of y2? 5
Distance between two points is: 2.828427
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.