UNIVERSITY COLLEGE LONDON
EXAMINATION FOR INTERNAL STUDENTS
MODULE CODE
ASSESSMENT P A TTERN
Copyright By PowCoder代写 加微信 powcoder
MODULE NAME LEVEL:
TIME ALLOWED
COMP0002 COMP0002A4UA
Principles of Programming Undergraduate
07 May 2019
2 hrs 30 mins
This paper is suitable for candidates who attended classes for this module in the following academic year(s):
Y ear 2018-19
EXAMINATION PAPER CANNOT BE REMOVED FROM THE EXAM HALL. PLACE EXAM PAPER AND ALL COMPLETED SCRIPTS INSIDE THE EXAMINATION ENVELOPE
Hall Instructions Standard Calculators N Non-Standard N Calculators
Principles of Programming, COMP0002 (A4U) (COMP101P) Main Summer Exam Period, 2018/19
Suitable for Cohorts: 2018/19, 2017/18
There are TWO questions in total. Answer BOTH questions.
Marks for each part of each question are indicated in square brackets. Calculators are NOT permitted.
C or Haskell code given in your answers does not have to be syntactically perfect but should, at least, be a good approximation.
COMP0002 (COMP101P) 1 TURNOVER
1. (C Language)
a. Show step-by-step how the following C expression is evaluated, giving the value of the expression and the type of that value.
26 I s / s.o – s * 0.2 * 10
b. Explain each of the following:
virtual memory, stack frame, static variable, local scope
c. Outline the stages the gee compiler goes through to compile source code, and ex- plain the role of linking to produce an executable program.
d. Write the following functions, using pointer syntax only, no square bracket syntax:
1. A function that takes two parameters, a C String and a character, and returns the count of the number of times that the character appears in the string. For example, given “Hello World” and ‘l’ the function returns 3.
11. A function that takes a C string as a parameter and counts the number of vow- els (a, e, i , o, u) that appear in the string, returning an in t array holding the countforeachvowel. Forexample,giventhestring”This is some text”, the function would return an in t array holding the values 0, 2, 2, 1, 0 show- ing that the string contains zero ‘a’ characters, two ‘e’ characters, two ‘i’ characters, one ‘o’ character, and zero ‘u’ characters. Use the function from (i) m your answer.
[7 marks] Note: A C string is an array of char with ‘ \ 0’ as the last value in the string.
[Question 1 cont. on next page]
COMP0002 (COMP101P) 2 CONTINUED
[Question 1 cont.]
e. Consider a linked-list of elements constructed using C structs.
1. Give the definition of a struct to represent a linked-list element to be used in a linked-list holding C String values.
11. Write a function that takes a C String as a parameter and returns a pointer to a new linked-list element holding a copy of the string.
m. Write a function that takes an array of C Strings and the size of the array as parameters, and returns a pointer to a linked-list containing copies of the C Strings in the same order as they are in the array.
1v. Write a function that deletes a linked-list, deallocating all the memory used by the linked-list and the C Strings stored in the linked-list.
[Total for Question 1: 50 marks]
COMP0002 (COMP101P) 3 TURNOVER
a. Define the following Haskell terms: higher order function, algebraic data type
b. A Pythagorean triple is a 3-tuple of whole numbers that correspond to the sides of a right angled triangle. Use list comprehension to compute all Pythagorean triples that correspond to triangles with area greater than 100 and with no side greater than 20.
c. Write a recursive function called merge that takes two ordered lists of the same type and merges them into a single ordered list.
d. Define a higher order function called swap that takes two functions as arguments and returns the composition of the two functions in the opposite order of the argu- ments. Be careful to give the correct type for swap.
e. Use higher order library functions to write a function that is called a 11Odd. It should have a single line definition and take a list of positive integers and check whether they are all odd.
f. Give the types ofthe library functions foldr, foldrl, and foldl.
COMP0002 (COMP101P) 4 CONTINUED
[Question 2 cont. on next page]
[Question 2 cont.]
g. Consider this implementation of the library function e l em
elem :: Eq a=> a-> [a] -> Bool elem z [ ] False
elemz (x:xs) I z==x=True
otherwise= elem z xs
Use (++) to write a property that can be used to test this definition of elem via
QuickCheck.
h. Consider the following user defined type:
data Tree a = Nil I Node a (Tree a) (Tree a)
deriving (Eq, Ord, Show, Read)
Define a function called depth that finds the number of nodes in the deepest part of the tree.
1. Write an input-output function, called s t u p i d , that prompts the user to input a whole number then prints the string “Stupid!” that number of times, each time on a separate line. So entering 3 causes the output
Stupid! Stupid! Stupid!
COMP0002 (COMP101P) 5
[Question 2 cont. over page]
[Question 2 cont.]
J. ConsiderthefunctionlistMult,definedbelow,thattakesalistofnumbersand multiplies the elements of the list together. This could be implemented with f o l d r but here it is implemented with a recursive function
listMult :: Num a=> [a] -> a listMult [ ] = 1
listM ult n:ns = n * (listM ult ns)
Write a tail recursive version of l i stMult, called lmt, and prove that it correctly calculates the product of the list when it is given as argument the correct value of the accumulating parameter.
[11 marks]
[Total for Question 2: 50 marks]
COMP0002 (COMP101P) 6 END OF PAPER
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com