320 likes | 435 Views
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
E N D
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
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
Prerequisites • Math 16A or 21A (may be taken concurrently); prior experience with basic programming concepts (e.g., loop) recommended. ecs30 Winter 2012 Lecture #01
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
Problem Solving ecs30 Winter 2012 Lecture #01
Course Requirements • 35%: Programming Assignments • 40%: Two close-book/note midterms • 25%: One close-book/note final ecs30 Winter 2012 Lecture #01
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
ecs30 is really about how to communicate with… ecs30 Winter 2012 Lecture #01
#include <stdio.h> int main () { intx; /* declare x */ x = 1; printf("Hello world %d\n", x); return 0; }// main() ecs30 Winter 2012 Lecture #01
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
{ … } • { … } • {… { … {…}…} … {…}…} • int main (void) { … } • float myfunc (int x) { …; return f;} • { <declarations> <statements> } ecs30b Fall 2008 Lecture #02
Memory Cell Concept Again ecs30b Fall 2008 Lecture #02
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
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
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
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
“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
“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
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
Compile your first program • emacs • gcc • make • a.out ecs30 Winter 2012 Lecture #01
How to "solve a problem" in C? 1. Problem Specification metric measurement conversion. 2. Analysis Input = the distance in miles Output = the distance in kilometers 1 miles = 1.609 kilometers. 3. Design Algorithm: (A correct procedure to solve a problem) step A. Get the distance in miles. step B. Convert the distance to kilometers. step C. Display the distance in Kilometers. 4. Implementation ecs30 Winter 2012 Lecture #01
How to "solve a problem" in C? 1. Problem Specification metric measurement conversion. 2. Analysis Input = the distance in miles Output = the distance in kilometers 1 miles = 1.609 kilometers. 3. Design Algorithm: (A correct procedure to solve a problem) step A. Get the distance in miles. step B. Convert the distance to kilometers. step C. Display the distance in Kilometers. 4. Implementation ecs30b Fall 2008 Lecture #02
Compiler/Linker • C (.c) ~ binary (.o) • High-level to “bits of 0 and 1” • Platform dependence • (.o) ~ executable • executable ~ loaded into OS for execution ecs30b Fall 2008 Lecture #02
Make # Makefile for HW1, ecs30b, f2008. # macros. CC = gcc CFLAGS = -g -Wall -Wstrict-prototypes # rules. ecs30b_hw1: ecs30b_hw1.o $(CC) ecs30b_hw1.o -o ecs30b_hw1 ecs30b_hw1.o: ecs30b_hw1.c $(CC) -c $(CFLAGS) ecs30b_hw1.c clean: rm -f *.o *~ core ecs30b_hw1 # end of Makefile ecs30b Fall 2008 Lecture #02
C program • The preprocessor part • Definitions: Macro and Variable • Define the memory/data • Variable name and its data type • Control Flow • Where should we start to execute? “main” • Reserved words • Function and blocks/scope of code ecs30b Fall 2008 Lecture #02
Compiler • Source code (e.g., e.c) • Object code (e.g., e.o) • Executable (e.g., a.out, IDontCare) ecs30 Winter 2012 Lecture #01