1 / 30

ENEE150 Midterm Review

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:

tobias
Download Presentation

ENEE150 Midterm Review

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. ENEE150 Midterm Review

  2. Open Notes: • Bring your notes! • Online references • HW#1, Project #1

  3. Scope: • Declarative vs. Imperative Knowledge: • Declarative: “What is” knowledge • Imperative: “How to” knowledge • Programming is imperative

  4. Program Complexity • Measuring Complexity: • Source Lines of code (SLOC) • Complexity management: • Divide and Conquer • Abstraction • Well documented Code • Coding Convention

  5. C Language Features: • Low level language • Few features • Allows Programmers to directly access memory

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

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

  8. Functional Decomposition: • Break up logical segments of code into modules • Improves code understanding • Easier to debug • Code management and resuage

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

  10. Multiple Modules: • Compiling and working with multiple files gccone.ctwo.cthree.c • Header files include: • Function prototypes • Global variables • Constants

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

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

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

  14. Shell Scripts • A shell is executing on your terminal • Common shells include: • Bash • Tcsh • Csh • Sh

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

  16. Shell Syntax • First line tells interpreter which shell you are in: #! /bin/tcsh FIRST_ARGUMENT = “Hello” echo “$FIRST_ARGUMENT World!”

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

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

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

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

  21. Debugging: • Printfvs Using GDB • Review Other Discussion slides 

  22. Introduction to Pointers: Pointers: • Powerful, but difficult to master • Simulate call-by-reference • Close relationship with arrays and strings

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

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

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

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

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

  28. 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!)

  29. Pointer Exercise!

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

More Related