{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

180midterm-sol

# 180midterm-sol - CSC 180H1 F October Midterm 2008 Duration...

This preview shows pages 1–5. Sign up to view the full content.

CSC 180H1 F October Midterm 2008 Duration — 120 minutes Aids allowed: none Student Number: Last Name: First Name: Lecture Section (circle a number): 1 2 Do not turn this page until you have received the signal to start. (Please fill out the identification section above, and read the instructions below.) This midterm consists of 9 questions on 11 pages (including this one). When you receive the signal to start, please make sure that your copy is complete. Comments are not required except where indicated, although they may help us mark your answers. They may also get you part marks if you can’t figure out how to write the code. For 3 bonus mark write your student number at the bottom of pages 2-11 of this test. Bonus Marks : / 3 # 1: / 10 # 2: / 15 # 3: / 10 # 4: / 10 # 5: / 10 # 6: / 15 # 7: / 10 # 8: / 10 # 9: / 10 TOTAL : /100 Total Pages = 11 Page 1 cont’d . . .

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CSC 180H1 F Midterm October 2008 Question 1. [10 marks] Define a function find item that returns the position of the first occurrence of an item in an array. If no such item were found in the array, it returns -1. find item has three parameters: a is array, n is array size, and item is the item to find. For example, given the following array, int a[] = {0, 1, 1, 8, 0, 1, 0, 1, 8, 0}; find item(a, 10, 8) returns 3. /* return index of item in a, -1 if none */ int find_item(int a[], int n, int item) { int i; for (i = 0; i < n; i++) if (a[i] == item) return i; return -1; } Student #: Page 2 of 11 cont’d . . .
CSC 180H1 F Midterm October 2008 Question 2. [15 marks] Define a function find pattern that returns the position of the first occurrence of a pattern in an array. If no such pattern were found in the array, it returns -1. find pattern has four parameters: a is array, n is array size, pattern is pattern array, and m is size of pattern array. For example, given the following two arrays, int a[] = {0, 1, 1, 8, 0, 1, 0, 1, 8, 0}; int b[] = {1, 8, 0}; /* pattern array */ find pattern(a, 10, b, 3) returns 2, the position of the first digit of pattern 1 8 0 in array a . a: 0 1 1 8 0 1 0 1 8 0 ^ | position: 2 /* return index of pattern in a, -1 if none */ int find_pattern(int a[], int n, int pattern[], int m) { int i, j, k; for (i = 0; i < n - m + 1; i++) { for (j = i, k = 0; k < m && a[j] == pattern[k]; k++, j++) ; if (k > 0 && k == m) return i; } return -1; } Student #: Page 3 of 11 cont’d . . .

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CSC 180H1 F Midterm October 2008 Question 3.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 11

180midterm-sol - CSC 180H1 F October Midterm 2008 Duration...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online