LEC20081114 - Introduction to Computer Programming November...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
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 <math.h> #include <stdio.h> 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 < 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

This note was uploaded on 01/10/2011 for the course CSC 180 taught by Professor Na during the Fall '01 term at University of Toronto- Toronto.

Page1 / 15

LEC20081114 - Introduction to Computer Programming November...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online