1 1 6 Midterm Review 2007 Pearson Education

  • Slides: 29
Download presentation
1 1 -6 Midterm Review 2007 Pearson Education, Inc. All rights reserved.

1 1 -6 Midterm Review 2007 Pearson Education, Inc. All rights reserved.

2 1. 2. Review chapter 1 -6 – Variables, arithmetic, decision making – Structured

2 1. 2. Review chapter 1 -6 – Variables, arithmetic, decision making – Structured program development in C – C program controls – C Functions – C Arrays Midterm exam rules, scope, and tips 2007 Pearson Education, Inc. All rights reserved.

1: Variables, Arithmetic, Decision making § Memory Concepts – Understand variables: type, size, value

1: Variables, Arithmetic, Decision making § Memory Concepts – Understand variables: type, size, value § Arithmetic in C – Remember the precedence of arithmetic operators § Decision Making: Equality and Relational Operators – Remember the meaning and syntax of equality and relational operators 2007 Pearson Education, Inc. All rights reserved.

2: Structured Program Development in C § Basic problem-solving techniques – 2 important primary

2: Structured Program Development in C § Basic problem-solving techniques – 2 important primary steps - Thorough understanding of the problem - Carefully plan an approach for solving it – Algorithms, Pseudo code, control structures – Formulating Algorithm with Top-down, stepwise refinement case § 3 phases programming – Initialization, processing, and termination § Control statements – If…else, while, nested control, counter and sentinel controlled repetition § Operators: assignment, increment, decrement 2007 Pearson Education, Inc. All rights reserved.

3: Program control § Essential of counter-controlled repetition § Control statements: – Pretest loop:

3: Program control § Essential of counter-controlled repetition § Control statements: – Pretest loop: for – Posttest loop: do … while – Multiple selection: switch § Alter flow control: – break – continue § Complex conditional expressions 2007 Pearson Education, Inc. All rights reserved.

3. 1: Pretest Loop for § Syntax: for ( init ; condition ; update

3. 1: Pretest Loop for § Syntax: for ( init ; condition ; update ) statement; § Init: assignments to counter variables § Update: changes to counter variables 2007 Pearson Education, Inc. All rights reserved.

3. 1: For Example § Printing vertical line of stars: for (counter = 0;

3. 1: For Example § Printing vertical line of stars: for (counter = 0; counter < 5; counter++) { printf(“*n”); } 2007 Pearson Education, Inc. All rights reserved.

3. 2: Posttest Loop do-while § Syntax: do { statement(s) } while (condition); 2007

3. 2: Posttest Loop do-while § Syntax: do { statement(s) } while (condition); 2007 Pearson Education, Inc. All rights reserved.

3. 2: Using the do-while § do { printf(“n Enter id# and salary: “);

3. 2: Using the do-while § do { printf(“n Enter id# and salary: “); scanf(“%d %f”, &id, &salary); printf(“n You entered id#%1 d”, id); printf(“ and salary $%. 2 f, ”, salary); printf(“ Is this correct? (Y/N) ” ); scanf(“ %c”, &ans); } while ((ans != ‘Y’) && (ans != ‘y’)); § Loop always executes at least once 2007 Pearson Education, Inc. All rights reserved.

3. 3: Multiple selection - Switch § switch – Useful when a variable or

3. 3: Multiple selection - Switch § switch – Useful when a variable or expression is tested for all the values it can assume and different actions are taken § Format – Series of case labels and an optional default case - switch ( value ){ – case '1': § actions – case '2': § actions – default: § actions – } – break; exits from statement 2007 Pearson Education, Inc. All rights reserved.

3. 4: Break statement § The break statement can be used to halt a

3. 4: Break statement § The break statement can be used to halt a loop. § do { scanf(“%d”, &num); if (num < 0) break; /* Loop ends */ } while (num != 0); 2007 Pearson Education, Inc. All rights reserved.

3. 5: Continue statement § A continue statement says jump past any remaining statements

3. 5: Continue statement § A continue statement says jump past any remaining statements § Example: for (I = 0; I < 100; I++) { if ((I % 2) == 1) continue; printf(“%d is even”, I); // continue goes to here: end of // statements } 2007 Pearson Education, Inc. All rights reserved.

3. 6: Logical operators 2007 Pearson Education, Inc. All rights reserved.

3. 6: Logical operators 2007 Pearson Education, Inc. All rights reserved.

4: C Functions § Program modules in C § Function definitions and prototypes §

4: C Functions § Program modules in C § Function definitions and prototypes § Function call stack and activation records § Headers § Calling function by value and reference § Functions – Math library functions – rand() – Recursion 2007 Pearson Education, Inc. All rights reserved.

15 4. 1: Program modules in C § Fig. 5. 1 | Hierarchical boss

15 4. 1: Program modules in C § Fig. 5. 1 | Hierarchical boss function/worker function relationship. 2007 Pearson Education, Inc. All rights reserved.

16 4. 2: Function definition & Prototypes § Function definition format return-value-type function-name( parameterlist

16 4. 2: Function definition & Prototypes § Function definition format return-value-type function-name( parameterlist ) { declarations and statements } § Function Prototypes – Function name, Parameters, Return type (default int) – Used to validate functions – Prototype only needed if function definition comes after use in program int maximum( int x, int y, int z ); - Takes in 3 ints - Returns an int 2007 Pearson Education, Inc. All rights reserved.

4. 3: Function call stack and activation records 17 § Program execution stack: §

4. 3: Function call stack and activation records 17 § Program execution stack: § Last-in, first-out data structure § Keep track of which functions have been called § Activation records: § Multiple function can be activated at the same time § Each function has an activation record (Stack frame) § Stack overflow: # activation records > memory capacity 2007 Pearson Education, Inc. All rights reserved.

18 4. 4: Function headers § Header files – Contain function prototypes for library

18 4. 4: Function headers § Header files – Contain function prototypes for library functions – <stdlib. h> , <math. h> , etc – Load with #include <filename> #include <math. h> § Custom header files – Create file with functions – Save as filename. h – Load in other files with #include "filename. h" – Reuse functions 2007 Pearson Education, Inc. All rights reserved.

19 4. 5: Calling Functions § Call by value – Copy of argument passed

19 4. 5: Calling Functions § Call by value – Copy of argument passed to function – Changes in function do not effect original – Use when function does not need to modify argument - Avoids accidental changes § Call by reference – Passes original argument – Changes in function effect original – Only used with trusted functions 2007 Pearson Education, Inc. All rights reserved.

20 4. 6 Storage class § Identifier attributes: – Storage duration – how long

20 4. 6 Storage class § Identifier attributes: – Storage duration – how long an object exists in memory – Scope – where object can be referenced in program – Linkage – specifies the files in which an identifier is known § Storage class specifiers: – Automatic storage duration: Auto, Register – Static storage duration: Extern, Static 2007 Pearson Education, Inc. All rights reserved.

21 4. 7: Scope rules § Identifier scopes: – Function scope: - Can only

21 4. 7: Scope rules § Identifier scopes: – Function scope: - Can only be referenced inside a function body - Used only for labels (start: , case: , etc. ) – File scope: - Identifier defined outside function, known in all functions - Used for global variables, function definitions, function prototypes – Block scope: - Identifier declared inside a block - Local variables of function 2007 Pearson Education, Inc. All rights reserved.

22 4. 8: Recursion (1) § Recursive functions – Functions that call themselves –

22 4. 8: Recursion (1) § Recursive functions – Functions that call themselves – Can only solve a base case § Recursion vs. Iteration – Repetition - Iteration: explicit loop - Recursion: repeated function calls – Termination - Iteration: loop condition fails - Recursion: base case recognized – Both can have infinite loops 2007 Pearson Education, Inc. All rights reserved.

23 4. 8: Recursion (2) § Example: factorials – 5! = 5 * 4

23 4. 8: Recursion (2) § Example: factorials – 5! = 5 * 4 * 3 * 2 * 1 – Notice that - 5! = 5 * 4! - 4! = 4 * 3!. . . 2007 Pearson Education, Inc. All rights reserved.

24 5: C Arrays § Array data structure § Passing arrays to functions §

24 5: C Arrays § Array data structure § Passing arrays to functions § Sorting, searching arrays § Multiple-Subscripted arrays 2007 Pearson Education, Inc. All rights reserved.

25 5. 1: Array data structure § Structure: – Group of consecutive memory locations

25 5. 1: Array data structure § Structure: – Group of consecutive memory locations – Same name and type § To refer to an element, specify – Array name – Position number – Format: arrayname[ position number ] § Defining arrays array. Type array. Name[number. Of. Elements ]; 2007 Pearson Education, Inc. All rights reserved.

26 5. 2: Passing arrays to functions § Passing arrays – Arrays passed call-by-reference

26 5. 2: Passing arrays to functions § Passing arrays – Arrays passed call-by-reference – Name of array is address of first element – Syntax: - Function_name ( array_name, array_size ); § Passing array elements – Passed by call-by-value – Pass subscripted name (i. e. , my. Array[ 3 ]) to function § Function prototype void modify. Array( int b[], int array. Size ); 2007 Pearson Education, Inc. All rights reserved.

27 5. 3 Sorting & Searching Arrays § Sorting arrays – Bubble sort –

27 5. 3 Sorting & Searching Arrays § Sorting arrays – Bubble sort – Computing: Mean, Median, Mode § Searching arrays – Linear – Binary – Recursive & Interative 2007 Pearson Education, Inc. All rights reserved.

28 5. 4: Multiple-Subscripted arrays § Multiple subscripted arrays – Tables with rows and

28 5. 4: Multiple-Subscripted arrays § Multiple subscripted arrays – Tables with rows and columns (m by n array) – Like matrices: specify row, then column § Initialization – int b[ 2 ] = { { 1, 2 }, { 3, 4 } }; § Referencing elements – Specify row, then column 2007 Pearson Education, Inc. All rights reserved.

29 Midterm exam rules, scope, & tips § Rules: – You are not allowed

29 Midterm exam rules, scope, & tips § Rules: – You are not allowed to refer to any materials except one cheating paper. – The examination starts at 9: 00 AM and ends at 10: 30 AM sharp. – You can leave any time if you want before 10: 30 PM. § Scope: Chapter 1 -6 § Tips: – – Review all lecture slides Review all class assignments and home works Do all Self-Review exercises after each chapter in text book Practice as many as possible 2007 Pearson Education, Inc. All rights reserved.