1 / 33

C Programming and Data Structures: Foundations for Computer Science

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.

sharda
Download Presentation

C Programming and Data Structures: Foundations for Computer Science

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

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

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

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

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

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

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

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

  11. Any questions before we start?

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

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

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

  15. 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”?

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

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

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

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

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

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

  22. 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; }

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

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

  25. 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; }

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

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

  28. 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; }

  29. 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; }

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

  31. 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; }

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

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