Presentation is loading. Please wait.

Presentation is loading. Please wait.

ECE-1021 Course Review. Engineering Problem Solving ä Define the problem clearly ä Work hand examples ä Develop the Algorithm ä Implement the Algorithm.

Similar presentations


Presentation on theme: "ECE-1021 Course Review. Engineering Problem Solving ä Define the problem clearly ä Work hand examples ä Develop the Algorithm ä Implement the Algorithm."— 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 Basic Flowchart Symbols Entry Exit TaskI/OQ? T F

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() and if()/else statements ä Syntax if(test){ if_code; if_code;}else{ else_code; else_code;} test? if_codeif()T F test? if_codeelse_codeif()...elseF T

13 switch() statement ä Syntax switch(int_expr){ case int_const1: code1; case int_const1: code1; break; break; case int_const2: code2; case int_const2: code2; case int_const3: code3; case int_const3: code3; break; break; default: code4; default: code4;} ä Compact way of writing certain types of complex but common if()/else blocks. (int_expr) must evaluate to an integer value at runtime. (int_constN) must evaluate to a unique integer constant 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.

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 while() loop ä Syntax ini_code // not part of loop while(test_expr){ loop_code; loop_code; increment_code; increment_code;} next_code; // not part of loop ä ä Features ä ä loop does not execute at all if test fails the first time. loop_code test? F T inc_code ini_code next_code

16 do/while() loop ä Syntax ini_code // not part of loop do{ loop_code; loop_code; increment_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. loop_code test? F T inc_code ini_code next_code

17 for() loop ä Syntax for(ini_code; test_expr; inc_code) { loop_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. loop_code test? F T inc_code ini_code next_code

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 struct pt3d PT3D; struct pt3d { int pt; double x, y, z; }; int main(void) { int i; PT3D pt1; PT3D pt[5];.... return 0; } Typedef can appear before structure declaration 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.


Download ppt "ECE-1021 Course Review. Engineering Problem Solving ä Define the problem clearly ä Work hand examples ä Develop the Algorithm ä Implement the Algorithm."

Similar presentations


Ads by Google