Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP1170 Midterm Preparation (March 17 th 2009) Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education.

Similar presentations


Presentation on theme: "COMP1170 Midterm Preparation (March 17 th 2009) Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education."— Presentation transcript:

1 COMP1170 Midterm Preparation (March 17 th 2009) Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.

2 Reminder The Midterm Test will be held on 17th March, 2009 (Tuesday) Exact Time: 8:30 – 10:20AM (110 minutes) Venue: LT1 Scope: Chapters 1-7 Notes: 1) The midterm is close book & close notes exam 2) The midterm is a written test with test paper Be well prepared for it! (10% of overall score) Copy cats are highly penalized!

3 Questions Total marks are 10. Basic concepts:  15 multiple choice questions (0.3*15 = 4.5 marks)  5 “Fill in the blank” (0.3*5 = 1.5 marks) Read and understand programs:  3 questions on writing down the output of programs (1*3 = 3 marks)  1) recursive function 2) array 3) pointer Provide solutions to real problems:  1 questions on writing real C codes for a real problem (1*1 = 1 mark) (Array and pointer)

4 Samples 1. Choose high-level languages a c d f a) C language b) machine language c) FORTRAN d) C++ e) assembly language f) Java 2. C stores lists of values in _________. 3. Write down the output of the program #include int main( void ) { printf(“Hello”); return 0; } Output: Hello 4. Write real codes You are asked to provide the function body of the specific function. The main function has already been provided.

5 Basic Concepts Computer hardware and software Phases of C Programs:  Edit (C program file names should end with the.cpp extension)  Preprocess  Compile  Link  Load  Execute C program: begin with main function  int main ( void ) { return 0;}

6 Chapter 1 Input output functions from standard library with header file : scanf(“%d”, &a) and printf(“%3.2f”, b) Preprocessor directives cannot be ended with semicolon  #include  #define SIZE 10 All statements must end with a semicolon ( ; ) Variable names: consist of letters, digits (cannot begin with a digit) and underscores( _ ) Variable definitions appear before executable statements

7 Chapter 2 Operators: assignment = and equal (double equal) ==

8 Chapter 3 All programs written in terms of three control structures  Sequence structures: Built into C. Programs executed sequentially by default  Selection structures: C has three types: if, if…else, and switch  Repetition structures: C has three types: while, do…while and for Please review carefully how to use these three structures especially “if…else” and “for …” Increment and Decrement Operators ++ and –

9 Chapter 4 When we know exactly how many iterations in the loop, we can use counter-controlled repetition. When we do not know the number of iterations in the loop, we can use sentinel-controlled repetition, where we use the sentinel value to stop the repetition.

10 Chapter 4 initialization; while ( loopContinuationTest ) { statement; increment; } do { statement ; } while ( condition ); The break and continue program control statements to alter the flow of program control;

11 Chapter 4 Series of case labels and an optional default case switch ( value ){ case '1': actions case '2': actions default: actions } break; exits from statement Logical operators: && || ! ExpressionResult true && falsefalse true || falsetrue !falsetrue

12 Chapter 5 Functions are basic modules in C language return-value-type function-name( parameter-list ) { declarations and statements; } Return value void The function prototype, function header and function calls should all agree in the number, type, and order of arguments and parameters, and in the type of return value. The mechanisms used to pass information between functions: call-by-value and call-by-reference.

13 Chapter 5 rand() is used to generate random numbers  To get a random number between 1 and n 1 + ( rand() % n ) #include Srand( seed ) is used to initialize the seed of rand()  srand( time( NULL ) );/*load */ time( NULL )  Returns the time at which the program was compiled in seconds  “Randomizes" the seed

14 Chapter 5 Recursive functions  Functions that call themselves Fibonacci series: 0, 1, 1, 2, 3, 5, 8...  Each number is the sum of the previous two  Can be solved recursively: fib( n ) = fib( n - 1 ) + fib( n – 2 )  Code for the fibonacci function long fibonacci( long n ) { if (n == 0 || n == 1) { // base case return n;} else { return fibonacci( n - 1) + fibonacci( n – 2 ); } }

15 Chapter 6 arrayType arrayName[ numberOfElements ]; Initializers int n[ 5 ] = { 1, 2, 3, 4, 5 };  If not enough initializers, rightmost elements become 0 int n[ 5 ] = { 0 } All elements 0  If too many initializers, a syntax error occurs If size omitted, initializers determine it int n[ ] = { 1, 2, 3, 4, 5 };  5 initializers, therefore 5 element array We cannot define an array by neither defining size nor initializing element, for example, int a[];

16 Chapter 6 char string1[] = "first";  Null character '\0' terminates strings  string1 actually has 6 elements scanf( "%s", string2 ); Note that if the subscript is out of range, it will make an error. Bubble sort is to sort an array in either ascending order or descending order. Binary search is to search a key number in a sorted array.

17 Chapter 7 A pointer contains the address of a variable that has a specific value (indirect reference) Pointers cannot be used if not assigned a value  Initialize pointers to 0, NULL, or an address 0 or NULL – points to nothing 0 is the only integer value that can be assigned directly to a pointer variable.  Address operator & and indirection operator *

18 Chapter 7 Call-by-reference is actually copy the pointer and pass the copied pointer to the function. Array name is a const pointer, so that its value cannot be modified. So Array name is a special pointer, and cannot provide all flexibilities like pointers.

19 Chapter 7 Using the const Qualifier with Pointers const int *const myPtr int *const myPtr const int *myPtr int *myPtr Some examples (Chapter 7 lecture notes fig07_21.c )

20 Chapter 7

21

22 The End Thank you very much! Good luck!


Download ppt "COMP1170 Midterm Preparation (March 17 th 2009) Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education."

Similar presentations


Ads by Google