Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

COMP0002

Principles of Programming

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 / 5 / 5.0 — 5 0.2 10

[5 marks]

b. Explain each of the following:

virtual memory, stack frame, static variable, local scope

[8 marks]

c. Outline the stages the gcc compiler goes through to compile source code, and explain the role of linking to produce an executable program.

[6 marks]

d. Write the following functions, using pointer syntax only, no square bracket syntax:

i. 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 66Hello World^, and '丄'the function returns 3.

[4 marks]

ii. A function that takes a C string as a parameter and counts the number of vowels (a, e, i, o, u) that appear in the string, returning an int array holding the count for each vowel. For example, given the string “This is some text", the function would return an int array holding the values 0,2,2,1, 0 showing that the string contains zero 6a9 characters, two 'e' characters, two 'i' characters, one 'o' character, and zerocharacters. Use the function from (i) in 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]

[Question 1 cont.]

e. Consider a linked-list of elements constructed using C structs.

i. Give the definition of a struct to represent a linked-list element to be used in a linked-list holding C String values.

[2 marks]

ii. 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.

[4 marks]

iii. 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.

[8 marks]

iv. 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.

[6 marks]

[Total for Question 1: 50 marks]


2. (Haskell Language)

a. Define the following Haskell terms:

higher order function, algebraic data type

[4 marks]

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.

[3 marks]

c. Write a recursive function called merge that takes two ordered lists of the same type and merges them into a single ordered list.

[6 marks]

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 arguments. Be careful to give the correct type for swap.

[3 marks]

e. Use higher order library functions to write a function that is called allOdd. It should have a single line definition and take a list of positive integers and check whether they are all odd.

[3 marks]

f. Give the types of the library functions foldr, f oldrl, and foldl.

[6 marks]

g. Consider this implementation of the library function elem

elem :: Eq a => a -> [a] -> Bool

elem z [ ] = False

elem z (x:xs) | z == x = True

I otherwise = elem z xs

Use (++) to write a property that can be used to test this definition of elem via QuickCheck.

[3 marks]

h. Consider the following user defined type:

data Tree a = Nil | 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.

[4 marks]

i. Write an input-output function, called stupid, that prompts the user to input a whole number then prints the string ^6Stupid!^, that number of times, each time on a separate line. So entering 3 causes the output

Stupid!

Stupid!

Stupid!

[7 marks]

j. Consider the function listMult, defined below, that takes a list of numbers and multiplies the elements of the list together. This could be implemented with foldr but here it is implemented with a recursive function

listMult :: Num a => [a] -> a

listMult [ ] = 1

listMult n:ns = n * (listMult ns)

Write a tail recursive version of listMult, called Imt, 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]