MPATE-GE 2618: C Programming for Music Technology Unit 1.1
What is an algorithm? An algorithm is a precise, unambiguous procedure for producing certain results (outputs) from given data (inputs). It is a method for accomplishing a task or solving a problem.
Goals Learn to develop algorithms Learn to decompose problems into smaller, solvable parts in an efficient way. Learn about data structures. Learn to express algorithms in C (programming). Learn how to program audio applications.
Organization & Introduction Today s outline Course organization Introduction to data representation, C, and Unix Course website: http://www.nyu.edu/projects/farbood/2618 Problem sets Plan on problem sets averaging 10-15 hours per week, possibly more Four types of problems in assignments: Programming (coding) Developing algorithms (design) Reading code Using tools Assessment: Lecture Best 6 out of 7 problem sets 65% Best 2 out of 3 quizzes 20% Final project 15 % Assessment: Lab Attendance 30% Problem set average (best 6 out of 7) = 70%
Office hours Instructor email: mfarbood@nyu.edu TA: Oriol (Uri) Nieto: onc202@nyu.edu Office hours: Tuesday, 10am-12pm, Education building, Rm. 630 ursday, 11am-1pm, Education building, Rm. 624 Office hours start next week Please email both instructor and TA whenever you have any question.
Problem sets and platforms Assignments were developed with a Unix-based system in mind, in particular Mac OS X, because most music technology students prefer to use Macs. However, all assignments (and later on in the course, audio APIs utilized) are cross-platform. Quick Poll: how many students are using Mac OS? Windows? Linux?
Using the terminal What is bash? It s a shell, or a command line interface that lets you interact with the OS. at s what you re using when you type into your terminal window. How do you set up your environment? What is a prompt? To type a file name with a space on the command line, use the escape characer \ before it. Example: cd My\ Docs cd s to the directory My Docs.
Unix Mac OS X is built on a Unix (BSD) core It comes with the GNU C compiler (gcc) that we ll be using for the course (make sure you download XCode) Traversing the Unix file system directory commands cd changes current directory to home directory cd <dir> changes current directory to specified directory cd.. go up one directory level ls list the contents of present working directory pwd show path to current directory mkdir <dir> make a new directory rmdir <dir> delete a directory
File commands Unix continued cat <filename> prints contents of file less <filename> same as cat but shows one screenful at a time cp <src> <dest> copy file <src> to <dest> mv <src> <dest> move or rename <src> to <dest> touch <filename> creates an empty new file called <filename> emacs <filename> opens a new file in the editor emacs
Some Unix questions How does the shell know where to find the programs you type? See what your path is echo $PATH Why don t I see all my files when I type ls? What are command flags? ls -af What is standard input and output? How do I save output to a file? <command> > <outputfilename> How do I find out more about a command? man <command name>
Programming: Some history In the beginning, computers could only be programmed in terms of binary numbers that corresponded directly to specific machine instructions and locations in the computer s memory. Not very human friendly. e next software advance occurred with the development of assembly language. Enabled the programmer to work with the machine on a higher level, using symbolic names for instructions. en came high-level languages like FORTRAN, in which programmers no longer had to concern themselves with the architecture of a particular computer. is resulted in the need for a compiler to translate the program into a form the computer could understand.
Compiling a C program: Basic outline Source code Human-readable instructions (your program) Format is text Source code is then translated by a compiler into machine code (binary) You can then run this program on your machine. Source code Machine code int x = 0;" x = y + z;" while (...)" Compiler 00100110101" 01101001001" 01011101000"
Compiling a C program: Detailed version Source code int x = 0;" x = y + z;" while (...)" Assembler Preprocessor Takes the assembly language produced by the compiler, and translates into object code Assembly instructions are turned into opcode (machine language codes that specifies a particular operation for the CPU) Assembly code Compiler movl %esp, %ebp" pushl %ebx" subl $36, %esp" Object code Machine code that is usually not directly executable (.o files) Linking e linker takes one of more object files and combines them into an executable (the linker is sometimes called the loader in Unix). External libraries used by your program are linked in at this stage. Object code Assembler 00100110101" (Library ref)" 01011101000" (Library ref)" Linker Libraries (object code) 11111001111" 01101001001" 00011010111" 00011111000" 01011111001" ld is the Unix linker Executable code 00100110101" 01101001001" 01011101000" 11001101111" 01011111001"
Structure of a C program /* This is a comment. Not a very useful one, though */" #include <stdio.h> int main (void) " {" "printf("hello World!\n"); " "return 0; " }"
Compiling a C program After creating a file, (in this case, we re calling it helloworld.c ) compile it and run it. hostname:user ~ % gcc helloworld.c hostname:user ~ % ls a.out* "helloworld.c hostname:user ~ %./a.out HelloWorld! hostname:user ~ %
Getting started Programming in C, Chapters 2-4, pp. 5-41 Absolute Beginner s Guide to C, Chapters 1-4, pp. 1-46 Install gcc and other Unix tools on your computer If you are using Windows, you must install Cygwin. If you are using MacOS, you might have to install XCode programming tools. If you are using Linux, you shouldn t need to install anything. Get acquainted with the Unix environment Learn how to navigate the Unix directory structure; experiment with basic Unix commands Get a feel for emacs (if you choose to use it as your text editor) see website for references online to help you get acquainted Go to website (under Lectures) and download the Getting Started document to help you set up your shell and emacs environments