C language programming (third edition) He Qinming exercises 5-3

C language programming (third edition) He Qinming exercises 5-3

List of exercises
1. C language programming (third edition) He Qinming exercises 2-1
2. C language programming (third edition) He Qinming exercises 2-2
3. C language programming (third edition) He Qinming exercises 2-3
4. C language programming (third edition) He Qinming exercises 2-4
5. C language programming (third edition) He Qinming exercises 2-5
6. C language programming (third edition) He Qinming exercises 2-6
7. C language programming (third edition) He Qinming exercises 3-1
8. C language programming (third edition) He Qinming exercises 3-2
9. C language programming (third edition) He Qinming exercises 3-3
10. C language programming (third edition) He Qinming exercises 3-4
11. C language programming (third edition) He Qinming exercises 3-5
12. C language programming (third edition) He Qinming exercises 4-1
13. C language programming (third edition) He Qinming exercises 4-2
14. C language programming (third edition) He Qinming exercises 4-3
15. C language programming (third edition) He Qinming exercises 4-4
16. C Language Programming (Third Edition) He Qinming with Exercises 4-5
17. C language programming (third edition) He Qinming exercises 4-6
18. C language programming (third edition) He Qinming exercises 4-7
19. C language programming (third edition) He Qinming exercises 4-8
20. C language programming (third edition) He Qinming exercises 4-9
21. C language programming (third edition) He Qinming exercises 4-10
22. C language programming (third edition) He Qinming exercises 4-11
23. C language programming (third edition) He Qinming exercises 5-1
24. C language programming (third edition) He Qinming exercises 5-2


topic

Use the function to calculate the distance between two points:
Given the coordinates of any two points on the plane (x1, y1) and (x2, y2),
find the distance between these two points (with 2 decimal places).
It is required to define and call the function dist (x1, y1, x2, y2) to calculate the distance between two points. Try to write the corresponding program.


Analysis process

enter

Condition: Enter (x1, y1) and (x2, y2)

Output

Condition: the distance between the two points (retaining 2 decimal places), the function dist (x1, y1, x2, y2) is required to be defined and called to calculate the distance between the two points

Code

#include <stdio.h>
#include <math.h>
double dist(double x1, double y1, double x2, double y2);/*函数声明*/

int main () {
    
    
	/*定义变量*/
	double x1, y1, x2, y2;                                                      /*定义变量,存储输入的(x1,y1)和(x2,y2)*/
	/*赋值*/
	printf("请输入(x1,y1)和(x2,y2):\n");                                 	/*输入提示*/
	scanf("%lf %lf %lf %lf \n", &x1, &y1, &x2, &y2);                            /*输入并赋给变量*/
	
    /*计算*/
    printf("(%lf %lf)和(%lf %lf)之间的距离为%.2f \n", x1, y1, x2, y2, dist(x1, y1, x2, y2)); /*输出计算结果*/
	return 0;
}

double dist(double x1, double y1, double x2, double y2){
    
    
    return sqrt((pow(fabs(x1-x2), 2) + pow(fabs(y1-y2), 2)));
    
}

operation result

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_43228814/article/details/112507817