-
Notifications
You must be signed in to change notification settings - Fork 124
/
exercise06.c
66 lines (52 loc) · 1.16 KB
/
exercise06.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// C Primer Plus
// Chapter 9 Exercise 6:
// Write and test a function that takes the addresses of three double variables
// as arguments and that moves the value of the smallest variable into the
// first variable, the middle value to the second variable, and the largest
// value into the third variable.
#include <stdio.h>
void sort_variables(double *x, double *y, double *z);
int main(void)
{
double x, y, z;
printf("Test sort_variables():\n");
printf("Enter three numbers x, y and z:\n");
while(scanf("%lf %lf %lf", &x, &y, &z) == 3)
{
putchar('\n');
printf("Before calling sort_variables:\n");
printf("x = %f, y = %f, z = %f\n", x, y, z);
sort_variables(&x, &y, &z);
putchar('\n');
printf("After calling sort_variables:\n");
printf("x = %f, y = %f, z = %f\n", x, y, z);
putchar('\n');
printf("Enter three numbers x, y and z:\n");
}
return 0;
}
void sort_variables(double *x, double *y, double *z)
{
double tmp;
if (*x > *y)
{
// switch x and y
tmp = *y;
*y = *x;
*x = tmp;
}
if (*y > *z)
{
// switch y and z
tmp = *z;
*z = *y;
*y = tmp;
if (*x > *y)
{
// switch x and y
tmp = *y;
*y = *x;
*x = tmp;
}
}
}