#include <stdio.h> int main(int argc, char *argv[]) { int i; printf("argc = %d\n", argc); for (i = 0; i < argc; ++i) printf("argv[%d] = %s\n", i, argv[i]); return 0; } |
#include <stdio.h> int main(void) { char a[2][15] = {"abc:", "a is for apple"}; char *p[2] = {"abc:", "a is for apple"}; printf("%c%c%c %s %s\n%c%c%c %s %s\n", a[0][0], a[0][1], a[0][2], a[0], a[1], p[0][0], p[0][1], p[0][2], p[0], p[1]); return 0; } |
abc abc: a is for apple abc abc: a is for apple |
f(x)2
, x=m,...,n
, f(x)
is passed as a parameter
#include <stdio.h> #include <math.h> double sum_square(double (*f)(double), int m, int n) { int k; double sum = 0.0; for (k = m; k <= n; ++k) sum += f(k) * f(k); return sum; } double f(double x) { return 1.0 / x; } int main(void) { printf("%s%.7f\n%s%.7f\n", " First computation: ", sum_square(sin, 2, 13), "Second computation: ", sum_square(f, 1, 10000)); return 0; } |
gcc -Wall -pedantic sum_square.c -o sum_square -lm |
First computation: 5.7577885 Second computation: 1.6448341 |
1/k2 = 2/6
#include <stdio.h> /* epsilon */ #define EPS 1e-12 double p(double); double root(double (*)(double), double, double); int main(void) { double x; x = root(p, 0.0, 3.0); printf("%s%.16f\n%s%.16f\n", "Approximate root: ", x, " Function value: ", p(x)); return 0; } double root(double f(double), double a, double b) { double m = (a + b) / 2.0; /* midpoint */ if (f(m) == 0.0 || b - a < EPS) return m; else if (f(a) * f(m) < 0.0) return root(f, a, m); else return root(f, m, b); } /* a polynomial p */ double p(double x) { return (x * x * x * x * x - 7.0 * x - 3.0); } |
Approximate root: 1.7196280914844237 Function value: -0.0000000000012403 |
static const int k = 3; const int a = 7; int *p = &a; const int a = 7; const int *p = &a; int a = 7; int * const p = &a; int a=7; const int * const p = &a; |
For example, if you have only one ball, you must start your experiments from the first floor. In the worst case you will end up performing an experiment every floor just to find out that there is no critical floor. Given one ball the minimum number of experiments required in the worst case is f.