1 / 27

Overview

Overview. C and Data Structures Baojian Hua bjhua@ustc.edu.cn. What is this course about?. Learn key elements of C language C is the most widely-used system language Linux, Windows, network server, compiler, … C is low-level Device driver, embedded (real-time) system, …

ena
Download Presentation

Overview

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Overview C and Data Structures Baojian Hua bjhua@ustc.edu.cn

  2. What is this course about? • Learn key elements of C language • C is the most widely-used system language • Linux, Windows, network server, compiler, … • C is low-level • Device driver, embedded (real-time) system, … • The interplay with architecture • Motivate ideas for other languages • Especially the C++/Java/C# etc.

  3. What is this course about? • Understand elementary data structures • Topics include: • List, array, stack, queue, tree, graph, hash, sorting, searching, etc. • General principals in designing and understanding data structures • Tradeoffs between space and time, efficiency and clarity, etc. • Design new data structures

  4. What’s the Goal of this Course? • Knowledge preparation • C and data structures are crucial preliminaries for later computer courses • Familiarize you with computer thinking • Ex: abstraction, recursion • Improve your programming skills • More experience in programming • Emphasis on modularity, ADT and clarity

  5. Get Help • Instructor: Hua, Baojian • 87161325 • bjhua@ustc.edu.cn • 307, Mingde buiding • Office hour: at every class, or to appoint • TA: Xi, Jing • 68839200 • jingxi@ustc.edu.cn • 234, Mingde building • Office hour: at every class

  6. Course Page • Home page http://ssg.ustcsz.edu.cn/~hbj/summer08 • Course administrative • Lecture notes • Programming assignments • Softwares • Test and evaluation • Check that page frequently

  7. 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.

  8. Homework • There are 9 programming assignments plus some write-on-paper homework • Optional assignments are not required, but highly recommended • Solve them independently • Late homework should only be considered under extraordinary circumstances • Submit to Xi, Jing (jingxi@ustc.edu.cn)

  9. Test and Evaluation • There is a final test: • Close book • Cover all materials in the course • Evaluation: 30% homework + 70% test • Be concerned • this course is more profitable and illuminating (and exciting) than you may assume

  10. Any questions before we start?

  11. The C Programming Language • C is system programming language • Originally used to write Unix • Data types and control structures close to most machines • Grow into popular general-purpose language • Pros and cons: • Can do whatever you want: flexible and powerful • Can do whatever you do NOT want: shoot yourself in the foot • History: • BCPL  B  C  K&R C  ANSI C  C99 1960 1970 1972 1978 1988 • LISP  Smalltalk  C++  Java

  12. we need the function printf to complete the task Comments. Has nothing to do with program run, but for documentation purpose. main() is the program entry point, so every program must have a main() somewhere every statement terminated with ; every function begins with {, and ends with } function argument, \n is newline • return statement. the convention for main: • 0 for normal return • 1 for error case First Program /* compile command: gcc –ansi first.c */ #include <stdio.h> int main () { printf(“hello, world\n”); return 0; }

  13. 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

  14. Program #include <stdio.h> int main() { int f, c; int lower, upper, step; lower = 0; upper = 200; step = 20; f = lower; while (f <= upper) { c = 5 * (f-32) / 9; printf("%d\t%d\n", f, c); f = f + step; } 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

  15. 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:

  16. Assignment • Assignment statement: • x=e; • x is a variable, and e is an expression • Meaning: set x the value of e • Ex: • lower = 0; • upper = 200; • c = 5 * (f - 32) / 9;

  17. while while (f <= upper) { ... } • Meaning: • if true then enter the loop, execute the statements; and back to the loop condition • if false, then skip the loop body

  18. the condition f<=upper is tested if true, then enter loop execute loop body, then go back to loop test while while (f <= upper) { //stms; } f<=upper N Y if false; skip the loop // stms;

  19. 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’ • more on this later

  20. f=0; f<=200 printf(…); f=f+20; start from here then this then this then this then this then this For Statement #include <stdio.h> int main() { int f; for (f = 0; f <= 200; f = f + 20) { printf("%d\t%d\n", f, 5*(f-32)/9); } return 0; }

  21. More on Function • A function in C, is just like: • subroutine in Fortran • procedure or function in Pascal • methods in Java • A function provides convenient way to: • encapsulate information • modularize system (along with others) • reuse code

  22. Example // calculate circle area, #1 try #include <stdio.h> int main() { float area; int r; r = 5; area = 3.14 * r * r; printf (“%f\n”, area); return 0; }

  23. Or #include <stdio.h> int main() { float area; int r; for (r = 0; r<10; r++) { area = 3.14 * r * r; printf (“%f\n”, area); } return 0; }

  24. Code Duplication // calculate circle area, 1st try int main() { float area; int r; for (r = 0; r<10; r++) area = 3.14 * r * r; // hard to modify, say, to 3.14159 for (r = 10; r<20; r++) area = 3.14 * r * r; return 0; }

  25. function prototype function definition function body, as we have discussed for main() implicit conversion function call with argument r Using Function float area(int r); float area(int r) { float pi = 3.14; return (pi * r * r); } int main() { float a; int r; for (r=0; r<10; r++) a = area(r); return 0; }

  26. 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);

  27. 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

More Related