Final Examination Version 1
COMP 208 -- Computers in Engineering Wednesday, December 11, 2013 18:00 --- 21:00
Examiner:
Professor Nathan Friedman
Assoc Examiner:
Professor Wenbo He
INSTRUCTIONS:
This is a CLOSED BOOK examination.
This examination consists of 30 multiple choice questions and 3 (three) programming questions, for a total of 33 questions. o
The Examination Security Monitor Program detects pairs of students with unusually similar answer patterns on multiple-choice exams. Data generated by this program can be used as admissible evidence, either to initiate or corroborate an investigation or a charge of cheating under Section 16 of the Code of Student Conduct and Disciplinary Procedures.
Mark your multiple choice answers on the computer sheet using PENCIL ONLY.
Answer questions 31, 32 and 33 in the examination booklet provided.
The examination consists of 17 pages including the cover page.
FACULTY STANDARD CALCULATOR permitted ONLY.
This examination is PRINTED ON BOTH SIDES of the paper
Grading: o
Questions 1 – 30 are worth 2 marks each
o
Question 31 is worth 15 marks
o
Question 32 is worth 15 marks
o
Question 33 is worth 15 marks (Note that this sums to 105. The extra marks are bonus marks)
COMP 208 Final Examination
1 of 17
December 11, 2013 Version 1
Question 1 Which of the following is not a correct variable type in Fortran? a) b) c) d) e)
integer real character float logical
Question 2 The C function free() is used to a) b) c) d) e)
Generate a pseudorandom number. Reclaim dynamically allocated storage Return the number of operations a program performs Seed or restart a random number generator None of the above
Question 3 What is the role of IMPLICIT NONE in a FORTRAN program? a) b) c) d)
It tells the compiler that implicit DO loops are not allowed in this program It forces the programmer to declare all variables It allows the programmer to create variables on the fly It does not serve any purpose and can be omitted: It is only a legacy from early versions of FORTRAN e) None of the above Question 4 How do you study a text book recursively? a) (1) Read the entire book in one day, and (2) read it again every day of the semester. b) (1) If there is one page left, read it, and you are done; otherwise: (2) study one page, then study the rest of the book c) (1) Rip the book in two, and (2) study each half. d) (1) Read all the pages in one horrible cram session the night before the final, and (2) forget everything the next day.
COMP 208 Final Examination
2 of 17
December 11, 2013 Version 1
Question 5 Which of the following statements is false? a) b) c) d) e)
Linear search begins by comparing the first (or last) element in the array. If a match is not present linear search examines each element in the array. Linear search takes O(n) time on average Linear search only works if the array is in sorted order Binary search is faster than linear search for any array
Question 6 If a binary search of a sorted array A between left and right terminates with left0. The function is supposed to compute the sum of the first n integers: 1+2+3+…+n. int sum(int n){ /* body */ } Which of the following code segments when substituted for body will correctly implement this function? 1) return n + sum(n-1); 2) if (n==1) return 1; else return n + sum(n-1); 3) if (n==1) return 1; else return sum(n) + sum(n-1); a) b) c) d) e)
1 only 2 only 3 only 1 and 2 only 1, 2 and 3
COMP 208 Final Examination
14 of 17
December 11, 2013 Version 1
Question 29 In writing the following C program, a programmer mistakenly wrote the expression i = 0 instead of i == 0 as the condition in the if statement. What is the result of the program as written? #include<stdio.h> int main(){ int i; for (i = 0;i < 3;i++){ if (i = 0) printf ("i "); else break; printf("3 "); } printf("4 "); return 0; } a) b) c) d) e)
04 0124 034 4 The compiler reports a syntax error.
COMP 208 Final Examination
15 of 17
December 11, 2013 Version 1
Question 30 What is the output of the following C program? #include <stdio.h> void swap(int *a, int *b){ int tmp = 0; tmp = *a; *a = *b; *b = tmp; } void main() { int a[2], b[2], c=3; a[0] = 1; a[1] = 2; b[0] = 10; b[1] = 11; swap(a,b); swap(b,&c); printf("a[0]=%d, a[1]=%d, b[0]=%d, b[1]=%d, c=%d\n", a[0], a[1], b[0], b[1], c); } a) b) c) d)
a[0]=1, a[1]=2, b[0]=10, b[1]=11, c=3 a[0]=3, a[1]=2, b[0]=1, b[1]=11, c=10 a[0]=10, a[1]=2, b[0]=1, b[1]=11, c=3 a[0]=10, a[1]=2, b[0]=3, b[1]=11, c=1
Question 31 (Programming) The greatest common divisor (GCD) of two non-negative integers is the largest number that divides both of them without any remainder. We can prove the following facts about the GCD. GCD(k,k) = k, for k>0 GCD(0,k) = GCD(k,-) = k, for k>0 For i>j, GCD(i,j) = GCD(i-j,j) Based on these facts, define a recursive function to compute the GCD of two positive integers.
COMP 208 Final Examination
16 of 17
December 11, 2013 Version 1
Question 32 (Programming) The Gaussian error function, Erf(y), is defined as:
Erf( y )
2
y
x e dx 2
0
Your goal is to write a C program to compute Erf(y) for various values of y. Please follow the following steps: (a)
Write a function to compute the integrand of the above function ( e x ). The function should have the following prototype: 2
double integrand(double x)
(b)
Write a general-purpose implementation of any integration algorithm you’ve seen in this class (midpoint, trapezoidal, Simpson, or Monte Carlo). It should have the following prototype: double integral(DfD fp, double a, double b, int n) (Where DfD
is defined as : typedef double (*DfD) (double);)
fp is a pointer to the function you would like to integrate, a is the left bound, b is the right bound, and n is the number of panels/segments. (c)
Write a function that uses the integral function to evaluate Erf(y). It should have the following prototype: double Erf(double y)
Use n=500 panels. Question 33 (Programming) An ancient method for finding all primes less than N, for a given N, is known as the sieve of Eratosthenes. This algorithm works as follows: 1. Create a list of consecutive integers from 2 to N (2, 3, 4, …, N) 2. Let p=2, the first prime number 3. Starting from 2*p cross off all multiples of p from the list (2p, 3p, 4p, etc). This can be done by writing 0 in place of the number. Note that some of them may already have been deleted. 4. Change p to the first non-zero value in the list greater than the previous value of p. 5. Repeat this process from step 3 until p is larger than √N Write a program (in C or in Fortran) that uses this method to compute a list of all primes less than 10000.
COMP 208 Final Examination
17 of 17
December 11, 2013 Version 1