310 likes | 468 Views
ENEE150 Midterm Review. Open Notes:. Bring your notes! Online references HW#1, Project #1. Scope:. Declarative vs. Imperative Knowledge: Declarative : “What is” knowledge Imperative: “How to” knowledge Programming is imperative. Program Complexity. Measuring Complexity:
E N D
Open Notes: • Bring your notes! • Online references • HW#1, Project #1
Scope: • Declarative vs. Imperative Knowledge: • Declarative: “What is” knowledge • Imperative: “How to” knowledge • Programming is imperative
Program Complexity • Measuring Complexity: • Source Lines of code (SLOC) • Complexity management: • Divide and Conquer • Abstraction • Well documented Code • Coding Convention
C Language Features: • Low level language • Few features • Allows Programmers to directly access memory
Edit, Compile and Execute Source code produces Editor welcome.c eg: vim welcome.c Executable code produces Cannot compile? Compiler a.out eg: gcc welcome.c Output Incorrect result? Execute produces Hello, welcome to CS1010! eg: a.out Test, test, and test! Week2 - 6 CS1010 (AY2013/4 Semester 1)
Program Structure: Input/Output: • %d and%c are examples of format specifiers; they are placeholders for values to be displayed or read • Examples of format specifiers used in printf(): • %5d: to display an integer in a width of 5, right justified • Note: For scanf(), just use the format specifier without indicating width, decimal places, etc. Week2 - 7 CS1010 (AY2013/4 Semester 1)
Functional Decomposition: • Break up logical segments of code into modules • Improves code understanding • Easier to debug • Code management and resuage
Functional Decomposition: • Functional Decomposition is an art, not a science • Hierarchical Decomposition • Top Down design • Code refactoring – breaking up of code into logical blocks (modules, methods)
Multiple Modules: • Compiling and working with multiple files gccone.ctwo.cthree.c • Header files include: • Function prototypes • Global variables • Constants
Redirect Input/Output • 1. Use “<” to redirect standard input from a file Ex: sheet < sheet-test1.in • 2. Use “>” to redirect standard output to a file Ex: sheet < sheet-test1.in > output
Redirect Input/Output • Using “!” to forcibly write to a file Ex: a.out < sheet-test1.in > ! sheet-test.out • This will redirect the output of a.out to sheet-test.out, if sheet-test.out already exists, it will overwrite the contents • Using diff to compare files
Unit Testing / Module Test Vectors • Allows you to: • Test one feature / function at a time • Testing during incremental project build • Supports debugging less at a time
Shell Scripts • A shell is executing on your terminal • Common shells include: • Bash • Tcsh • Csh • Sh
Shell Syntax • Each shell has a different syntax, but generally all share common attributes • Shells act as linux command interpreters: • Any combination of valid linux commands can be written into a shell script
Shell Syntax • First line tells interpreter which shell you are in: #! /bin/tcsh FIRST_ARGUMENT = “Hello” echo “$FIRST_ARGUMENT World!”
Shell Scripts • Remember to put enter at the last line of your shell script orit won’t execute your last line! • The commands are being executed on the command line, line by line, and so pressing “enter” tells the command line to execute that line.
Shell Scripts • Variables needed to be declared, note it is case-sensitive (e.g. foo, FOO, Foo) • Add ‘$’ for storing values % salutation=Hello % echo $salutation Hello % salutation=7+5 % echo $salutation 7+5 % salutation=“yes dear” % echo $salutation yes dear % read salutation Hola! % echo $salutation Hola!
File Permissions • files have 3 different types of permissions: a user, a group, and everyone else • each with its own set of permissions • permissions are to read/write/execute in order user/group/other, cf. -rw-rw-r-- 1 maestro user 35414 Mar 25 01:38 baton.txt • set using chmodcommand (chmod 777)
3. Errors • Compile Time Errors (Syntax Errors) • Program does not obey C construct /grammar such as invalid choice of identifier name, invalid expression, missing semi-colon, etc. • Warning happens, for example, incomparable use of types for output • Run-time Errors • Program terminates unexpectedly due to illegal operation, such as dividing a number by zero • Logic Errors • Program produces result as opposed to what is expected (wrong algorithm) • Undetected Errors • Exist if we are not able to test all cases The process of correcting errors in programs is called debugging. This process can be verytime-consuming!
Debugging: • Printfvs Using GDB • Review Other Discussion slides
Introduction to Pointers: Pointers: • Powerful, but difficult to master • Simulate call-by-reference • Close relationship with arrays and strings
Pointer Declaration and Initialization: • Pointer variables • Contain memory addresses as their values • Normal variables contain a specific value (direct reference) • Pointers contain address of a variable that has a specific value (indirect reference) • Indirection - referencing a pointer value
Pointer Declaration and Initialization: • Pointer declarations • * used with pointer variables int *myPtr; • Declares a pointer to an int(pointer of type int *) • Multiple pointers, multiple * int *myPtr1, *myPtr2; • Can declare pointers to any data type • Initialize pointers to 0, NULL, or an address • 0 or NULL - points to nothing (NULL preferred)
yptr y y 5 yPtr Address of y is value of yptr 500000 600000 600000 5 Pointer Operations: • & (address operator) • Returns address of operand int y = 5; int *yPtr; yPtr = &y; //yPtr gets address of y • yPtr“points to” y
Pointer Operators: • * (indirection/dereferencing operator) • Returns a synonym/alias of what its operand points to *yptr returns y (because yptr points to y) • * can be used for assignment • Returns alias to an object *yptr = 7; // changes y to 7
Pointer Operators: • * and& are inverses • They cancel each other out *&yptr -> * (&yptr) -> * (address of yptr)-> returns alias of what operand points to -> yptr &*yptr -> &(*yptr) -> &(y) -> returns address of y, which isyptr -> yptr
Printing out Pointer Values • Printing out the address of which pointer refers to => “%p” • Printing out the value of a pointer • “%d”, “%c”, “%l” (Depending on the type of pointer!)
Assume: Address of c is 6940 Address of d is 9772 Address of e is 2224 What is the following output? char c = ‘T’, d = ‘S’; char *p1 = &c; char *p2 = &d; char *p3; p3 = &d; printf(“%d”, *p3); //1 p3 = p1; printf(“%d”, *p3); //2 printf(“%p”, p3); //3 *p1 = *p2; printf(“%d”, *p1) //4