310 likes | 448 Views
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.
E N D
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. • Define the interaction between tasks. • Recursively solve the individual tasks. • Build up the overall solution from the pieces.
Structured Programming Elements • Sequences • Selections • Repetitions
F Entry Task I/O Q? Exit T Basic Flowchart Symbols
Logical Values • FALSE: A value that is EXACTLY zero. • TRUE: A value that is not FALSE • TRAP: Use of floating point values.
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;
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()
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.
Assignment Expressions Normal Assignment Operator: a = b + c; Abbreviated Assignment Operators: a += b + c; /* same as a = a + b + c; */ Evaluate to value assigned.
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)
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; }
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.
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.
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.
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.
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.
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.
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
Bitwise Operations • Operators work on entire value. • Result is determined bit-by-bit • Operators: • ~ - bitwise NOT • & - bitwise AND • | - bitwise OR • ^ - bitwise XOR
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.
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
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.
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.
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
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)
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.
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.
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
Accessing Elements • By variable name: point.pt = 42; j = point.pt + 3; • By pointer to a structure variable: (*ptr).pt = 42; ptr->pt = 42;
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.