Stuff to look at
- Data Representation spread sheet
- Carnegie-Mellon CMU 15-213
Requirements for programming assignment
- Always use standard C99
- Always use a Makefile
- Even if you use NetBeans or Eclipse or Xcode, create a simple Makefle
- Test your Makefle
- Do not submit a NetBeans Makefile!
- Put your entire project (without the junk files) in either a zip or tar.gz file.
Tips on programming
- Start early
- Do a little each day
- Test incrementally
- Figure it out yourself
- Use debugging statements
const int debug = 1 ; … if (debug) { printf("i is %d, v[i] is %f\n", i, v[i]) ; }
#define DEBUG 1 … #ifdef DEBUG printf("i is %d, v[i] is %f\n", i, v[i]) ; #endif
- Use gdb.
When things go wrong
Read the compiler error messages and fix them yourself. The compiler cannot determine your intent, but it can tell you where your program goes wrong. Fix your errors one at a time.
$ make
collatz.c: In function ‘collatz’:
collatz.c:9:13: error: expected ‘;’ before ‘n’
n = 3 n + 1 ;
^
$ make
collatz.c: In function ‘collatz’:
collatzc:6:15: error: lvalue required as left operand of assignment
if (n % 2 = 0) {
^
$ make
collatz.c: In function ‘collatz’:
collatz.c:7:11: error: ‘m’ undeclared (first use in this function)
n = m / 2 ;
^
collatz.c:7:11: note: each undeclared identifier is reported only once for each function it appears in
$ make
collatz.c: In function ‘print_table’:
collatz.c:29:5: warning: unknown conversion type character 0xa in format [-Wformat=]
fprintf(stdout, "%4d --> %8\n", i, table[i]) ;
^
collatz.c:29:5: warning: too many arguments for format [-Wformat-extra-args]
Registers and variables
Take a look at an old CSCI 320 homework that shows code in both the MIPS32 and x86-64 instruction sets. Notice the heavy use of registers for storing variables. Now look at the annotated code.