{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

LEC20081114

# LEC20081114 - Introduction to Computer Programming CSC180...

This preview shows pages 1–7. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Introduction to Computer Programming November 14, 2008 CSC180 Fall 2008, University of Toronto Pointers to functions Every function has an address. Pointers to functions can be assigned, placed in arrays, passed to functions, and returned by functions. Example: /* funptr.c */ #include &amp;lt;math.h&amp;gt; #include &amp;lt;stdio.h&amp;gt; double add(double a, double b) { CSC180 Fall 2008, University of Toronto 1 return a+b; } double sub(double a, double b) { return a-b; } double mul(double a, double b) { return a*b; } double div(double a, double b) { return a/b; } typedef double (*Funcptr)(double, double); Funcptr getop(char c) { if (c == +) CSC180 Fall 2008, University of Toronto 2 return add; else if (c == -) return sub; else if (c == *) return mul; else if (c == /) return div; else return NULL; } #define N 1e6 /* a simplistic method for computing a functions integral */ double integrate(double a, double b, double (*f)(double)) { register double sum; double d, x; int i; sum = 0.0; d = (b - a)/N; for (i = 0; i &amp;lt; N; i++) { CSC180 Fall 2008, University of Toronto 3 x = a + i*d; sum += (*f)(x) * d; } return sum; } double root(double x) { return sqrt(x); } double square(double x) { return x*x; } double cube(double x) { return x*x*x; } double linear(double x) CSC180 Fall 2008, University of Toronto 4 { return x; } double gaussian(double x) { return exp(-1.0*x*x); } int main() { int i; double (*f)(double, double); double (*g[4])(double, double) = {add, sub, mul, div};...
View Full Document

{[ snackBarMessage ]}

### Page1 / 15

LEC20081114 - Introduction to Computer Programming CSC180...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online