330 likes | 343 Views
Learn key elements of C programming, data structures design, efficiency principles, and more. Prepare for future computer courses & improve programming skills. Course by Baojian Hua.
E N D
Overview C and Data Structures Baojian Hua bjhua@ustc.edu.cn
What is this course about? • Key elements of C programming: • C is the most widely-used system language • Linux, Windows, network server, compiler, … • C is low-level • Device driver, embedded (real-time) system, … • Closely related with architectures • Motivating ideas for other languages • C++/Java/C# etc.
What is this course about? • Data structures design and impl’ • Specific topics include: • List, array, stack, queue, tree, graph, hash, sorting, searching, etc. • General principals in designing and analyzing data structures • Tradeoffs between space and time • Efficiency and clarity, etc. • Design new data structures!
What’s the goal of this course? • Knowledge preparation • C and data structures are crucial for CS • Foundations for later computer courses • Familiarize you with computer thinking • e.g.: abstraction (ADT), recursion • Improve your programming skills • More experience in programming • Emphasis on modularity, ADT, and clarity
Administrivia • Instructor: Hua, Baojian • bjhua@ustc.edu.cn • Office hour: at every class, or to appoint • TA: Xi, Jing • 68839304 • jingxi@ustc.edu.cn • 304, Mingde building
Course Home Page • Home page http://staff.ustc.edu.cn/~bjhua/courses/summer10 • Course administrivia • Lecture notes • Programming assignments • Software • Test and evaluation • Check course home page everyday
Textbooks and References • The C Programming Language (Second Edition) . Kernighan and Ritchie, 1988. • C: A Reference Manual, Harbison and Steele, 2002. • The Practice of Programming, Kernighan and Pike, 1999. • Programming with GNU Software, Loukides and Oram, 1997. • Fundamentals of data structures in C. Ellis Horowitz et al. 2006. • Algorithm in C. Rdbert Sedgewick. Addison-Wesley Professional; 3 edition. 1997.
Labs • There are 5 labs (tentative) • Each consists of some required problems and optional problems (Optional) • Optional assignments are not required, but highly recommended • Solve them independently • Late homework should only be considered under extraordinary circumstances • Submit to Xi, Jing
Labs • This year we use gcc • a Windows nice IDE call CodeBlocks • Version 4.1 of gcc, compiles C99 • In another year, we used Microsoft’s Visual Studio, but… :-( • So if you’d like to use other compilers, come to talk with us in advance
Test and Evaluation • There is a final test: • Close book • Cover all materials in the course • Evaluation: 50% labs + 50% test • Be concerned • this course is more profitable and illuminating (and exciting) than you may assume
What are programming languages? • Machines only understand “01” • so we must program with “01”? • but early programmers do this! (crazy!) • Program languages offer a high-level view of computer • another level of indirection, • more and more abstract • Software toolchain helps to do the mapping
The C Programming Language • Capsule history: • BCPL B C K&R C ANSI C C99 1960 1970 1972 1978 1988 • LISP Smalltalk C++ Java 1960’s 1970’s 1980’s 1994 C# 2000
The C Programming Language • C is a system programming language • Originally used to write Unix • and later Linux and Windows • Data types and control structures close to most machines • So you know system deeper, you can program better • Pros and cons: • Can do whatever you want: flexible and powerful • Can do whatever you do NOT want: shoot yourself in the foot
First Program int main () { return 0; } // 1. Compile // 2. run // 3. OS takes over, and runs it on machine // 4. your program runs // 5. return back to OS // 6. finished! Why the name is “main”? Why “0”?
A bit more #include <stdio.h> int main () { printf(“hello, world\n”); return 0; } What’s “stdio.h”? And where is it? What’s “#include”? What’s “printf” doing? Why bother to use it?
Variables and Arithmetic Expressions • Print the table of Fahrenheit temperatures and their centigrade or Celsius equivalents: C=(5/9)*(F-32) F: C: 0 -17 20 -6 40 4 60 15 80 26 100 37 120 48 140 60 160 71 180 82 200 93
Program #include <stdio.h> int main() { int f, c; f = 0; c = 5 * (f-32) / 9; printf("%d\t%d\n", f, c); return 0; } F: C: 0 -17 20 -6 40 4 60 15 80 26 100 37 120 48 140 60 160 71 180 82 200 93
Program #include <stdio.h> int main() { int f, c; for (f=0; f<=200; f+=20) { c = 5 * (f-32) / 9; printf("%d\t%d\n", f, c); } return 0; } F: C: 0 -17 20 -6 40 4 60 15 80 26 100 37 120 48 140 60 160 71 180 82 200 93
Data Types • In C, the key word “int” stands for integer types • Its range depends on the machine, typical 16 or 32 bits • C provides other data types:
Assignment • Assignment statement: • x=e; • x is a variable, and e is an expression • Meaning: set x the value of e • Ex: • f = 0; • c = 5 * (f - 32) / 9;
f=0; f<=200 …; f +=20; For Statement #include <stdio.h> int main() { int f, c; for (f=0; f<=200; f+=20) { c = 5 * (f-32) / 9; printf ("%d\t%d\n", f, c); } return 0; }
More on printf printf ("%d\t%d\n", f, c); • Format string: • Control how the data to output • %d means output an integer • See the text for a complete list and their meanings • Escape character: • ‘\t’, ‘\n’ • It’s one of the so-called IO functions • More on this later
Function • A function in C, is just like: • subroutine in Fortran • procedure or function in Pascal • methods in Java or C# • A function provides a convenient way to: • encapsulate information • modularize system (along with others) • reuse code
Example // calculate circle area, #1 try #include <stdio.h> int main() { double area; int r; r = 5; area = 3.14 * r * r; printf (“%lf\n”, area); return 0; }
Or #include <stdio.h> int main() { double area; int r; for (r = 0; r<10; r++) { area = 3.14 * r * r; printf (“%lf\n”, area); } return 0; }
Problem #1:Code Duplication // calculate circle area, 1st try int main() { double area; int r; for (r = 0; r<10; r++) area = 3.14 * r * r; for (r = 10; r<20; r++) area = 3.14 * r * r; return 0; }
Problem #2:Hard to Maintain // calculate circle area, 1st try int main() { double area; int r; // Say, want to change to 3.1415926 for (r = 0; r<10; r++) area = 3.14 * r * r; for (r = 10; r<20; r++) area = 3.14 * r * r; return 0; }
Problem #3:Client Transparent // calculate circle area, 1st try int main() { double area; int r; // We really don’t want to know this … for (r = 0; r<10; r++) area = 3.14 * r * r; for (r = 10; r<20; r++) area = 3.14 * r * r; return 0; }
Summary So Far • Code duplication • Same code anywhere • remember: good programmers are lazy • Hard to maintain and evolve • Must touch all parts of a big entity • Client Transparent • Essentially, we want the interface, not the implementation details
function prototype function definition function body, as we have discussed for main() implicit conversion function call with argument r Using Function double area (int r); double area (int r) { double pi = 3.14; return (pi*r*r); } int main() { double a; int r; for (r=0; r<10; r++) a = area(r); return 0; }
Function Summary • Function prototype: return-type function-name (parameter declarations, if any) ; • Function definition: return-type function-name (parameter declarations, if any) { declarations statements } • Function call: function-name (expressions, if any);
decrement operation, n=n-1 type void, no value the value of n? Call-by-value void foo (int n); void foo (int n) { n--; return; } int main () { int n = 9; foo (n); printf (“%d\n”, n); return 0; } 9 8 9