Skip to content

Commit c4d827c

Browse files
committed
Complete C boot camp materials
1 parent c24557e commit c4d827c

File tree

11 files changed

+721
-128
lines changed

11 files changed

+721
-128
lines changed

lessons/C Bootcamp.ipynb

Lines changed: 606 additions & 111 deletions
Large diffs are not rendered by default.

lessons/buggy.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
# Create a function pointer type that takes a double and returns a double
3+
double *func(double x);
4+
5+
# A higher order function that takes just such a function pointer
6+
double apply(func f, double x)
7+
{
8+
return f(x);
9+
}
10+
11+
double square(double x)
12+
{
13+
return x * x;
14+
}
15+
16+
double cube(double x)
17+
{
18+
return pow(3, x);
19+
}
20+
21+
double mystery(double x)
22+
{
23+
double y = 10;
24+
if (x < 10)
25+
x = square(x);
26+
else
27+
x += y;
28+
x = cube(x);
29+
return x;
30+
}
31+
32+
int main()
33+
{
34+
double a = 3;
35+
func fs[] = {square, cube, mystery, NULL}
36+
37+
for (func *f=fs, f != NULL, f++) {
38+
printf("%d\n", apply(f, a));
39+
}
40+
}

lessons/makefile

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
1-
TARGET=_apop.so
2-
OBJECTS=ftest.o ftest_wrap.o
3-
CFLAGS=-I/usr/include -I/usr/local/Cellar/gsl/1.16/include -I/Users/cliburn/anaconda/include/python2.7 -g -Wall -Wno-self-assign -O3
4-
LDLIBS=-bundle -lgsl -lapophenia -lpython2.7
5-
CC=clang
1+
TARGET = test_make
2+
OBJECTS = stuff.o
3+
CFLAGS = -g -Wall -O3
4+
LDLIBS = -lm
5+
CC = clang
66

7-
SWIGTARGET=apop.py ftest_wrap.c
8-
SWIGOBJECTS=ftest.i
9-
SWIG=swig
10-
SWIGFLAGS=-python
11-
12-
$(TARGET): $(OBJECTS) $(SWGTARGET)
13-
$(CC) $(CFLAGS) $(LDLIBS) -o $(TARGET) $(OBJECTS)
14-
15-
$(SWIGTARGET): $(SWIGOBJECTS)
16-
$(SWIG) $(SWIGFLAGS) $(SWIGOBJECTS)
17-
7+
all: $(TARGET)
8+
189
clean:
19-
rm $(SWIGTARGET) $(OBJECTS) $(TARGET)
10+
rm $(TARGET) $(OBJECTS)
11+
12+
$(TARGET): $(OBJECTS)

lessons/obs

4.21 KB
Binary file not shown.

lessons/obs.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
main() {int b, c; int a=(b=2, c=3, a<0) ? (b+=3, b-1) : (c+=b, c+2);}

lessons/square_buggy.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
// Create a function pointer type that takes a double and returns a double
3+
double *func(double x);
4+
5+
// A higher order function that takes just such a function pointer
6+
double apply(func f, double x)
7+
{
8+
return f(x);
9+
}
10+
11+
double square(double x)
12+
{
13+
return x * x;
14+
}
15+
16+
double cube(double x)
17+
{
18+
return pow(x, 3);
19+
}
20+
21+
int main()
22+
{
23+
double a = 3;
24+
func fs[] = {square, cube, NULL};
25+
26+
for (func *f=fs; f != NULL; f++) {
27+
printf("%d\n", apply(*f, a));
28+
}
29+
}

lessons/stuff.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include "stuff.h"
2+
3+
void do_stuff() {
4+
printf("The square root of 2 is %.2f\n", sqrt(2));
5+
}

lessons/stuff.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#include <stdio.h>
2+
#include <math.h>
3+
4+
void do_stuff();

lessons/test_make.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include "stuff.h"
2+
3+
int main()
4+
{
5+
do_stuff();
6+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>CFBundleDevelopmentRegion</key>
6+
<string>English</string>
7+
<key>CFBundleIdentifier</key>
8+
<string>com.apple.xcode.dsym.test_make</string>
9+
<key>CFBundleInfoDictionaryVersion</key>
10+
<string>6.0</string>
11+
<key>CFBundlePackageType</key>
12+
<string>dSYM</string>
13+
<key>CFBundleSignature</key>
14+
<string>????</string>
15+
<key>CFBundleShortVersionString</key>
16+
<string>1.0</string>
17+
<key>CFBundleVersion</key>
18+
<string>1</string>
19+
</dict>
20+
</plist>

0 commit comments

Comments
 (0)