210 likes | 227 Views
Prepare for Exam 2 testing knowledge in C programming, including arithmetic & relational ops, loops, functions, arrays, pointers, and searching/sorting algorithms. Study key concepts for success.
E N D
UMBCCMSC 104 – Section 01, Fall 2016 Exam 2 Review
Notes & Announcements • Project 8 due… now! • Will review in a moment • Today is final day to fill out course evals • Look for email from StudentCourseEvaluations@umbc.edu
Final Exam • The final exam is scheduled for: Thursday, December 15th 6:00 – 8:00 PM Engineering 122 (here) • The exam will be on paper – bring a pencil! • You may bring a calculator. You probably won’t need it. • NO PHONES OR COMPUTER USAGE ALLOWED • Bring your UMBC ID!
Requesting Feedback Did you prefer the course website over Blackboard? Why? Would you have preferred less projects and another exam? Why? Would you have preferred more in-class labs, assuming they didn’t count for EC? Other comments?
Overview • If it’s in the slides, it’s fair game • Broad Concepts • Arithmetic Ops (+, -, *, /, %) • Assignment Ops (++, --, +=, -=, etc) • Relational Ops & Selection Statements (if, else if, else) • For loops and While loops • Switch Statements • Functions • Arrays • Searching & Sorting Algorithms • Pointers
Increment and Decrement Operators • The increment operator ++ • The decrement operator -- • Precedence: lower than (), but higher than * / and % • Associativity: right to left • Increment and decrement operators can only be applied to variables, not to constants or expressions
Assignment Operators = += -= *= /= %= StatementEquivalent Statement a = a + 2 ; a += 2 ; a = a - 3 ; a -= 3 ; a = a * 2 ; a *= 2 ; a = a / 4 ; a /= 4 ; a = a % 2 ; a %= 2 ; b = b + ( c + 2 ) ; b += c + 2 ; d = d * ( e - 5 ) ; d *= e - 5 ;
Relational Operators < less than > greater than <= less than or equal to >= greater than or equal to == is equal to != is not equal to Relational expressions evaluate to the integer values 1 (true) or 0 (false). All of these operators are called binary operators because they take two expressions as operands.
Logical Operators • So far we have seen only simple conditions. if ( count > 10 ) . . . • Sometimes we need to test multiple conditions in order to make a decision. • Logical operators are used for combining simple conditions to make complex conditions. && is AND if ( x > 5 && y < 6 ) || is OR if ( z == 0 || x > 10 ) ! is NOT if (! (bob > 42) )
Nesting of if-else Statements if ( condition1 ) { statement(s) } else if ( condition2 ) { statement(s) } . . . /* more else clauses may be here */ else { statement(s) /* the default case */ }
The while Repetition Structure while ( condition ) { statement(s) } The braces are not required if the loop body contains only a single statement. However, they are a good idea and are required by the 104 C Coding Standards.
The for Loop Repetition Structure • The for loop handles details of the counter-controlled loop “automatically”. • The initialization of the loop control variable, the termination condition test, and control variable modification are handled in the for loop structure. for ( i = 1; i < 101; i = i + 1) { initialization modification } test
The switch Multiple-Selection Structure switch ( integer expression ) { case constant1 : statement(s) break ; case constant2 : statement(s) break ; . . . default: statement(s) break ; }
Examining PrintMessage #include <stdio.h> void PrintMessage ( void ) ; function prototype int main ( ) { PrintMessage ( ) ; function call return 0 ; } void PrintMessage ( void ) function header { printf (“A message for you:\n\n”) ; function printf (“Have a nice day!\n”) ; body } function definition
Array Declaration and Initialization int numbers[5] ; • The name of this array is “numbers”. • This declaration sets aside a chunk of memory that is big enough to hold 5 integers. • It does not necessarilyinitialize those memory locations to 0 or any other value. They may contain garbage. • Initializing an array may be done with an array initializer, as in : int numbers[5] = { 5, 2, 6, 9, 3 } ; numbers 5 2 6 9 3
Accessing Array Elements • Each element in an array has a subscript (index) associated with it. • Subscripts are integers and always begin at zero. • Values of individual elements can be accessed by indexing into the array. For example, printf(“The third element = %d.\n”, numbers[2]); would give the output The third element = 6. numbers 5 2 6 9 3 0 1 2 3 4
Don’t Forget… • Searching & Sorting • Searching Algorithms • Sorting Algorithms • Efficiency • Pointers • Definition of a pointer • Types of pointers • Uses for pointers