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

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

  • View
    213

  • Download
    0

Embed Size (px)

Citation preview

COMP1170

Midterm Preparation (March 17th 2009)

Acknowledgment

The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.

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!

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)

Samples

1. Choose high-level languages a c d fa) C language b) machine language c) FORTRAN d) C++

e) assembly language f) Java2. C stores lists of values in _________.

3. Write down the output of the program#include <stdio.h>int main( void ) { printf(“Hello”); return 0; }Output: Hello

4. Write real codesYou are asked to provide the function body of the specific

function. The main function has already been provided.

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;}

Chapter 1

Input output functions from standard library with header file <stdio.h>: scanf(“%d”, &a) and printf(“%3.2f”, b)

Preprocessor directives cannot be ended with semicolon #include <stdio.h> #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

Chapter 2

Operators: assignment = and equal (double equal) ==

Operators Associativity Type

[] () left to right highest

++ -- !

(type) right to left unary

* / % left to right multiplicative

+ - left to right additive

< <= > >=

left to right relational

== != left to right equality

&& left to right logical AND

|| left to right logical OR

?: right to left conditional

= += -= *= /= %= right to left assignment

, left to right comma

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 –

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.

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;

Chapter 4

Series of case labels and an optional default caseswitch ( value ){

case '1':actions

case '2':actions

default:actions

}

break; exits from statement Logical operators: && || !

Expression Result

true && false falsetrue || false true!false true

Chapter 5

Functions are basic modules in C languagereturn-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.

Chapter 5

rand() is used to generate random numbers To get a random number between 1 and n

1 + ( rand() % n ) #include <stdlib.h> Srand(seed) is used to initialize the seed of

rand() srand( time( NULL ) );/*load <time.h> */

time( NULL ) Returns the time at which the program was compiled in seconds “Randomizes" the seed

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 ); }}

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 itint 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[];

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.

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 *

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.

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)

Chapter 7

1 /* Fig. 7.21: fig07_21.c

2 Copying a string using array notation and pointer notation. */

3 #include <stdio.h>

4

5 void copy1( char * const s1, const char * const s2 ); /* prototype */

6 void copy2( char *s1, const char *s2 ); /* prototype */

7

8 int main( void )

9 {

10 char string1[ 10 ]; /* create array string1 */

11 char *string2 = "Hello"; /* create a pointer to a string */

12 char string3[ 10 ]; /* create array string3 */

13 char string4[] = "Good Bye"; /* create a pointer to a string */

14

15 copy1( string1, string2 );

16 printf( "string1 = %s\n", string1 );

17

18 copy2( string3, string4 );

19 printf( "string3 = %s\n", string3 );

20

21 return 0; /* indicates successful termination */

22

23 } /* end main */

24

Chapter 725 /* copy s2 to s1 using array notation */

26 void copy1( char * const s1, const char * const s2 )

27 {

28 int i; /* counter */

29

30 /* loop through strings */

31 for ( i = 0; ( s1[ i ] = s2[ i ] ) != '\0'; i++ ) {

32 ; /* do nothing in body */

33 } /* end for */

34

35 } /* end function copy1 */

36

37 /* copy s2 to s1 using pointer notation */

38 void copy2( char *s1, const char *s2 )

39 {

40 /* loop through strings */

41 for ( ; ( *s1 = *s2 ) != '\0'; s1++, s2++ ) {

42 ; /* do nothing in body */

43 } /* end for */

44

45 } /* end function copy2 */ string1 = Hello string3 = Good Bye

The End

Thank you very much! Good luck!