Download presentation
Presentation is loading. Please wait.
1
1-6 Midterm Review
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
3
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
4
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
5
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
6
3.1: Pretest Loop for Syntax: Init: assignments to counter variables
for ( init ; condition ; update ) statement; Init: assignments to counter variables Update: changes to counter variables
7
3.1: For Example Printing vertical line of stars: for (counter = 0;
{ printf(“*\n”);}
8
3.2: Posttest Loop do-while
Syntax: do { statement(s) } while (condition);
9
3.2: Using the do-while do { printf(“\n Enter id# and salary: “);
scanf(“%d %f”,&id,&salary); printf(“\n You entered id#%1d”, id); printf(“ and salary $%.2f,”, salary); printf(“ Is this correct? (Y/N) ” ); scanf(“ %c”,&ans); } while ((ans != ‘Y’) && (ans != ‘y’)); Loop always executes at least once
10
3.3: Multiple selection - 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': default: } break; exits from statement
11
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);
12
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 }
13
3.6: Logical operators
14
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
15
Fig. 5.1 | Hierarchical boss function/worker function relationship.
4.1: Program modules in C Fig. 5.1 | Hierarchical boss function/worker function relationship.
16
4.2: Function definition & Prototypes
Function definition format return-value-type function-name( parameter-list ) { 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
17
4.3: Function call stack and activation records
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
18
4.4: Function headers Header files Custom 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
19
4.5: Calling Functions Call by value Call by reference
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
20
4.6 Storage class Identifier attributes: Storage class specifiers:
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
21
4.7: Scope rules Identifier scopes: Function scope: File 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
22
4.8: Recursion (1) Recursive functions Recursion vs. Iteration
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
23
4.8: Recursion (2) Example: factorials Notice that
5! = 5 * 4 * 3 * 2 * 1 Notice that 5! = 5 * 4! 4! = 4 * 3! ...
24
5: C Arrays Array data structure Passing arrays to functions
Sorting, searching arrays Multiple-Subscripted arrays
25
5.1: Array data structure Structure: To refer to an element, specify
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 arrayType arrayName[numberOfElements ];
26
5.2: Passing arrays to functions
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., myArray[ 3 ]) to function Function prototype void modifyArray( int b[], int arraySize );
27
5.3 Sorting & Searching Arrays
Sorting arrays Bubble sort Computing: Mean, Median, Mode Searching arrays Linear Binary Recursive & Interative
28
5.4: Multiple-Subscripted arrays
Tables with rows and columns (m by n array) Like matrices: specify row, then column Initialization int b[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } }; Referencing elements Specify row, then column
29
Midterm exam rules, scope, & tips
You are not allowed to refer to any materials except one cheating paper. The examination starts at 9:00 AM and ends at 10:30AM sharp. You can leave any time if you want before 10:30PM. 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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.