1 / 31

ECE-1021 Course Review

ECE-1021 Course Review. Engineering Problem Solving. Define the problem clearly Work hand examples Develop the Algorithm Implement the Algorithm Test the Implementation. Top-Down Algorithm Development. Divide and Conquer Break Problem into smaller tasks.

hank
Download Presentation

ECE-1021 Course 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. ECE-1021 Course Review

  2. Engineering Problem Solving • Define the problem clearly • Work hand examples • Develop the Algorithm • Implement the Algorithm • Test the Implementation

  3. Top-Down Algorithm Development • Divide and Conquer • Break Problem into smaller tasks. • Define the interaction between tasks. • Recursively solve the individual tasks. • Build up the overall solution from the pieces.

  4. Structured Programming Elements • Sequences • Selections • Repetitions

  5. F Entry Task I/O Q? Exit T Basic Flowchart Symbols

  6. Logical Values • FALSE: A value that is EXACTLY zero. • TRUE: A value that is not FALSE • TRAP: Use of floating point values.

  7. Logical Expressions • Evaluate to: • 0 if FALSE • 1 if TRUE (but recognize any nonzero value as TRUE) • Operators: • ! - Logical NOT • Example: k = !j; • && - Logical AND • Example k = i && j; • || - Logical OR • Example k = i || j;

  8. C Programming Elements • Sequences • Statements execute in order presented. • Control Structures override normal order. • Selections • One (or more) branches chosen based on a test. • if(), if()/else, switch() • Repetitions • Selection Structure that branches to a point above the branch point. • while(), do/while(), for()

  9. C is Function Oriented function header { function body } function header: return-type function-name(param list) • return-type: type of value returned by function. • param list: variable declarations that are initialized with the function call’s corresponding argument values.

  10. Assignment Expressions Normal Assignment Operator: a = b + c; Abbreviated Assignment Operators: a += b + c; /* same as a = a + b + c; */ Evaluate to value assigned.

  11. Increment/Decrement Operator • Changes the value stored in a variable by one. • Post-Increment/Post-Decrement • c++; c--; • Evaluates to original value in variable. • Pre-Increment/Pre-Decrement • ++c; --c; • Evaluates to (original value +/- 1)

  12. if() if()...else test? test? F F T T if_code if_code else_code if() and if()/else statements • Syntax if(test) { if_code; } else { else_code; }

  13. switch() statement (int_expr) must evaluate to an integer value at runtime. (int_constN) must evaluate to a uniqueintegerconstant at compile time. execution jumps to case where (int_constN == int_expr) is TRUE. break; terminates switch() execution. default case Optional. Executes only if NO other case executes. • Syntax switch(int_expr) { case int_const1: code1; break; case int_const2: code2; case int_const3: code3; break; default: code4; } • Compact way of writing certain types of complex but common if()/else blocks.

  14. Loop Structures Special case of Selection Statement • One branch eventually leads back to the original selection statement. • Permits a block of code to be executed repeatedly as long as some test condition is satisfied. C provides three different looping structures • while(), do/while(), for() • Only one is needed and any one is sufficient. • Different structures are better matches for different logic. • Using the “proper” one aides the programmer and anyone else reading the code. • The compiler doesn’t care and will often implement the code identically regardless of which structure is used.

  15. ini_code test? F T loop_code inc_code next_code while() loop • Syntax ini_code // not part of loop while(test_expr) { loop_code; increment_code; } next_code; // not part of loop • Features • loop does not execute at all if test fails the first time.

  16. ini_code loop_code inc_code T test? F next_code do/while() loop • Syntax ini_code // not part of loop do { loop_code; increment_code; } while(test_expr); next_code; // not part of loop • Features • loop will always execute at least once, even if test fails the first time.

  17. ini_code test? F T loop_code inc_code next_code for() loop • Syntax for(ini_code; test_expr; inc_code) { loop_code; } next_code; // not part of loop • Features • Just a while() loop with the initialization and increment code formally incorporated into the syntax. • Can make a cleaner divide between the loop logic and the housekeeping logic. • Makes it harder to omit initialization code if loop is moved or copied.

  18. Macros • Perform text replacement prior to compile. • Object-like macros • Symbolic Constants • Function-like macros: • Two step replacement process • Golden Rules • Surround ALL macro arguments with parentheses. • Surround entire macro body with parentheses. • Ensures predictable evaluation.

  19. ASCII code • Characters and control actions encoded as an integer value. • Standard ASCII - 7 bits • Eleven subgroups (in addition to complete group): • ASCII, CONTROL, PRINTING, GRAPHICAL • ALPHANUMERIC, PUNCTUATION • ALPHABETICAL, NUMERIC • UPPERCASE, LOWERCASE • HEXADECIMAL, WHITESPACE

  20. Bitwise Operations • Operators work on entire value. • Result is determined bit-by-bit • Operators: • ~ - bitwise NOT • & - bitwise AND • | - bitwise OR • ^ - bitwise XOR

  21. Integer Representation • Integer • Unsigned Integers • Pure Binary • Signed Integers • Signed Binary • Offset Binary • One’s Compliment • Two’s Compliment (most commonly used) • C Standard does not allow use of offset binary • Positive integers must use same representation as unsigned integers.

  22. Floating Point Representation • IEEE-754 Floating Point Standard • Value broken into three parts from left-to-right • Sign Bit (0 for positive, 1 for negative) • Exponent (used offset binary with 0111....1 mapping to zero) • Mantissa Magnitude • Normalized with implied leading 1. • Denormalized if exponent pattern is all 0. • Special Values • Exponent Pattern is all 1’s • +/- infinity if mantissa is all 0’s • NaN (Not-a-Number) if mantissa has any 1’s

  23. Recursion • To be a successful, recursive function: • Must have a recursive path • Must have a non-recursive path (“base case”) • Often quicker to develop and debug. • Generally slower. • Generally consumes more memory resources. • Iterative alternative always exists.

  24. File Operations • File interaction performed via FILE structure. • fopen(), fclose() • Primary Modes: “rt”, “wt”, “rb”, “wb” • Should ALWAYS verify fopen() success • If FILE * returned by fopen is NULL: • Do NOTHING more with that file - NOTHING! • Close files as soon as they are no longer needed.

  25. File I/O • Two types of files - Text and Binary • Text: • Contents are expected to consist of one-byte ASCII codes. • Character I/O: putc(), getc(), puts(), gets() • Formatted I/O: fprintf(), fscanf() • Binary: • Direct copy between memory and file. • fread()/fwrite() • fseek(), fset(), ftell() • SEEK_SET, SEEK_CUR, SEEK_END

  26. Pointers • A variable used to store memory addresses. • The address were a data item is stored. • Dereference to access the data. • *ptr = variable + *ptr2; • The address operator evaluates to the address where an object is stored: • ptr = &variable; • Pointer arithmetic: • pointer +/- integer • integer is number of elements (not bytes)

  27. Arrays • One or more variables stored: • In one contiguous block of memory. • Array name is a pointer to start of block. • All variables are the same type. • Can access elements by way of pointer offset • *(array+index) = value; • array[index] = value; • Passed by reference in function calls.

  28. Structures • Programmer-defined data type. • One or more variables stored: • In one contiguous block of memory. • May be different data types. • Passed by value in function calls.

  29. Structure Declarations Typedef can appear before structure declaration typedef struct pt3d PT3D; struct pt3d { int pt; double x, y, z; }; int main(void) { int i; PT3D pt1; PT3D pt[5]; .... return 0; } Structure declaration outside any function to make it have global scope

  30. Accessing Elements • By variable name: point.pt = 42; j = point.pt + 3; • By pointer to a structure variable: (*ptr).pt = 42; ptr->pt = 42;

  31. Structure Utilization • Quasi-Object Oriented Programming • Structure contains data. • Primitive functions are ONLY functions to directly access the structure’s data. • Get()/Set() function pairs. • Utility Functions invoke primitive functions if they need to access the structure’s data.

More Related