-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex09_05.c
55 lines (46 loc) · 1.05 KB
/
ex09_05.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
/*
* pr_pset09_05:
*
* Write and test a function called larger_of() that replaces the contents
* of two double variables with the maximum of the two values.
* For example, larger_of(x,y) would reset both x and y to the larger
* of the two.
*/
#include <stdio.h>
#include <stdbool.h>
// Prompt user with name of variable
double get_num(char c);
// Main function which replaces values of variables
void larger_of(double * u, double * v);
int main(void)
{
double x, y;
x = get_num('x');
y = get_num('y');
printf("Was x = %g and y = %g\n", x, y);
larger_of(&x, &y);
printf("Now x = %g and y = %g\n", x, y);
printf("Bye.\n");
return 0;
}
double get_num(char c)
{
char retry;
double num;
printf("Enter %c: ", c);
while (scanf("%lf", &num) != 1)
{
while ((retry = getchar()) != '\n')
putchar(retry);
printf(" is not a number.\n");
printf("Retry: ");
}
return num;
}
void larger_of(double * u, double * v)
{
if (*u > *v)
*v = *u;
else
*u = *v;
}