-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.c
51 lines (42 loc) · 1.1 KB
/
2.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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void tabulate(float a, float b, float h, void (*func)(float, float, float));
void tabulate_while(float a, float b, float h);
void tabulate_for(float a, float b, float h);
void tabulate_dowhile(float a, float b, float h);
float fn(float x);
int main(void) {
float a = 1., b = 9., h = 1.;
tabulate(a, b, h, tabulate_for);
tabulate(a, b, h, tabulate_while);
tabulate(a, b, h, tabulate_dowhile);
}
float fn(float x) {
return x * cbrt(1. - x);
}
void tabulate(float a, float b, float h, void (*function)(float, float, float)) {
function(a, b, h);
}
void tabulate_for(float a, float b, float h) {
printf("Tabulate (for):\n");
for(float x = a; x <= b; x += h) {
printf("| %5.1f | %5.1f |\n", x, fn(x));
}
}
void tabulate_while(float a, float b, float h) {
printf("Tabulate (while):\n");
float x = a;
while(x <= b) {
printf("| %5.1f | %5.1f |\n", x, fn(x));
x += h;
}
}
void tabulate_dowhile(float a, float b, float h) {
printf("Tabulate (do while):\n");
float x = a;
do {
printf("| %5.1f | %5.1f |\n", x, fn(x));
x += h;
} while(x <= b);
}