1 / 69

ecs30 Winter 2012: Programming and Problem Solving #01: Chapters 1~2

ecs30 Winter 2012: Programming and Problem Solving #01: Chapters 1~2. Dr. S. Felix Wu Computer Science Department University of California, Davis http://www.cs.ucdavis.edu/~wu/ wu@cs.ucdavis.edu. About the Instructor. S. Felix Wu wu@cs.ucdavis.edu Facebook group: ecs30

duscha
Download Presentation

ecs30 Winter 2012: Programming and Problem Solving #01: Chapters 1~2

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. ecs30 Winter 2012:Programming and Problem Solving#01: Chapters 1~2 Dr. S. Felix Wu Computer Science Department University of California, Davis http://www.cs.ucdavis.edu/~wu/ wu@cs.ucdavis.edu ecs30 Winter 2012 Lecture #01

  2. About the Instructor • S. Felix Wu • wu@cs.ucdavis.edu • Facebook group: ecs30 • Office: 2109 Watershed • Phone: 919.443.5398 (Google Voice) • Office Hours: • 1-2 p.m. on Tue/Fri • by appointment ecs30 Winter 2012 Lecture #01

  3. ecs30 Winter 2012 Lecture #01

  4. ecs30 Winter 2012 Lecture #01

  5. Prerequisites • Math 16A or 21A (may be taken concurrently); prior experience with basic programming concepts (e.g., loop) recommended. ecs30 Winter 2012 Lecture #01

  6. Syllabus • Basic (1~2) • Function, Selection, & Repetition (3~6, 13) • Data Structure (7~9, 11, 14) • Recursion (10) • File (12) ecs30 Winter 2012 Lecture #01

  7. Problem Solving ecs30 Winter 2012 Lecture #01

  8. Course Requirements • 35%: Programming Assignments • 40%: Two close-book/note midterms • 25%: One close-book/note final ecs30 Winter 2012 Lecture #01

  9. Grading • I will give +/- grades. • possible grading : • A: >= 92 A-: >= 89 B+: >= 85 • B: >= 82 B-: >= 79 C+: >= 75 • C: >= 72 C-: >= 69 D+: >= 65 • D: >= 62 D-: >= 59 ecs30 Winter 2012 Lecture #01

  10. ecs30 is really about how to communicate with… ecs30 Winter 2012 Lecture #01

  11. ecs30 Winter 2012 Lecture #01

  12. ecs30 Winter 2012 Lecture #01

  13. #include <stdio.h> int main () { intx; /* declare x */ x = 1; printf("Hello world %d\n", x); return 0; }// main() ecs30 Winter 2012 Lecture #01

  14. Computer Programming basics • Computer Organization: • CPU (Central Processing Unit), Memory (mail box cells), I/O (Input Output devices) • Program: • A sequence of commands together achieving one goal. • Programming Language: • the language human use to communicate with computers. BTW, human use "natural languages" (e.g., English, Chinese...) to communicate among themselves. In this class, we learn how to use C to communicate with a Computer. • Compiler: • a tool that translates a program written in a high-level programming language into assembly/machine codes (low level). We need a C compiler (gcc) for C programs. ecs30 Winter 2012 Lecture #01

  15. ecs30b Fall 2008 Lecture #02

  16. ecs30b Fall 2008 Lecture #03

  17. { … } • { … } • {… { … {…}…} … {…}…} • int main (void) { … } • float myfunc (int x) { …; return f;} • { <declarations> <statements> } ecs30b Fall 2008 Lecture #02

  18. ecs30 winter 2012 Lecture #02

  19. printf("Hello world %d\n", x); scanf(“%lf”, &miles); Try this yourself – intx = 999; printf("Hello [%d %d]\n", x, &x); ecs30 winter 2012 Lecture #02

  20. Do I need to know exactly where to store/save the value of the variable “miles”? ecs30 winter 2012 Lecture #02

  21. ecs30b Fall 2008 Lecture #02

  22. Memory Cell Concept Again ecs30b Fall 2008 Lecture #02

  23. “Address” • Linguistics in C programming Language Where is THIS really located on my device? Then, we need a way to represent “address”! ecs30 winter 2012 Lecture #02

  24. “Address” • 32 bits or 64 bits address 01101001 10110001 11100000 10100111 0x69b1e0a7 ecs30 winter 2012 Lecture #02

  25. ecs30 Winter 2012 Lecture #01

  26. Binary Representation • Integer (int) 4 bytes • intx; • Double 8 bytes • double miles; • Character 1 byte • char c; • And, it depends on OS and hardware… ecs30 winter 2012 Lecture #03

  27. American Standard Code for Information Interchange ecs30 Winter 2012 Lecture #01

  28. ecs30 Winter 2012 Lecture #01

  29. Information Representation: We need Memory to store both Programs/Data. But How? bit: 0/1 physical memory. (BInary digiT) byte: 8 bits word: 32 bits Numbers (Decimal/Binary/Hexadecimal): How do we use bits to represent an integer. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ===> Decimal 0, 1 ===> Binary 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f ===> Hexadecimal ecs30b Fall 2008 Lecture #02

  30. Numbers: Base: 10, 2, 16 Binary {0,1} 0,1,2,3,4,5,6,7,8,9, 10,11, 0,1,2…,10,11,12,…99,100,101,102,… 0,1,10 (2), 11(3), 100 (4), 101(5), 110(6), 4 2 1 22, 21, 20 1 0 0 1 0 + 1 1 0 ecs30b Fall 2008 Lecture #02

  31. 0+1 = 1 1 20 1+1 = 10 2 21 10+1 = 11 3 11+1 = 100 4 22 100+1 = 101 5 101+1 = 110 6 110+1 = 111 7 111+1 = 1000 8 23 9 = 1+8 0001 1000 1001 001 010 100 111 001 100 101 7=1+2+4 5 = 1 + 4 ecs30b Fall 2008 Lecture #02

  32. ecs30 Winter 2012 Lecture #01

  33. 210 21 ecs30 Winter 2012 Lecture #01

  34. 210 29 28 27 23 24 25 26 24 +24 22 24 22 21 ecs30 Winter 2012 Lecture #01

  35. 210 29 28 27 23 24 25 26 24 +24 22 24 22 24 *24 21 ecs30 Winter 2012 Lecture #01

  36. 0 = 0000 = 0 * 8 + 0 * 4 + 0 * 2 + 0 * 1 1 = 0001 = 0 * 8 + 0 * 4 + 0 * 2 + 1 * 1 2 = 0010 = 0 * 8 + 0 * 4 + 1 * 2 + 0 * 1 3 = 0011 = 0 * 8 + 0 * 4 + 1 * 2 + 1 * 1 ....... 7 = 0111 = 0 * 8 + 1 * 4 + 1 * 2 + 1 * 1 8 = 1000 = 1 * 8 + 0 * 4 + 0 * 2 + 0 * 1 9 = 1001 = 1 * 8 + 0 * 4 + 0 * 2 + 1 * 1 10 (a) = 1010 = 1 * 8 + 0 * 4 + 1 * 2 + 0 * 1 11 (b) = 1011 = 1 * 8 + 0 * 4 + 1 * 2 + 1 * 1 12 (c) = 1100 = 1 * 8 + 1 * 4 + 0 * 2 + 0 * 1 13 (d) = 1101 = 1 * 8 + 1 * 4 + 0 * 2 + 1 * 1 14 (e) = 1110 = 1 * 8 + 1 * 4 + 1 * 2 + 0 * 1 15 (f) = 1111 = 1 * 8 + 1 * 4 + 1 * 2 + 1 * 1 Examples: 09FE (in Hex) == ? (in Binary) == ? (in Decimal) ecs30b Fall 2008 Lecture #02

  37. “Memory Box” Each “address-able” box is ONE Byte 1 byte = 8 bits 1 bit is 0 or 1 Binary representation ecs30 Winter 2012 Lecture #01

  38. Bytes • 1 byte == 8 bits • 1 K bytes = 1024 = 210 • 1 M bytes = 1024 * 1024 = 220 • 1 G bytes = 230 • 1 T bytes = 240 • 1 Pbytes = 250 ecs30 Winter 2012 Lecture #01

  39. “The story of X” int X; X, &X, *X, ((struct S *) X)->index, ((int *) X)[20], *(((int *) X)+20), ((double *) X)[20], ((struct S *) 0)->index,… ecs30 Winter 2012 Lecture #01

  40. Byte versus Double • Each Memory cell is 1 Byte • How about Double? • How will Double map to your hardware memory? ecs30 winter 2012 Lecture #02

  41. Arithmetic Operations in Binary/Bits? +/- 0001 + 0001 = 0010 bit: 0/1 physical memory. byte: 8 bits in Unix, “usually” an integer has 4 bytes, i.e., 32 bits. unsigned 0 ~ 4294967295 (i.e. 2 32 -1) signed -2147483648 ~ 2147483647 (i.e. -(231) ~ (231 - 1)) ecs30 winter 2012 Lecture #02

  42. How do I represent a character? (Internal coding) ASCII (American Standard Code for Information Interchange) use 7 bit to represent a character (might or might not on your keyboard) Example: 'A' 1100001 (in Byte, 0110001) (Appendix A: page 839) Decimal: 65, Hex: 41. Question: How many different characters can we represent? ecs30 winter 2012 Lecture #02

  43. Binary Representation • Integer (int) 4 bytes • Double 8 bytes • Character 1 byte ecs30 winter 2012 Lecture #02

  44. /* an example program for the sizeof operator in C */ #include <stdio.h> main() { printf("The size of an integer is %d.\n", sizeof(int)); printf("The size of a float is %d.\n", sizeof(float)); printf("The size of a double is %d.\n", sizeof(double)); printf("The size of a character is %d.\n", sizeof(char)); } The size of an integer is 4. The size of a float is 4. The size of a double is 8. The size of a character is 1. ecs30 winter 2012 Lecture #02

  45. Int 4 bytes 0000 0000 0000 0000 0000 0000 0000 0000 1111 1111 1111 1111 1111 1111 1111 1111 ecs30 Winter 2012 Lecture #01

  46. Character /* an example program for the sizeof operator in C */ #include <stdio.h> int main(void) { printf("The size of an integer is %d.\n", sizeof(int)); printf("The size of a float is %d.\n", sizeof(float)); printf("The size of a double is %d.\n", sizeof(double)); printf("The size of a character is %d.\n", sizeof(char)); } Output: The size of an integer is 4. The size of a float is 4. The size of a double is 8. The size of a character is 1. ecs30 winter 2012 Lecture #03

  47. printf("value = %d, address = %d\n", cats, &cats); ===> value = 3, address = -268438156; **** printf("value = %d, address = %x\n", cats, &cats); ===> value = 3, address = effff574; -268438156 (in decimal) == effff574 (in hex) aa = -268438156; aa = 0xeffff574; ecs30 winter 2012 Lecture #02

  48. ecs30 winter 2012 Lecture #02

  49. My Experience … • How we learn to communicate with a Computer in C. • you will not know everything clearly at one time. • you will never learn if you do not try. • you will observe the results from your trying. • you should observe the C examples in the textbook and how this language is used in various situations. You might not know why it is used “this” way (even after I explain to you), but just try to use it the same way in the examples. you will learn eventually. • In general, there is no short cut. ecs30 Winter 2012 Lecture #01

  50. Compile your first program • emacs • gcc • make • a.out ecs30 Winter 2012 Lecture #01

More Related